Presentation is loading. Please wait.

Presentation is loading. Please wait.

General Trees and Variants CPSC 335. General Trees and transformation to binary trees B-tree variants: B*, B+, prefix B+ 2-4, Horizontal-vertical, Red-black.

Similar presentations


Presentation on theme: "General Trees and Variants CPSC 335. General Trees and transformation to binary trees B-tree variants: B*, B+, prefix B+ 2-4, Horizontal-vertical, Red-black."— Presentation transcript:

1 General Trees and Variants CPSC 335

2 General Trees and transformation to binary trees B-tree variants: B*, B+, prefix B+ 2-4, Horizontal-vertical, Red-black trees Lecture Outline

3  Trees in which the number of subtrees of any node need not be required to be 0, 1, or 2.  The number of subtrees for any node may be variable. Some nodes may have 1 or no subtrees, others may have 3, some 4, or any other combination.  Ternary tree – 3 subtrees per node General Trees

4  Problems The number of references for each node must be equal to the maximum that will be used in the tree. Most of the algorithms for searching, traversing, adding and deleting nodes become much more complex in that they must now cope with situations where there are not just two possibilities for any node but multiple possibilities.  Solution General trees can be converted to binary trees The algorithms that are used for binary tree processing can be used with minor modifications. Problems with General Trees and Solutions

5  Conversion process Use the root of the general tree as the root of the binary tree. Determine the first child of the root. This is the leftmost node in the general tree at the next level. Insert this node. The child reference of the parent node refers to this node. Continue finding the first child of each parent node and insert it below the parent node with the child reference of the parent to this node. Creating a Binary Tree from a General Tree

6  Conversion process (continued) When no more first children exist in the path just used, move back to the parent of the last node entered and repeat the above process. Complete the tree for all nodes. In order to locate where the node fits you must search for the first child at that level and then follow the sibling references to a nil where the next sibling can be inserted. The children of any sibling node can be inserted by locating the parent and then inserting the first child. Then the above process is repeated. Creating a Binary Tree from a General Tree

7 General Tree Binary Tree

8 Traversing a Tree When the general tree has been represented as a binary tree, the algorithms which were used for the binary tree can now be used for the general tree. In-order traversals make no sense when a general tree is converted to a binary tree. In the general tree each node can have more than two children so trying to insert the parent node in between the children is rather difficult, especially if there is an odd number of children. Pre - order This is a process where the root is accessed and processed and then each of the subtrees is preorder processed. It is also called a depth-first traversal.

9 Traversing a Tree  Pre-order Traversal In this way the resulting printout has all nodes at any given level starting in the same tab column. It is relatively easy to draw lines to produce the original general tree except that the tree is on its side with it's root at the left rather than with the root at the top. Result of pre-order traversal

10 General Trees and transformation to binary trees B-tree variants: B*, B+, prefix B+ 2-4, Horizontal-vertical, Red-black trees Part 2

11 B* - introduced by D. Knuth A B-tree variant where all nodes, except the root, are required to be at least 2/3 full The implementation is more complex, since instead of combining two nodes into one on deletes, and splitting one node into two on inserts, you have to combine three nodes into two, and split two nodes into three A split operation is however delayed by an attempt to redistribute keys between neighbours (similarly to a B tree deletion) when the node becomes full. So, instead of a split, you first need to try to redistribute keys in the node, its neighbor and the key in the parent node. B-tree variants: B*

12 B+ tree is a B tree where data is stored only in the leaves, never higher up. This makes for more compact parent nodes (which now contain only keys), at the cost of some redundancy : the keys in the parents are duplicated in the leaves. Note that this changes the deletion of the keys in the parents, in that the nodes to the right of a key are now greater than or equal to that key. This complicates the implementation somewhat, as leaf nodes are now different than the other nodes. B-tree variants: B+

13 Each leaf can hold up to n – 1 values and contain at least [(n – 1) / 2] values. Nonleaf node pointers point to tree nodes (leaf nodes). Nonleaf nodes can hold up to n pointers and must hold at least [n/2] pointers. PE500 MA244 RA200 BT100 DD112 MA244 RA200 RA687 PE500 B-tree variants: B+

14 Insertion – if the new node has a search key that already exists in another leaf node, then adds the new record to the file and a pointer to the bucket of pointers. If the search key is different from all others, it is inserted in order. Deletion – remove the search key value from the node. B-tree variants: B+

15 Prefix B+ tree (Bayer, Unterauer) is a B+ tree where index uses the SHORTEST possible separators needed to distinguish two keys. Full key is not stored – thus instead of storing CD244 in the node, only C or CD or CD2 will be stored depending onother values in the tree. Prefix B+ tree

16 Bit trees introduced by Ferguson in 1999 represented keys as bits and stored only the first portion of the bit string (similarly to extendible hashing) that was required to separate 2 keys. Bit tree

17 B* (D. Knuth) is a B-tree variant where all nodes, except the root, are required to be at least 2/3 full and split operation is delayed B+ tree is a B tree where data is stored only in the leaves, and keys are duplicated in the index. Prefix B+ tree (Bayer, Unterauer) is a B+ tree where index uses the SHORTEST possible separators needed to distinguish two keys. Bit trees (Ferguson,1999) represent keys as bits and stored only the first portion of the bit string that is necessary to separate 2 keys. Summary on B-trees and variants

18 General Trees and transformation to binary trees B-tree variants: B*, B+, prefix B+ 2-4, Horizontal-vertical, Red-black trees Part 3

19 2-4 trees are B trees of order 4, i.e. with 3 keys in the node. They can be easily represented as a binary tree and have the following advantages: –Insertions are easily and faster handled –Trees are balanced –Easily transformed back and forth from B tree to binary tree 2-4 trees, horizontal-vertical, red-black

20 –Intermediate result of transformation is called the horizontal-vertical tree –Each node is a single key node that has pointers (horizontal) onto its former neighbors (keys from the same node) from the 2-4 tree or vertical onto former children 2-4 trees, horizontal-vertical, red-black 3 5 8 9 6 7 4 1 2 124679 385

21 B-tree variants: from horizontal- vertical to binary tree 124679 385 1 2 4 6 79 38 5 –The path from root to any node in horizontal-vertical tree contains the same number of vertical links

22 Split operation in 2-4 tree vs. horizontal vertical In order to perform a split (when inserting) in 2-4 tree, a new node needs to be created and two pointers adjusted, plus keys moved. In horizontal-vertical tree all that is required is to change the type of link from horizontal to vertical! This is called “bit flipping” operation and it is extremely fast!

23 Red-black tree are a variation of the BST (similar to above) that are balanced – thus the path from the root of the tree to the leaf contains the same number of black nodes Root is black Each node contains 1 bit (color) black or red If node is red, than both children are black Every red node is either –A full node (with two black children) or –A leaf node On any path from the root to a leaf, red nodes must not be adjacent. However, any number of black nodes may appear in a sequence. Red-black trees

24

25 Height is O(lgn), n –number of nodes All operations can be done in O(lgn time) Search is the same as in BST Insertion/deletion involve rotations to keep tree balanced (similarly to AVL tree). Red-black properties

26 Red-black trees offer worst-case guarantees for insertion time, deletion time, and search time. Used in time-sensitive applications such as real-time applications, and also in algorithms which provide worst-case guarantees (computational geometry) The AVL tree is another structure supporting O(log n) search, insertion, and removal. AVL tree is more rigidly balanced than Red-Black trees, leading to slower insertion and removal but faster retrieval as AVL tree is more compact. Red-black properties

27 Red-black tree worst-case Red-black properties

28 Douglas Wilhelm Harder, UWaterloo

29 2-4 trees are B trees of order 4, i.e. with 3 keys in the node. They can be easily represented as a binary tree and are balanced, with easy insertion operations Intermediate result of transformation from 2-4 tree to binary tree is called the horizontal-vertical tree Red-black tree are a variation of the BST that are balanced, the path from the root of the tree to the leaf contains the same number of black nodes. They support O(log n) search, insertion, and removal and is less rigidly balanced than AVL tree Summary on 2-4 trees and variants

30 The use of the discussed variants of the trees depends greately on the conditions of the given problem and needed features. While B trees and their variants used for indexing of databases, GIS systems, and file accesses, the 2-4, balanced and red-black trees are used instead of BST for fast information retrieval, in games, computer graphics, and computer modeling. Conclusions


Download ppt "General Trees and Variants CPSC 335. General Trees and transformation to binary trees B-tree variants: B*, B+, prefix B+ 2-4, Horizontal-vertical, Red-black."

Similar presentations


Ads by Google