chapter 12 - advanced topics in trees

84
Chapter 12 Data Structures: A Programming Approach with C, PHI, Dharmender Singh Kushwaha & A.K.Misra Advanced Topics in Trees

Upload: amit-kumar-yadav

Post on 15-Apr-2016

230 views

Category:

Documents


1 download

DESCRIPTION

tree

TRANSCRIPT

Chapter 12

Data Structures: A Programming Approach with C, PHI, Dharmender Singh Kushwaha & A.K.Misra

Advanced Topics in Trees

Height-Balanced Binary Search Tree

• Proposed by Russian mathematician Adelson-Velskii

and Landis (AVL).

• It is not perfectly balanced, but pairs of sub-tree

differ in height by at most 1.

Data Structures: A Programming Approach with C, PHI, Dharmender Singh Kushwaha & A.K.Misra

differ in height by at most 1.

• Maintains an O(logn) search time and it is almost

height balanced tree.

Height-Balanced Binary Search Tree /

AVL Tree

Definition

• The sub-trees of every node differ in height by at most one.

• [h-1-(h-2)] = 1.

12

Data Structures: A Programming Approach with C, PHI, Dharmender Singh Kushwaha & A.K.Misra

Left sub tree

R-sub

tree

root

h-1

h-2h

8

1

18

5 1611

Properties

• Difference between Height of left and right subtrees

is within 1 (balancing of height).

• All the values in the left subtree are smaller than the

root value, which is smaller than the value in the

Data Structures: A Programming Approach with C, PHI, Dharmender Singh Kushwaha & A.K.Misra

root value, which is smaller than the value in the

right subtree.

• Height of the tree is limited to O(log N).

Height-Balanced Binary Search Tree

Inserting an Element

� Insert the given value into the tree as if it were an unbalanced

binary search tree

� Traverse upward one step toward the root and update the

balance factor of the nodes.

� If the balance factor becomes:

Data Structures: A Programming Approach with C, PHI, Dharmender Singh Kushwaha & A.K.Misra

� If the balance factor becomes:

� -1, 0, or 1 then the tree is still in AVL form, and no rotations are

necessary.

� 2 or -2, then the tree rooted at this node is unbalanced, and a tree

rotation is needed.

• At most a single or double rotation along with pruning will be

enough to balance the tree.

Height-Balanced Binary Search Tree

(Example)Inserting an Element

Insert 4 Insert 104

4

Data Structures: A Programming Approach with C, PHI, Dharmender Singh Kushwaha & A.K.Misra

Insert 1 Insert 2

10

4

2 10

1

4

2 10

Contd..

Insert 04

2

1

10Unbalanced subtree

Data Structures: A Programming Approach with C, PHI, Dharmender Singh Kushwaha & A.K.Misra

� Subtree under node labeled 2 is not balanced. This is the

unbalanced node and this has to be corrected.

� To balance the height of subtrees, we perform the rotation

operation.

0

Contd..

• 1 rotates up into 2’s position, 2 goes down on the opposite

side as shown here.

12

Data Structures: A Programming Approach with C, PHI, Dharmender Singh Kushwaha & A.K.Misra

20

12

0

1

Contd..

• So we have a tree

20

1

4

10

Data Structures: A Programming Approach with C, PHI, Dharmender Singh Kushwaha & A.K.Misra

• Insert -3

– Root node becomes unbalanced.

0

10

4

1

-3

2

Contd..

• Rotate the longer subtree up and push down the root node

in the opposite direction.

• ‘1’ right subtree now becomes the left subtree of the node

that rotated down and now the tree is height balanced.

0

Data Structures: A Programming Approach with C, PHI, Dharmender Singh Kushwaha & A.K.Misra

0

2-3 10

1

4

A General Tree Rotation Algorithm

� Pivot node The deepest node where the imbalance occurs.

� Rotator node The root of the tallest subtree of pivot node.

Data Structures: A Programming Approach with C, PHI, Dharmender Singh Kushwaha & A.K.Misra

Contd..The algorithm consists of six steps:

Step 1: Prune the pivot node.

Step 2: Prune the rotator node.

Step 3: Prune the rotator’s inside subtree.

� While defining the inside tree, we must understand that it depends on whether the rotator is the left or right child of

Data Structures: A Programming Approach with C, PHI, Dharmender Singh Kushwaha & A.K.Misra

depends on whether the rotator is the left or right child of the pivot node.

� If the rotator is the right child, the inside subtree is its left subtree and the outside subtree is its right subtree.

� On the other hand, if the rotator node is the left child, the inside subtree is its right subtree and the outside subtree is its left subtree.

Contd..

Step 4: Join the pruned inside subtree to the pivot node

in place where the rotator was earlier.

Step 5: Join the pivot to the rotator in the place where

inside had been.

Data Structures: A Programming Approach with C, PHI, Dharmender Singh Kushwaha & A.K.Misra

inside had been.

Step 6: Join the rotator to the pivot’s original parent in

the place where the pivot was.

• The result of the rotation is that:

– the rotator is rotated up,

– the pivot node rotates down on the other side &

the inside subtree jumps across to join the pivot.

Data Structures: A Programming Approach with C, PHI, Dharmender Singh Kushwaha & A.K.Misra

Example

• Deepest unbalanced node is 5

105

8

Pivot

Data Structures: A Programming Approach with C, PHI, Dharmender Singh Kushwaha & A.K.Misra

1

0

-3

11

2

6Rotator

Contd..

Rotation

• Step 1: Prune the pivot node.

• Step 2: Prune the rotator node.

• Step 3: Prune - 3 prunes the rotator’s inside subtree.8

Data Structures: A Programming Approach with C, PHI, Dharmender Singh Kushwaha & A.K.Misra

11

-3

2

5

1

10

6

0

Pruning

Inside subtreeOf rotator

Rotator

Contd..

• Step 4: We join the pruned inside subtree to the pivot in

the place where the rotator had been.

8

Data Structures: A Programming Approach with C, PHI, Dharmender Singh Kushwaha & A.K.Misra

5

2 61

10

11

0

-3

rotator

Contd..

• Step 5: Now join the pivot to the rotator in the place where

inside subtree had been.

1

88

Data Structures: A Programming Approach with C, PHI, Dharmender Singh Kushwaha & A.K.Misra

2

0

1

-3

5

6

11

105

2 61

10

11

0

-3

2nd Condition of Imbalance

• If the Rotator’s inside sub-tree is longer, then:

i) First rotate the root of the rotator’s inside subtree

up into the rotator’s position &

ii) ii) The rotator goes down the other side.

• Now follow the General Tree Rotation Algorithm.

Data Structures: A Programming Approach with C, PHI, Dharmender Singh Kushwaha & A.K.Misra

• Now follow the General Tree Rotation Algorithm.

Example when Inside Sub-tree is longer

• The new value is in the rotator’s inside subtree, therefore,

we must rotate this subtree before correcting the

imbalance.

105

88

Pivot

Data Structures: A Programming Approach with C, PHI, Dharmender Singh Kushwaha & A.K.Misra

1 6

10

-3

0

5

3

11

1

10

0 11

-3

5

6

3

RotateRotator

Pivot

Height-Balanced Binary Search Tree

Rotation

• Step 6: Finally, we join the rotator to the pivot’s original parent in the place where the pivot had been.

8

Data Structures: A Programming Approach with C, PHI, Dharmender Singh Kushwaha & A.K.Misra

2

10

0 11

-3 6

1

5

Node Insertion

• Place the node as is done in normal BST insert.

• If the tree is balanced even after insertion of the node, we have an AVL tree.

• Else follow the following steps:

Data Structures: A Programming Approach with C, PHI, Dharmender Singh Kushwaha & A.K.Misra

• Else follow the following steps:

– Starting at the newly inserted nodes, traverse up the tree until we find the first node whose subtrees height differs by 2 or more.

– This node is the pivot node and the root of its longer subtree is the rotator and the new value is inserted in one of the rotator’s subtree.

Contd..

– If the node is inserted in the rotator’s outside subtree, then this subtree will we longer than inside, and a single rotation of the rotator node will correct the imbalance.

– If the node is inserted in the rotator’s inside subtree, then this subtree will be longer than outside subtree.

– Now we must do the following:

i) First rotate the root of the rotator’s inside subtree up into

Data Structures: A Programming Approach with C, PHI, Dharmender Singh Kushwaha & A.K.Misra

i) First rotate the root of the rotator’s inside subtree up into the rotator’s position,

ii) The rotator goes down the other side, &

iii) Now follow the General Tree Rotation Algorithm.

Example

• Insert 3

10

8

5 10

8

5

Pivot

Data Structures: A Programming Approach with C, PHI, Dharmender Singh Kushwaha & A.K.Misra

1

0 11

-3

6

1

0 11

-3

6

3

Rotator

Height-Balanced Binary Search Tree

• The new value is in the rotator’s inside subtree, therefore,

we must rotate this subtree before correcting the

imbalance.

105

88

Pivot

Data Structures: A Programming Approach with C, PHI, Dharmender Singh Kushwaha & A.K.Misra

1 6

10

-3

0

5

3

11

1

10

0 11

-3

5

6

3

RotateRotator

Pivot

Height-Balanced Binary Search Tree

• A single rotation of the rotator node shall rectify the height

imbalance that exists in the tree.

8

101105

8After Rotation

Data Structures: A Programming Approach with C, PHI, Dharmender Singh Kushwaha & A.K.Misra

11

-3

5

6

0

3

1 6

-3

0 3

11

Deletion

• If the node is a leaf, remove it.

• If the node is not a leaf, replace it with either the largest in

its left subtree or the smallest in its right subtree.

• After deletion retrace the path back up the tree (parent of

the replacement) to the root, adjusting the balance factors

Data Structures: A Programming Approach with C, PHI, Dharmender Singh Kushwaha & A.K.Misra

the replacement) to the root, adjusting the balance factors

as needed.

M-way or M’ary Search Trees

• An M-way search tree has (M-1) values per

node and M subtrees.

• M is said to be the degree of the tree.

Data Structures: A Programming Approach with C, PHI, Dharmender Singh Kushwaha & A.K.Misra

• Example

– Binary search tree is a 2-way Search tree where

M=2.

Number of keys in Node

• It is not necessary for every node to contain exactly

(M-1) values and have exactly M subtree.

• In an M-way tree a node can have values ranging

from 1 to (M-1).

• The number of (non empty) subtree can range from

Data Structures: A Programming Approach with C, PHI, Dharmender Singh Kushwaha & A.K.Misra

• The number of (non empty) subtree can range from

0 for a leaf node to one more than the number of

values.

• Thus M is a fixed upper limit on how much data can

be stored in a node.

3-Way Search tree

• The data values in a node

are stored in ascending

order and the subtrees are

placed between adjacent

values.

8 40

224 6 50 70

Data Structures: A Programming Approach with C, PHI, Dharmender Singh Kushwaha & A.K.Misra

values.

• All the values in data D’s

left subtree are less than D

and all the values in D’s

right subtree are greater

than D.

46 62 65

M-way Tree: Application

• M is usually very large.

• Each node corresponds to a physical block on the

disk, and M represents the maximum number of data

items that can be stored in a single block of memory.

Data Structures: A Programming Approach with C, PHI, Dharmender Singh Kushwaha & A.K.Misra

items that can be stored in a single block of memory.

• M should be maximized in order to speedup the

process.

• To move from one node to another actually involves

reading a block from disk which is a slow operation

as compared to traversing a data structure stored in

memory.

M-way Search Algorithm

• If we are searching for value K and we are currently

at node consisting of values V1 to Vk, there can be

four possible cases:

– If K < V1, we recursively search for K in V1’s left subtree.

Data Structures: A Programming Approach with C, PHI, Dharmender Singh Kushwaha & A.K.Misra

– If K > Vk, we recursively search for K in Vk’s right subtree.

– If K = Vi, then we have found the desired value.

– The only remaining possibility is that, for some i, Vi < K <

V(i+1). In this case recursively search for x in the subtree

that is in between Vi and V(i+1).

B-Tree

• A B-tree is a perfectly height balanced M-way search tree.

• A node in the tree may contain many records or key values

and pointer to children.

Data Structures: A Programming Approach with C, PHI, Dharmender Singh Kushwaha & A.K.Misra

Properties

• It is perfectly height balanced and therefore every leaf

node is at the same depth.

• Every internal node, except the root, is at least half-full. i.e.

contains ceil(M/2) or more children.

• Every leaf node must contain ceil(M/2)-1 keys. where ceil(x)

Data Structures: A Programming Approach with C, PHI, Dharmender Singh Kushwaha & A.K.Misra

• Every leaf node must contain ceil(M/2)-1 keys. where ceil(x)

is the ceiling function.

• The root may have any number of value (1 to M-1) when M

is the degree of the tree.

• The height of the tree must be kept to a minimum.

Contd..

• Values or keys are arranged in a definite order within the node.

• All key values in the subtree to the left of a key value are predecessor of the key and the one of the right are successor of the key. This ensures efficient storage and retrieval.

Data Structures: A Programming Approach with C, PHI, Dharmender Singh Kushwaha & A.K.Misra

• When a new key value is to be inserted into a full node:

• the node is split into two nodes, &

• the key with the median value is inserted into the parent node.

• In case the parent node is the root, a new root is created.

B-TreeExample: 5-way B-tree

• Each node other than the root must contain a minimum of

2 or a maximum of 4 values.

10 50

Data Structures: A Programming Approach with C, PHI, Dharmender Singh Kushwaha & A.K.Misra

55 66 68 7022 443 7

B-Tree

Operations on B-trees

• Searching for a key value

• Inserting a key value

Data Structures: A Programming Approach with C, PHI, Dharmender Singh Kushwaha & A.K.Misra

• Deletion of a key value

Search Operation in a B-tree• Search 13

4 9 14

Data Structures: A Programming Approach with C, PHI, Dharmender Singh Kushwaha & A.K.Misra

1 2 3 10 11 136 7 8 16 17 20

Contd..

Step 1: We start the search from the root node. So check for the root node of the tree.

• Check the all value in current node.

• Find a value that is equal or greater than 13.

Data Structures: A Programming Approach with C, PHI, Dharmender Singh Kushwaha & A.K.Misra

Step 2:

• a) Since 13 < 14, we follow the pointer to the left of 14 and perform the above same operation on the node pointed by this pointer.

4 9 14

Contd..

10 11 13

4 9 14

Data Structures: A Programming Approach with C, PHI, Dharmender Singh Kushwaha & A.K.Misra

• Search the node pointed by left pointer of ‘14’.

• We find that value resides in this node.

10 11 13

Inserting a key value in a B-tree

Two Conditions

• We simply can’t create a new leaf node and insert a key

because the resulting tree would no longer be a B-tree.

Data Structures: A Programming Approach with C, PHI, Dharmender Singh Kushwaha & A.K.Misra

• In an existing node, there may be a condition that the

existing node already contains (M-1) keys. Then ,

– split the node around its median and the key at the median

position moves up to the immediate parent node

Insert Algorithm

• Search the M-way trees to find the leaf node to

which V should be added.

• Add V to this node in the appropriate place among

the values already there.

Data Structures: A Programming Approach with C, PHI, Dharmender Singh Kushwaha & A.K.Misra

the values already there.

• If this is a leaf node and it is not full, because there

are M-1 or fewer values in the node after adding V.

• If there are M keys in a node after adding X, we say

the node has overflowed.

Contd..

• To correct this situation, we split the node in

to three parts.

– Left part: The first (M-1) /2 values.

Data Structures: A Programming Approach with C, PHI, Dharmender Singh Kushwaha & A.K.Misra

– Middle part : (1+(M-1) /2) if M is even, else

(M-1) /2) if M is odd.

– Right part: The last (M-1)/2 values.

Contd..

• A condition may arise when there is no room in the parent?

– If it overflows we split it into 3 parts left, middle and right.

– Now make left and right into new nodes and add middle with left and right as its children to the node above.

Data Structures: A Programming Approach with C, PHI, Dharmender Singh Kushwaha & A.K.Misra

with left and right as its children to the node above.

• We continue doing this until no overflow occurs, or until the root itself overflows.

• If the root overflows, we split it, and create a new root node with Middle as its only value and left and right as its children.

Example

Inserting a value k7 in a already full node

k1 k2 k3 k4 k5 k6 k7

Data Structures: A Programming Approach with C, PHI, Dharmender Singh Kushwaha & A.K.Misra

k1 k2 k3 k4 k5 k6 k7

k4

k5 k6 k7k1 k2 k3

K4 medianMake this parent

B-Tree

Insert 33 (5 – Way Tree)

• It is inserted in the right subtree of 7.

• The node in right subtree is full hence spliting is done.

7 Split node and

Data Structures: A Programming Approach with C, PHI, Dharmender Singh Kushwaha & A.K.Misra

7

1 3 5 10 15 16 18

10 15 16 18 33

7 16

1 3 5 18 33 10 15

Split node and send 16 to its parent node

33

median

Deletion of a Key from a B-tree

• A key could be deleted from any node and not just

the leaf node.

• When a key is deleted from an internal node, this

may demand rearrangement of its children nodes.

Data Structures: A Programming Approach with C, PHI, Dharmender Singh Kushwaha & A.K.Misra

may demand rearrangement of its children nodes.

• Ensure that any non-root node contains at least (M-

1)/2 keys.

B-Tree

• Delete 5

1 3 55 60 65 9015 20 25 30 5 8

4 9 50

Data Structures: A Programming Approach with C, PHI, Dharmender Singh Kushwaha & A.K.Misra

• After deletion node underflows.

8

B-Tree

Deletion

• To make sure there are no underflow carry out following

steps:

– The under flowed node contributes whatever values are left in the

node which right now is only 8.

Data Structures: A Programming Approach with C, PHI, Dharmender Singh Kushwaha & A.K.Misra

node which right now is only 8.

– The immediate more populous neighbor contributes all its keys.

8

15 20 25 30

Contd..

• One key value from parent node whose key falls between

Under flowed node and neighbor is 9.

• We finally have 8, 9 ,15, 20, 25, 30. Median is 15.

• This median key is inserted at proper place in parent node

only if number of keys in step 4 are more than (M-1).

Data Structures: A Programming Approach with C, PHI, Dharmender Singh Kushwaha & A.K.Misra

only if number of keys in step 4 are more than (M-1).

– If the number of keys obtained > (M-1) push the median key in

parent node.

– If the number of keys obtained ≤ (M-1) don’t push the median key

in the parent node.

B-Tree

• Delete 5.

1 3 55 60 65 9015 20 25 30 5 8

4 9 50

Data Structures: A Programming Approach with C, PHI, Dharmender Singh Kushwaha & A.K.Misra

1 3 55 60 65 9020 25 30 8 9

4 15 50

Contd..

• Delete 3

1 3 55 60 65 9020 25 30 8 9

4 15 50

Data Structures: A Programming Approach with C, PHI, Dharmender Singh Kushwaha & A.K.Misra

• After deleting 3, the node underflows.

• Applying the step 4 of algorithm we have values 1 4 8 9.

• Number of keys are not more than M-1 i.e. (5-1) =4 .

• No need to calculate the median, Simply merge these

values in a single node.

Contd..

• Delete 3

1 3 55 60 65 9020 25 30 8 9

4 15 50

Data Structures: A Programming Approach with C, PHI, Dharmender Singh Kushwaha & A.K.Misra

1 4 8 9 55 60 65 90 20 25 30

15 50

Deletion of a value from Root node

• For the root to underflow, it should originally contain just

one value.

• This key is the one which now has to be deleted.

• If the root was also a leaf, there is no problem.

• If the root is not a leaf, it must originally be having two

Data Structures: A Programming Approach with C, PHI, Dharmender Singh Kushwaha & A.K.Misra

• If the root is not a leaf, it must originally be having two

subtree because it initially contained one value.

• If root is not a leaf then its child are combined to form a

new node and this becomes the root now.

Example

• Delete 6

18 20

10

3 6

Data Structures: A Programming Approach with C, PHI, Dharmender Singh Kushwaha & A.K.Misra

3 10 18 20

After deletion

B-Trees: Application

• B-tree are often used to index the data and to

provide fast access.

• Searching an un-indexed and unsorted database

containing n key values will have a worst case

Data Structures: A Programming Approach with C, PHI, Dharmender Singh Kushwaha & A.K.Misra

containing n key values will have a worst case

running time of O(n) and if the same data is indexed

with a B-tree, the same search operation will run in

O(log n).

• B-tree also optimizes costly disk accesses that are of

concern when dealing with large data bases.

B+ Tree

• A B+ tree has two types of nodes - index and data.

• Index nodes are the internal nodes and data nodes are the

external or leaf nodes.

• Data nodes store the data with their corresponding keys on

the other hand index nodes holds the keys.

Data Structures: A Programming Approach with C, PHI, Dharmender Singh Kushwaha & A.K.Misra

the other hand index nodes holds the keys.

• Leaf nodes also contain an additional pointer, called the

sibling pointer.

• All the leaf nodes are connected to each other via the

sibling pointer.

Characteristics

• An order size of M means that an internal node can contain

M-1 keys and M pointers.

• It is in the form of a balanced tree in which every path from

the root of the tree to leaf is the same length.

• Each non leaf node in the tree has between [M/2] and [M-1]

Data Structures: A Programming Approach with C, PHI, Dharmender Singh Kushwaha & A.K.Misra

• Each non leaf node in the tree has between [M/2] and [M-1]

children where M is the degree of the tree.

• Although B+ tree is good for searches, it has overhead

concerning issues in wasted space.

Contd..

• An internal node in a B+ tree consists of a set of key values

and pointers.

• Each pointer points to nodes containing values that are less

than or equal to the values of the key immediately to its

right.

Data Structures: A Programming Approach with C, PHI, Dharmender Singh Kushwaha & A.K.Misra

right.

• The last pointer in an internal node is called the max

pointer.

• The max pointer points to a node containing key values that

are greater than the last key value in the node.

B+ Tree

Leaf Node

• A leaf node in a B+ tree consists of a set of key values and

data pointers.

• The data pointer points to a record or block in the database

identified by the key value.

Data Structures: A Programming Approach with C, PHI, Dharmender Singh Kushwaha & A.K.Misra

identified by the key value.

• Searching a leaf node for a key value begins at the leftmost

value and moves rightwards until a matching key is found.

B+ Tree

• Example

8

Data Structures: A Programming Approach with C, PHI, Dharmender Singh Kushwaha & A.K.Misra

|

|

\

6

||

|

10 11

4 6 1513111098 |

Basic Search Algorithm

search (record R1)

rootptr = root

while (rootptr is not a leaf) do

choose the correct pointer in the node

move to the first node by following the pointer

Data Structures: A Programming Approach with C, PHI, Dharmender Singh Kushwaha & A.K.Misra

move to the first node by following the pointer

rootptr = current node

scan rootptr for R1

Inserting a node in a B+ Tree

• Insert 8,11 in a tree of order 3.

• Maximum number of elements in a node is 2.

• Insert 4, this node overflows. Split this node.

8 11

Data Structures: A Programming Approach with C, PHI, Dharmender Singh Kushwaha & A.K.Misra

8 114

Split this node

4 8 111

Contd..

• A new node is created and key values are arranged so that

8 is in the root node.

8

Data Structures: A Programming Approach with C, PHI, Dharmender Singh Kushwaha & A.K.Misra

1184

8 114

Contd..

• Example Insert 15 in following

86

Data Structures: A Programming Approach with C, PHI, Dharmender Singh Kushwaha & A.K.Misra

• 15 is placed in rightmost node. But it overflows and need to

be split.

1110864

Contd..

• Split the node and make an index node with index 11.

86 11

Data Structures: A Programming Approach with C, PHI, Dharmender Singh Kushwaha & A.K.Misra

• Now the tree requires a root node hence we promote the key 8 as the root of the tree.

11 158 104 6

Contd..

• Promote 8 as a root.

8

Data Structures: A Programming Approach with C, PHI, Dharmender Singh Kushwaha & A.K.Misra

6

1164 8

11

10 15

Deleting a key from a B+ Tree

• Delete value 13

106

8

11

Data Structures: A Programming Approach with C, PHI, Dharmender Singh Kushwaha & A.K.Misra

• Search for the location of key value 13.

• Go through the pointers to reach the rightmost leaf node.

10

4

6

6 8 109 11

11

1513

Contd..

• After deleting the 13, the node is still not less than half full and hence the node doesn’t underflow.

8

Data Structures: A Programming Approach with C, PHI, Dharmender Singh Kushwaha & A.K.Misra

• Now delete 11.

6 1110

4 6 8 109 11 15

Contd..

• Deleting 11 causes the node to underflow.

8

6 1110

Data Structures: A Programming Approach with C, PHI, Dharmender Singh Kushwaha & A.K.Misra

6 1110

4 6 8 109 156

B+ Tree

• Two possible ways to correct this situation:– We can remove the empty node from the tree. This shall require no

further action. OR

– We see that left sibling of empty node is full hence we can place half of the key values in the empty node.

Data Structures: A Programming Approach with C, PHI, Dharmender Singh Kushwaha & A.K.Misra

6

8

9 11

64 98 1510

Threaded Binary Tree

� A binary tree is said to be a threaded binary tree if:

– all the right child pointers that would normally be null point to the

inorder successor of the node, and

– all left child pointers that would also normally be null point to the

inorder predecessor of the node.

Data Structures: A Programming Approach with C, PHI, Dharmender Singh Kushwaha & A.K.Misra

� A threaded binary tree makes it possible to traverse

the values in the binary tree via a linear traversal that

is more rapid than a recursive in-order traversal.

Use of B+ tree

• In this binary tree there are 8

null pointers numbered 7 to

14. The actual number of

pointers is just 6 numbered 1

to 6.

A

B C

1

4 53 6

2

Data Structures: A Programming Approach with C, PHI, Dharmender Singh Kushwaha & A.K.Misra

to 6.

• lot of memory wastage in

storing these null pointers.

• Null pointers are replaced by

appropriate pointer values

called threads.

D E F G

1110987 1312 14

Contd..

Number of nodes=7.

Numbers of useful pointers =6

Number of null pointer is 8.

In general form for n Nodes,

A

B C

1

4 53 6

2

Data Structures: A Programming Approach with C, PHI, Dharmender Singh Kushwaha & A.K.Misra

In general form for n Nodes,

Number of non-null links: n -1

Total number of links : 2n

Number of null links :

2n - (n - 1) = n+1Note: Hence we can replace these null pointers with some useful “threads”.

D E F G

1110987 1312

Conversion of Threaded binary tree

from General Binary Tree

• If ptr->left_child is null

{

replace it with a pointer to the node that would be

visited before ptr in an inorder traversal

}

Data Structures: A Programming Approach with C, PHI, Dharmender Singh Kushwaha & A.K.Misra

}

• If ptr->right_child is null

{

replace it with a pointer to the node that would be

visited after ptr in an inorder traversal

}

Example

A

B C

Data Structures: A Programming Approach with C, PHI, Dharmender Singh Kushwaha & A.K.Misra

D E

The inorder traversal for the above tree is

D, B, A, E, C

Node structure of a Threaded Binary Tree

struct NODE

{

struct NODE *leftchild;

struct NODE*leftthread;

int node_value; TRUE ------ FALSE

Left_ChildLeft_Thread data Right_Child Right_Thread

Data Structures: A Programming Approach with C, PHI, Dharmender Singh Kushwaha & A.K.Misra

int node_value;

struct NODE *rightchild;

struct NODE *rightthread;

}

Note : If the value of left or right thread is true, it indicates that it is a thread and a false value means it is a normal pointer.

TRUE ------ FALSE

.

A TBT

Data Structures: A Programming Approach with C, PHI, Dharmender Singh Kushwaha & A.K.Misra

Memory Representation of A CompleteTBT

Data Structures: A Programming Approach with C, PHI, Dharmender Singh Kushwaha & A.K.Misra

Inserting Nodes into TBT

• Set parent->right_thread to FALSE

• Set child->left_thread and child->right_thread to

TRUE

• Set child->left_child to point to parent

Data Structures: A Programming Approach with C, PHI, Dharmender Singh Kushwaha & A.K.Misra

• Set child->left_child to point to parent

• Set child->right_child to parent->right_child

• Set parent->right_child to point to child

Contd..

header

A

B

f Header f

t A f

f B f

Data Structures: A Programming Approach with C, PHI, Dharmender Singh Kushwaha & A.K.Misra

C Dheader

A

B

C D

t D tt C t

f B f

memory representation of the tree

Algorithm for traversing a threaded

binary tree

Th_bin_tree(T)

{

header = T;

while(1) /while true

{

Data Structures: A Programming Approach with C, PHI, Dharmender Singh Kushwaha & A.K.Misra

{

T = Inorder_sucr(T);

If (T= =header)

Exit (0);

else

Printf(“%c”, T--> data);

}

}

Conclusion

• Self-balancing binary search tree is called AVL Tree.

• The height balanced binary also termed as AVL tree is the

first dynamically balanced tree proposed and it is not

perfectly balanced, but pairs of sub-tree differ in height

by at most 1.

Data Structures: A Programming Approach with C, PHI, Dharmender Singh Kushwaha & A.K.Misra

by at most 1.

• M-way search tree, which has (M-1) values per node and

M subtrees. Here M is said to be the degree of the tree..

• The organization of a M-way tree is based on the binary

tree hence, the algorithm for searching a value in an M-

way search tree is generalization of the algorithm for

search operation in a binary search tree.

Conclusion

• B-tree is a type of tree that is used to store and

access data.

• Databases and File systems are the major

implementations of B-tree.

Data Structures: A Programming Approach with C, PHI, Dharmender Singh Kushwaha & A.K.Misra

implementations of B-tree.

• B-trees do not need re-balancing as frequently as

other self-balancing search trees like AVL Tree.