Programming Part 10
Programming Part 10
The first node is the root. Each node can have up to two children only.
• They support fast search, insert, and delete operations (average O(log n)).
What is Traversal?
Clock Concept
• 12 o’clock: visit
• 9 o’clock: go left
• 3 o’clock: go right
Algorithm:
Pseudocode:
Example Result: 50 30 19 43 60
Algorithm:
Algorithm:
3. Visit root
Pseudocode:
Example Result: 19 43 30 60 50
Insert Algorithm:
Insert(root, value):
ELSE
root.right = Insert(root.right, value)
RETURN root
Steps:
• Else → go right
• Just remove it
Pseudocode:
Delete(root, value):
ELSE
successor = FindMin(root.right)
root.data = successor.data
RETURN root
FindMin(node):
node = node.left
RETURN node
Pseudocode:
Search(root, item):
RETURN FOUND
root = root.left
ELSE
root = root.right
• Traversal Types:
8. Practice Tasks
1. Draw the binary tree for: [70, 50, 90, 30, 60, 80, 100, 20]
A. 40 20 10 30 60 80
B. 10 20 30 40 60 80
C. 10 30 20 40 80 60
D. 20 10 30 60 80 40
2. Which traversal method visits the root first, then left, then right? A. In-Order
B. Post-Order
C. Pre-Order
D. Reverse-Order
3. Which case is the most complex when deleting from a BST? A. Node with no children
B. Node with one child
C. Node with two children
D. Deleting the root
• Traversals:
• Deletion Cases:
o Leaf → remove directly
Contact info :
email : tinodaishemchibi@gmail.com
phone (whatsapp) : +263 71 817 6525
Website: bluewave-portfolio-nexus