CS 241 Data Organization More trees March 29, 2018
Removing Node from Ordered Tree How can we remove an item from this tree, keeping it ordered? aid? of? the? now is the for men of time all good party their to aid come
Three possible cases • Node has no children (is a leaf): Just remove the node. • Node has one child: Remove node, replace with child. • Node has two children: Replace value in node with value from in-order successor (or predecessor) node and delete that node instead.
Balanced Binary Tree • A binary tree is balanced if height of left and right children of every node differ by at most 1. • A balanced BST has height of O (log n ), so operations run in O (log n ) time.
AVL Tree • An AVL tree is a self-balancing binary search tree. • Named after its Soviet inventors: Adelson-Velskii and Landis • Add/remove as usual, but follow with tree rotations to restore balance.
AVL tree: Example 5 5 3 D 4 D A 4 3 C B C A B
AVL tree: Example 5 4 3 5 4 D A B C D 3 C A B
Recommend
More recommend