30
General Trees and Variants CPSC 335

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

  • View
    216

  • Download
    0

Embed Size (px)

Citation preview

Page 1: 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

General Treesand Variants

CPSC 335

Page 2: 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

• General Treesand transformation to binary trees

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

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

Lecture Outline

Page 3: 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

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

Page 4: 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

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

Page 5: 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

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

Page 6: 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

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

Page 7: 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

Creating a Binary Tree from a General Tree

General Tree Binary Tree

Page 8: 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

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.

Page 9: 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

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

Page 10: 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

• General Treesand transformation to binary trees

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

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

Part 2

Page 11: 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

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

Page 12: 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

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

Page 13: 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

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

Page 14: 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

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

Page 15: 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

• 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

Page 16: 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

• 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

Page 17: 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

• 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

Page 18: 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

• General Treesand transformation to binary trees

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

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

Part 3

Page 19: 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

• 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

Page 20: 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

– 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

1 2 4 6 7 9

3 85

Page 21: 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

B-tree variants: from horizontal-vertical to binary

tree

1 2 4 6 7 9

3 85

1

2 4

6

7 9

3 8

5

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

Page 22: 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

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!

Page 23: 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

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

Page 24: 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

Red-black trees

Page 25: 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

• 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

Page 26: 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

• 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

Page 27: 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

Red-black tree worst-case

Red-black properties

Page 28: 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

Red-black properties

Douglas Wilhelm Harder, UWaterloo

Page 29: 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

• 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

Page 30: 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

• 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