I'll create a comprehensive study guide for you right now!
๐ BST & AVL Trees Complete Study Guide
๐ณ BST Visual Reference
code
50 A BST with root 50
/ \
30 70 Properties:
/ \ / \ - Left subtree values < node value
10 40 60 90 - Right subtree values > node value
- No duplicates allowed
๐ BST Operations Cheat Sheet
Searching (O(log n) average, O(n) worst)
code
search(root, key):
if root is null or root.key = key:
return root
if key < root.key:
return search(root.left, key)
else:
return search(root.right, key)
Insertion (O(log n) average, O(n) worst)
code
insert(root, key):
if root is null:
return new Node(key)
if key < root.key:
root.left = insert(root.left, key)
else if key > root.key:
root.right = insert(root.right, key)
return root
Deletion (O(log n) average, O(n) worst)
code
delete(root, key):
if root is null:
return root
if key < root.key:
root.left = delete(root.left, key)
else if key > root.key:
root.right = delete(root.right, key)
else:
// Node with only one child or no child
if root.left is null:
return root.right
else if root.right is null:
return root.left
// Node with two children
root.key = minValue(root.right) // Find inorder successor
root.right = delete(root.right, root.key)
return root
๐ Tree Traversals
In-order (Left โ Root โ Right) - GIVES SORTED ORDER
code
inorder(root):
if root is not null:
inorder(root.left)
print(root.key)
inorder(root.right)
Pre-order (Root โ Left โ Right)
code
preorder(root):
if root is not null:
print(root.key)
preorder(root.left)
preorder(root.right)
Post-order (Left โ Right โ Root)
code
postorder(root):
if root is not null:
postorder(root.left)
postorder(root.right)
print(root.key)