AVL Tree

Review

  • AVL Tree: BST Lesson 13 AVL Tree, 2-3-4 Tree, Red-Black Tree - 图1
  • RR rotate AVL tree:
    Lesson 13 AVL Tree, 2-3-4 Tree, Red-Black Tree - 图2
    An anti-clock rotation

Another special case of AVL tree

simple version

RL case
Lesson 13 AVL Tree, 2-3-4 Tree, Red-Black Tree - 图3
2 rotations

general version

    1. After insertion in Lesson 13 AVL Tree, 2-3-4 Tree, Red-Black Tree - 图4, root A,B,C become unblanced:

Lesson 13 AVL Tree, 2-3-4 Tree, Red-Black Tree - 图5

    1. Rotate C, B

Lesson 13 AVL Tree, 2-3-4 Tree, Red-Black Tree - 图6

    1. rotate A, C, return to balance

Lesson 13 AVL Tree, 2-3-4 Tree, Red-Black Tree - 图7
After insertion, the height of the tree maintain

Summarize

  • RR, LL: one rotation
  • RL, LR: two rotations

Removal

Similar to insertion, rotate while needed.
When remove the root, pick up a leaf

2-3-4 Tree

AVL tree: rebalance “almost” immediately
2-3-4 tree: temp storage
2-3-4:three types of child node: 2- node, 3-node, 4-node
Lesson 13 AVL Tree, 2-3-4 Tree, Red-Black Tree - 图8

2-4 tree

Lesson 13 AVL Tree, 2-3-4 Tree, Red-Black Tree - 图9

  • flexible: 2-node, 3-node, 4-node
  • strict(compare to BST): all leaves same height

Lesson 13 AVL Tree, 2-3-4 Tree, Red-Black Tree - 图10
While insertion, extend root to maintain the two limits

When insert 13: overflow Lesson 13 AVL Tree, 2-3-4 Tree, Red-Black Tree - 图11 How to divide?

Lesson 13 AVL Tree, 2-3-4 Tree, Red-Black Tree - 图12
Do this operation recursively until the 2-3-4 tree sable

Red-Black Tree

2-3-4 tree to red-black tree

Sequential insert data to the 234 tree:

    1. insert 1,2,3,
      Lesson 13 AVL Tree, 2-3-4 Tree, Red-Black Tree - 图13
    1. When 4 is inserted, 2 go to the upper layer
      Lesson 13 AVL Tree, 2-3-4 Tree, Red-Black Tree - 图14
    1. Continue to insert 5,6,7,8,9
      Lesson 13 AVL Tree, 2-3-4 Tree, Red-Black Tree - 图15
    1. When insert 10
      Lesson 13 AVL Tree, 2-3-4 Tree, Red-Black Tree - 图16

Then, we turn the 2-3-4 tree to a binary search tree:
Let suqre for 2-3-4 node, circle for BST node:
Lesson 13 AVL Tree, 2-3-4 Tree, Red-Black Tree - 图17

An example for 2-3-4 tree transformed to red-black tree

2-3-4 tree:
Lesson 13 AVL Tree, 2-3-4 Tree, Red-Black Tree - 图18
one type of RBT:
Lesson 13 AVL Tree, 2-3-4 Tree, Red-Black Tree - 图19

Insertion for red-black tree

  • insert 1.5

Lesson 13 AVL Tree, 2-3-4 Tree, Red-Black Tree - 图20
just insert a red node after node1

  • insert 4.5

Lesson 13 AVL Tree, 2-3-4 Tree, Red-Black Tree - 图21
just insert a red node after node 4

  • insert 1.8

if we just add node 1.8 after node 1.5
This is illegal, we change it like the AVL tree’s rotation:
Lesson 13 AVL Tree, 2-3-4 Tree, Red-Black Tree - 图22
Lesson 13 AVL Tree, 2-3-4 Tree, Red-Black Tree - 图23
We summarize into two sentences:
新生先为红
红父不能容

We focus on the two types of insertion seperately:

    1. new node inserted to a 3-node

Lesson 13 AVL Tree, 2-3-4 Tree, Red-Black Tree - 图24

黑叔逼爷退

    1. new node inserted to a 4-node
    • Firstly,the node just inserted to the 4-node:
      Lesson 13 AVL Tree, 2-3-4 Tree, Red-Black Tree - 图25
    • Secondly, we split the two sides’ nodes
      Lesson 13 AVL Tree, 2-3-4 Tree, Red-Black Tree - 图26
    • Finaly, we change the root’s colour to red and insert it up(recursively until satisfy the RBT rule)
      Lesson 13 AVL Tree, 2-3-4 Tree, Red-Black Tree - 图27
      红叔变色龙