1 / 18

More Trees II

More Trees II. 2-3-4 Trees, Red-Black Trees, B Trees. Objectives. You will be able to Describe Red-Black trees. Describe B-Trees. Red-Black Trees. A way of constructing 2-3-4 trees from 2-nodes. Defined as a BST which: Has two kinds of links (red and black).

baina
Download Presentation

More Trees II

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. More Trees II 2-3-4 Trees, Red-Black Trees, B Trees

  2. Objectives You will be able to • Describe Red-Black trees. • Describe B-Trees.

  3. Red-Black Trees • A way of constructing 2-3-4 trees from 2-nodes. • Defined as a BST which: • Has two kinds of links (red and black). • Every path from root to a leaf node has same number of black links. • No path from root to leaf node has more than two consecutive red links. • Data member added to store color of link from parent.

  4. Red-Black Trees enum ColorType {RED, BLACK}; class RedBlackTreeNode { public: DataType data; ColorType parentColor; // RED or BLACK RedBlackTreeNode * parent; RedBlackTreeNode * left; RedBlackTreeNode * right; } • Now possible to construct a red-black tree to represent a 2-3-4 tree

  5. Red-Black Trees • We will convert each 3-Node in the 2-3-4 tree into two 2-Nodes. • Each 4-Node into three 2-Nodes.

  6. Red-Black Trees • 2-3-4 tree represented by red-black trees as follows: • Make a link black if it is a link in the 2-3-4 tree. • Make a link red if it connects nodes containing values in same node of the 2-3-4 tree. Some authors use h and v instead of red and black. h (horizontal) links connect nodes from the same node of the 2-3-4 tree. v (vertical) links are links from the 2-3-4 tree.

  7. Example • 2-3-4 tree • Corresponding red-black tree 59 55

  8. Adding to a Red-Black Tree • Do top-down insertion as with 2-3-4 tree • Search for place to insert new node – Keep track of parent, grandparent, great grandparent. • When 4-node q encountered, split as follows:a. Change both links of q to blackb. Change link from parent to red:

  9. Adding to a Red-Black Tree • If now two consecutive red links, (from grandparent gp to parent p to q)Perform appropriate AVL type rotation determined by direction (left, right, left-right, right-left) from gp -> p-> q

  10. Example • Let's convert the quiz solution into a Red-Black tree.

  11. MA GA PA TX IL IN VT WY RI DE MI NY OH After Adding WY

  12. MA GA TX PA RI WY VT OH IN NY DE IL MI Corresponding Red-Black Tree

  13. MA GA TX PA RI WY VT OH IN NY DE IL MI Will be right child of MI Add NM We have to split the "4-node" of MI-NY-OH

  14. After Adding NM MA GA TX PA NY RI WY VT OH IN MI NM DE IL End of Section

  15. B-Trees • Drozdek Chapter 7

  16. B-Trees • Etymology unknown • Rudolf Bayer and Ed McCreight invented the B-tree while working at Boeing Research Labs in 1971, but they did not explain what, if anything, the B stands for. • Balanced Trees ? • Bayer Trees ? • Boeing Trees ? • See http://en.wikipedia.org/wiki/B-tree • Many variations: B+ Trees, B* Trees • See Drozdek Chapter 7

  17. B-Trees • Previous trees used in internal searching schemes • Tree sufficiently small to be all in memory • B-trees are intended for external searching • Data stored in secondary memory • Each node is a block on disk. • Typically the "data" in a node is really a pointer. • B-tree of order m has properties: • The root has at least two subtrees unless it is a leaf. • Each node stores at most m – 1 data values • and has at most m links to subtrees. • Each internal node stores at least ceil(m/2) data values. • All leaves on same level

  18. B-Trees • A 2-3-4 tree is a B-tree of order 4 • Note example below of order 5 B-tree • Best performance for disk storage found to be with values for 50 ≤ m ≤ 400 End of Presentation

More Related