Advertisement
coffeebeforecode

pseudo-1-dsa

Jun 22nd, 2021
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.90 KB | None | 0 0
  1. struct node
  2.     declare data: integer
  3.     declare next: pointer of type node
  4. end struct
  5.  
  6. begin procedure newNode
  7.     pass in: integer val
  8.     initialize pointer temp of type node and allocate memory to it
  9.     temp→data ← val
  10.     temp→left_child ← NULL
  11.     temp→right_child ← NULL
  12.     pass out pointer temp of type node
  13. end procedure
  14.  
  15. begin procedure insert
  16.     pass in: pointer root of type node, integer data
  17.     if root is equal to NULL
  18.         return call newNode(data)
  19.     end if
  20.     if data is less than root→data
  21.         root→left_child ← call insert(root→left_child, data)
  22.     else
  23.         root→right_child ← call insert(root→right_child, data)
  24.     end if
  25.     pass out: pointer root of type node
  26. end procedure
  27.  
  28. begin procedure minValueNode
  29.     pass in: pointer root of type node
  30.     initialize pointer current of type node as root
  31.     while current and current→left_child is not equal to NULL do
  32.         current ← current→left_child
  33.     end do
  34.     pass out: pointer root of type node
  35. end procedure
  36.  
  37. begin procedure deleteNode
  38.     pass in: pointer root of type node, integer data
  39.     if root is equal to NULL
  40.         return root
  41.     end if
  42.     if data is less than root→data
  43.         root→left_child ← call deleteNode(root→left_child, data)
  44.     else if data is greater than root→data
  45.         root→right_child ← call deleteNode(root→right_child, data)
  46.     else
  47.         if root→left_child is equal to NULL
  48.             initialize pointer temp of type node as root→right_child
  49.             free the pointer root
  50.             return temp
  51.         else if root→right_child is equal to NULL
  52.             initialize pointer temp of type node as root→left_child
  53.             free the pointer root
  54.             return temp
  55.         end if
  56.         initialize pointer temp of type node as call minValueNode(root→right_child)
  57.         root→data ← temp→data
  58.         root→right_child ← call deleteNode(root→right_child, temp→data)
  59.     end if
  60.     pass out: pointer root of type node
  61. end procedure
  62.  
  63. begin procedure height
  64.     pass in: pointer root of type node
  65.     if root is equal to NULL
  66.         return NULL
  67.     else
  68.         initialize lheight as call height(root→left_child)
  69.         initialize rheight as call height(root→right_child)
  70.         if lheight is greater than rheight
  71.             return (lheight+1)
  72.         else
  73.             return (rheight+1)
  74.         end if
  75.     end if
  76. end procedure
  77.  
  78. begin procedure printGivenLevel
  79.     pass in: pointer root of type node, integer level
  80.     if root is equal to NULL
  81.         return NULL
  82.     end if
  83.     if level is equal to 1
  84.         print root→data
  85.     else if level is greater than 1
  86.         call printGivenLevel(root→left_child, level-1)
  87.         call printGivenLevel(root→right_child, level-1)
  88.     end if
  89.     pass out: NULL
  90. end procedure
  91.  
  92. begin procedure printLevelOrder
  93.     pass in: pointer root of type node
  94.     initialize integer h as call height(root)
  95.     for i from 1 to h do
  96.         call printGivenLevel(root, i)
  97.     end do
  98.     pass out: NULL
  99. end procedure
  100.  
  101. begin procedure printPreOrder
  102.     pass in: pointer root of type node
  103.     if root is not equal to NULL
  104.         print root→data
  105.         call printPreOrder(root→left_child)
  106.         call printPreOrder(root→right_child)
  107.     end if
  108.     pass out: NULL
  109. end procedure
  110.  
  111. begin procedure printInOrder
  112.     pass in: pointer root of type node
  113.     if root is not equal to NULL
  114.         call printInOrder(root→left_child)
  115.         print root→data
  116.         call printInOrder(root→right_child)
  117.     end if
  118.     pass out: NULL
  119. end procedure
  120.  
  121. begin procedure printPostOrder
  122.     pass in: pointer root of type node
  123.     if root is not equal to NULL
  124.         call printPostOrder(root→left_child)
  125.         call printPostOrder(root→right_child)
  126.         print root→data
  127.     end if
  128.     pass out: NULL
  129. end procedure
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement