Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 Balanced Search Trees  several varieties  AVL trees  2-3-4 trees  Red-Black trees  B-Trees (used for searching secondary memory)  nodes are added.

Similar presentations


Presentation on theme: "1 Balanced Search Trees  several varieties  AVL trees  2-3-4 trees  Red-Black trees  B-Trees (used for searching secondary memory)  nodes are added."— Presentation transcript:

1 1 Balanced Search Trees  several varieties  AVL trees  2-3-4 trees  Red-Black trees  B-Trees (used for searching secondary memory)  nodes are added and deleted so that the height of the tree is kept under control  insert and delete take more work, but retrieval (also insert & delete) never more than log 2 n because height is controlled

2 2 AVL Trees  a BST where each node has a balance factor  balance factor of a leaf node is 0  balance factor of a node:  height of left subtree - height of right subtree  insertions or deletions change the balance factor of one or more nodes  if a balance factor becomes 2 or -2 the AVL tree must be rebalanced  done by rotating nodes

3 3 Some AVL Trees 0 00 0 1 0 0 balance is height(left subtree) - height(right subtree)

4 4 Inserting an item  follow a search path as for a BST  allocate a node and insert the item at the end of the path (as for BST)  balance factor of new node is 0  as recursion unwinds update the balance factors  if a balance factor becomes 2 or -2 perform a rotation to bring the AVL tree back into balance

5 5 An Insertion 0 1 0 1 0 0 0 01 0 1 0 00 no rotation required 0 0 #'s are balance factors

6 6 Another Insertion 0 01 0 1 0 0 0 01 0 0 0 00 0 0 12 simple right rotation required 0

7 7 Another Insertion 0 01 0 0 0 00 0 0 0 0 0 0 0 0 1 0 -2 simple left rotation required 0

8 8 Another Insertion 0 0 0 1 0 00 0 1 1-2 double rotation needed a. right rotation around right subtree of the unbalanced subtree b. left rotation around root of the unbalanced subtree

9 9 The right rotation 00 -2 1 0 1 00 0 1 -2 1 0 01 0

10 10 The left rotation 00 -2 1 0 1 00 0 0 0 1 1 0 1 00

11 11 AVL Trees  oldest form of balanced search tree  maximum height is 1.4 log 2 N  insert, delete and retrieve always O(log 2 N)  rebalancing needed for about 45% of the insertions  about half of the re-balancings require double rotations  Named for inventors: Adelson-Velskii and Landis

12 12 2-3-4 Tree  uses larger nodes  a node has fields for 3 items and 4 nodePointers item item item  2-3-4 tree increases in height from the top, not the bottom because all leaf nodes are on the same level  insertion and deletion simpler than AVL tree but space is wasted  3/4 of the nodePointers are NULL  some nodes hold only 1 or 2 items

13 13 Inserting 35, 12, 68, 22, 75 12 35 68 35 -- -- 12 22 --68 75 --

14 14 Red-Black Tree  implementation of a 2-3-4 tree which does not require space which is unused  nodes are like those for a BST with the addition of a color field (red/black)  search and traverse ignore the node color  insert and delete use color to determine when a rotation is needed to keep the tree balanced  tree height guaranteed to be O(log 2 N)  the underlying data structure for the STL's associative containers

15 15 A red-black tree 35 -- -- 12 22 --68 75 -- 35 22 75 12 68


Download ppt "1 Balanced Search Trees  several varieties  AVL trees  2-3-4 trees  Red-Black trees  B-Trees (used for searching secondary memory)  nodes are added."

Similar presentations


Ads by Google