Trees Complete Notes
Trees Complete Notes
1. Basic Concepts
2. Types of Trees
- Binary Tree
- AVL Tree
- Red-Black Tree
- B-Tree
- Trie
3. Tree Traversals
Sample Code:
def inorder(root):
if root:
inorder(root.left)
print(root.data)
inorder(root.right)
Sample Insert:
else:
return root
def count_leaves(root): return 1 if root and not root.left and not root.right else count_leaves(root.left) +
count_leaves(root.right)
def is_balanced(root):
def check(root):
lh = check(root.left)
rh = check(root.right)
return check(root) != -1
8. Mirror Tree
Tree Data Structures - Complete Notes
def mirror(root):
if root:
mirror(root.left)
mirror(root.right)
9. Applications
- Expression Trees
- Decision Trees
- Tries
- Heaps
- Segment Trees
- Left Rotate
- Right Rotate
- Left-Right Rotate
- Right-Left Rotate
Sample Rotation:
def right_rotate(y):
x = y.left
T2 = x.right
x.right = y
y.left = T2
return x