1 / 30

# General Trees and Variants CPSC 335 - PowerPoint PPT Presentation

General Trees and Variants CPSC 335. Lecture Outline. General Trees and transformation to binary trees B-tree variants: B*, B+, prefix B+ 2-4, Horizontal-vertical, Red-black trees. General Trees. Trees in which the number of subtrees of any node need not be required to be 0, 1, or 2.

I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.

## PowerPoint Slideshow about 'General Trees and Variants CPSC 335' - kamal

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.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.

- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript

### General Treesand VariantsCPSC 335

• General Treesand transformation to binary trees

• B-tree variants: B*, B+, prefix B+

• 2-4, Horizontal-vertical, Red-black trees

• 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

• 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.

• 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.

• 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.

General Tree Binary 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.

• 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

• General Treesand transformation to binary trees

• B-tree variants: B*, B+, prefix B+

• 2-4, Horizontal-vertical, Red-black trees

• 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 delayedby 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 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.

• 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

• 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.

• 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.

• 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.

• 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.

• General Treesand transformation to binary trees

• B-tree variants: B*, B+, prefix B+

• 2-4, Horizontal-vertical, Red-black trees

• 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

• 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

3 5 8

1 2

4

6 7

9

3

5

8

1

2

4

6

7

9

3

5

8

• The path from root to any node in horizontal-vertical tree contains the same number of vertical links

1

2

4

6

7

9

5

3

8

2

4

7

9

1

6

• 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!

• 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.

• 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 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

Douglas Wilhelm Harder, UWaterloo

• 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

• 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.