1 More Trees Trees, Red-Black Trees, B Trees
2 Objectives You will be able to Describe trees. Describe Red-Black trees. Describe B-Trees.
3 Non Binary Trees Some applications require more than two children per node Genealogical tree Game tree
Trees Drozdek Section 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
Trees Example of m-node Definition of 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.
Trees Example of a 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 Building a Balanced 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 Building a Balanced 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 Building a Balanced 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 Example Let's repeat the example of adding states to a BST, using a tree. Add RI, PA, DE, GA, OH, MA, IL, MI, IN, NY, VT, TX, WY
11 Building a Balanced 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