Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "1 More Trees 2-3-4 Trees, Red-Black Trees, B Trees."— Presentation transcript:

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

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

3 3 Non Binary Trees Some applications require more than two children per node Genealogical tree Game tree

4 4 2-3-4 Trees Drozdek Section 7.1.7 We extend searching technique for BST We will now have more than two paths that a search may follow from a given node. Can keep the tree balanced without doing rotations. Define m-node in a search tree Stores m – 1 data values k 1 < k 2 … < k m-1 Has links to m subtrees T 1..T m Where for each i all data values in T i < k i ≤ all values in T k+1

5 5 2-3-4 Trees Example of m-node Definition of 2-3-4 tree: Each node stores at most 3 data values Each internal node is a 2-node, a 3-node, or a 4-node All the leaves are on the same level.

6 6 2-3-4 Trees Example of a 2-3-4 node which stores integers To search for 36 Start at root … 36 < 53, go left Next node has 27 < 36 < 38. take middle link Find 36 in next lowest node

7 7 Building a Balanced 2-3-4 Tree If tree is empty Create a 2-node containing new item, root of tree Otherwise 1. Find leaf node where item should be inserted by repeatedly comparing item with values in node and following appropriate link to child 2. If room in leaf node Add item 3. Otherwise … (next slide)

8 8 Building a Balanced 2-3-4 Tree Otherwise (Leaf holds three values): a. Split the 4-node into two 2 nodes, one storing the item less than median value, other storing the item greater than median. b. Move median value up to the parent having these 2 nodes as children c. If parent has no room for median, spilt that 4-node in same manner, continuing until either a parent is found with room or we reach the root. d. If root is a 4-node split it into two 2-nodes, create new parent 2-node as the new root

9 9 Building a Balanced 2-3-4 Tree Note sequence of building a tree by adding numbers... Add 41 Add 70 Add 38 Add 16 Add 36 Add 59 Add 73

10 10 Example Let's repeat the example of adding states to a BST, using a 2-3-4 tree. Add RI, PA, DE, GA, OH, MA, IL, MI, IN, NY, VT, TX, WY

11 11 Building a Balanced 2-3-4 Tree Note that last step in algorithm may require multiple splits up the tree. Instead of bottom-up insertion Can do top-down insertion. Do not allow any parent nodes to become 4-nodes. Split 4-nodes along search path as encountered End of Section


Download ppt "1 More Trees 2-3-4 Trees, Red-Black Trees, B Trees."

Similar presentations


Ads by Google