chap 7 binary threaded tree

22
Chapter 7 Binary Threaded Tree

Upload: raj-sarode

Post on 12-Apr-2017

19 views

Category:

Software


0 download

TRANSCRIPT

Page 1: Chap 7 binary threaded tree

Chapter 7

Binary Threaded Tree

Page 2: Chap 7 binary threaded tree

By Prof. Raj Sarode 2

AVL Tree (Adelson, Velski, Landis)

AVL tree is a special kind tree in which the balance factor of each can not be more than 0, 1 or -1.

It also called as height balanced tree. Or we can say that height of two child sub tree of any node

differ at most by one where left and right sub trees again AVL Tree.

Balance Factor (BF) = Height of the left sub tree - Height of the Right Sub tree

Or BF = HL – HR

Page 3: Chap 7 binary threaded tree

By Prof. Raj Sarode 3

E.g.

A

B

C

D

BF = 0 - 3 = -3

BF = 0 -2 = - 2

BF = 0 - 1 = -1

BF = 0 - 0 = 0

C

A

B

D

EBF = 0 - 0 = 0

BF = 0 - 1 = -1

BF = 0 - 0 = 0

BF = 0 - 1 = -1

BF = 0 - 0 = 0

(a) Unbalance BST (b) Balance BST

Note:BF = -1 i.e. right sub tree is higher than left sub tree or it is called right heavy treeBF = 1 i.e. left sub tree is higher than right sub tree or it is called as left heavy tree.BF = 0 i.e. Both sub tree are on same height.

Page 4: Chap 7 binary threaded tree

By Prof. Raj Sarode 4

Representation of AVL Tree AVL Tree can also be represented or implemented as other tree in memory with an additional field to keep track of balance factor.The balance factor of node represents the difference of height between the left sub tree and right sub tree of the node.

Left Info Right BF

Structure of AVL Tree nodeStruct Node

{Node * Left;Int Info;Node * Right;Int BF;

};Node * root;Building AVL Tree or Height Balance Tree

AVL Tree is constructed by using the same process as applied to BST.The only thing we need to remember that it follow the property of height balance tree (BF).The balance factor of each node can be 0, 1 or -1.While the insertion of deletion is preformed, the tree become unbalanced and violate the property of AVL tree that time we have to apply different rotation on the AVL Tree

Page 5: Chap 7 binary threaded tree

By Prof. Raj Sarode 5

AVL Tree Rotation

A

B

C

BF = 0 -2 = - 2

BF = 0 - 1 = -1

BF = 0 - 0 = 0

R R

A

B

CBF = 0 - 0 = 0

BF = 1 - 1 = 0

BF = 0 - 0 = 0

A

B

C

BF = 2 - 0 = 2

BF = 1 - 0 = 1

BF = 0 - 0 = 0

L L

C

B

ABF = 0 - 0 = 0

BF = 1 - 1 = 0

BF = 0 - 0 = 0

1. Left to Left Rotation

2. Right to Right Rotation

Page 6: Chap 7 binary threaded tree

By Prof. Raj Sarode 6

AVL Tree Rotation

A

B

C

BF = 0 - 2 = - 2

BF = 1 - 0 = 1

BF = 0 - 0 = 0

R L

A

C

BBF = 0 - 0 = 0

BF = 1 - 1 = 0

BF = 0 - 0 = 0

A

B

C

BF = 2 - 0 = 2

BF = 0 - 1 = -1

BF = 0 - 0 = 0

L R

B

C

ABF = 0 - 0 = 0

BF = 1 - 1 = 0

BF = 0 - 0 = 0

3. Left to Right Rotation

4. Right to Left Rotation

Page 7: Chap 7 binary threaded tree

By Prof. Raj Sarode 7

Sr. No.

I/P Node Tree Rotation

1 Tree is balanced

2 Tree is balanced

3 Tree is balanced

23 BF = 0 - 0 = 023

1223

12

1

0

82 23

12

1

0

82

0

e.g. 23, 12, 82, 15, 16, 57, 50

Page 8: Chap 7 binary threaded tree

By Prof. Raj Sarode 8

Sr. No.

I/P Node Tree Rotation

4 Tree is balanced

5

Now Tree is balanced

15

16

23

12

1

-1

82

0

15

0

23

12

1

-2

82

0

15

-1

16

0

23

12

8215

16

R R0 0

00

1

e.g. 23, 12, 82, 15, 16, 57, 50

Page 9: Chap 7 binary threaded tree

By Prof. Raj Sarode 9

e.g. 23, 12, 82, 15, 16, 57, 50 Sr.

No.I/P Node Tree Rotation

6 Tree is balanced

7

Now Tree is balanced

57

23

12

8215

16 57

0 0 0

0 1

0

50 23

12

8215

16 57

50

0 0

0

0

1

2

-123

12 82

15

16

57

50

0 0

0

0 0

0

0

L L

Page 10: Chap 7 binary threaded tree

By Prof. Raj Sarode 10

Expression TreeAll arithmetic operation are unary or binary so expression tree are generally

binary

Left child represent the left operand.

Right child represent the right operand.

In case of unary operator a node can have only one child.

Root node always contains the operator.

Parenthesis are evaluated first then the exponent, followed by multiplication or

division & then addition or subtraction.

E.g. A + B

A

+

B

Page 11: Chap 7 binary threaded tree

By Prof. Raj Sarode 11

E.g. ( A – B * C ) / ( D + E / F )

B

*

C

Step :1 First parenthesis is evaluated so construct the tree for (A – B * C )

B

*

C

A

-B * C Then (A - B * C)

Step :2 Second parenthesis is evaluated so construct the tree for (D + E / F )

E

/

F

E

/

F

D

+E / F Then (D + E / F )

Page 12: Chap 7 binary threaded tree

By Prof. Raj Sarode 12

E.g. ( A – B * C ) / ( D + E / F )

Step :3 Result of First parenthesis is divided by second parenthesis

B

*

C

A

-

E

/

F

D

+

/

In-order: (A-B*C)/(D+E/F)Pre-order: / + D / E F – A * B CPost- order: D E F / + A B C * - /

Since expression is also binary tree so it can also be traversed in three different ways

Page 13: Chap 7 binary threaded tree

By Prof. Raj Sarode 13

B Tree

In previous section we learn BST, AVL Tree and Expression Tree but the problem is that no. of node is more then height of tree is increased also and will consume more time for different operation.So to remove such drawback we introduce M way tree or B Tree having following properties1. Each node can contain N-1 key elements, where N is the order of tree 2. Each node can have N branches.3. The root is either a leaf or it has 2 to n sub tree, where N is the order of B tree.4. All nodes expect the root and leaves can have minimum N/2 and Maximum N

Branches.5. All the leaf nodes are on the same level so tree is balanced.6. When the new node is inserted into full node (with N-1 elements), then the

node is split in to two new node at the same level and key with the median value is inserted in the parent node in case the parent node is the root, a new root will be created.

7. Full node condition is called the overflow.

Page 14: Chap 7 binary threaded tree

By Prof. Raj Sarode 14

Construct the B-Tree of order 4 for the following elements 1, 6, 8, 2, 9, 12, 15, 7, 18, 3, 4, 20

1

1 6

1 6 8

2 6 81 2

6

81

2

6

81 9

Insert 1

Insert 6

Insert 8

Insert 2

Insert 9

Page 15: Chap 7 binary threaded tree

By Prof. Raj Sarode 15

Construct the B-Tree of order 4 for the following elements 1, 6, 8, 2, 9, 12, 15, 7, 18, 3, 4, 20

2

6

81 9 12

Insert 12

2

6

81 9 12 15

Insert 15

2

6

81

9

12 15

2

6

81

9

12 15

Insert 7

7

2

6

81

9

12 157

Insert 18

18

Page 16: Chap 7 binary threaded tree

By Prof. Raj Sarode 16

Construct the B-Tree of order 4 for the following elements 1, 6, 8, 2, 9, 12, 15, 7, 18, 3, 4, 20

3

6

82

9

12 157

Insert 3

181

4

6

82

9

12 157

Insert 4

181 3

4

6

8

2 9

12 157 181 3

Page 17: Chap 7 binary threaded tree

By Prof. Raj Sarode 17

Construct the B-Tree of order 4 for the following elements 1, 6, 8, 2, 9, 12, 15, 7, 18, 3, 4, 20

Insert 20

4

6

8

2 9

12 157 181 3 20

4

6

8

2 9

12

15

7 181 3 20

4

6

8

2 9

12

15

7 181 3 20 Final B Tree

Page 18: Chap 7 binary threaded tree

By Prof. Raj Sarode 18

Construct the B-Tree of order 4 for the following elements 1, 6, 8, 2, 9, 12, 15, 7, 18, 3, 4, 20

Delete 15

4

6

8

2 9

12

15

7 181 3 20

Final B Tree

4

6

8

2 9

12

18

7 201 3

Page 19: Chap 7 binary threaded tree

By Prof. Raj Sarode 19

Construct the B-Tree of order 4 for the following elements 1, 6, 8, 2, 9, 12, 15, 7, 18, 3, 4, 20

Delete 12

Final B Tree

4

6

8

2 9

12

18

7 201 3

4

6

8

2 9 18

7 201 3

Page 20: Chap 7 binary threaded tree

By Prof. Raj Sarode 20

B+ TreeIn B Tree we can access record only in random , we cannot traverse the records sequentially.After finding particular record we can not get to get the previous or next record but B+ tree has both properties, random access as well as sequential traversal.In B+ tree the structure of leaf node & internal node is differentInternal node only store keys and child pointer.Leaf node store keys & Corresponding data items so data items present only in leaf node

Page 21: Chap 7 binary threaded tree

By Prof. Raj Sarode 21

Threaded Binary TreeBinary trees have a lot of wasted space: the leaf nodes each have 2 null pointersWe can use these pointers to help us in in-order traversalsThread: a pointer to other nodes in the tree for replacing the 0-link.We have the pointers reference the next node in an in-order traversal; called threadsWe need to know if a pointer is an actual link or a thread, so we keep a Boolean for each pointer

Page 22: Chap 7 binary threaded tree

By Prof. Raj Sarode 22

Thank You