Title: General Trees and Variants CPSC 335
1General Treesand VariantsCPSC 335
2Lecture Outline
- General Treesand transformation to binary trees
- B-tree variants B, B, prefix B
- 2-4, Horizontal-vertical, Red-black trees
3General 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
4Problems with General Trees and Solutions
- 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.
5Creating a Binary Tree from a General Tree
- 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. -
6Creating a Binary Tree from a General Tree
- 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.
7Creating a Binary Tree from a General Tree
General Tree Binary Tree
8Traversing 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.
9Traversing 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
10Part 2
- General Treesand transformation to binary trees
- B-tree variants B, B, prefix B
- 2-4, Horizontal-vertical, Red-black trees
11B-tree variants B
- 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.
12B-tree variants B
- 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.
13B-tree variants B
- 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
14B-tree variants B
- 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.
15Prefix B tree
- 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.
16Bit 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.
17Summary on B-trees and variants
- 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.
18Part 3
- General Treesand transformation to binary trees
- B-tree variants B, B, prefix B
- 2-4, Horizontal-vertical, Red-black trees
192-4 trees, 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
202-4 trees, 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
3 5 8
9
6 7
4
1 2
3
8
5
1
2
4
6
7
9
21B-tree variants from horizontal-vertical to
binary tree
3
8
5
1
2
4
6
7
9
- The path from root to any node in
horizontal-vertical tree contains the same number
of vertical links
5
3
8
2
4
7
9
1
6
22Split 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!
23Red-black trees
- 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.
24Red-black trees
25Red-black properties
- 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).
26Red-black properties
- 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.
27Red-black tree worst-case
Red-black properties
28Red-black properties
Douglas Wilhelm Harder, UWaterloo
29Summary on 2-4 trees and variants
- 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
30Conclusions
- 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.