Types of Binary Trees Expression Tree A binary tree can store anything where the number ‘2’...

81
Types of Binary Trees Expression Tree ry tree can store anything where the number ‘2’ comes into pi In an arithmetic expression: arithmetic expression can be easily represented using Binary ( A + B ) + A B Operands Operator Traversals? InOrder PreOrder PostOrder Infix Prefix Postfix A + B + A B A B + Operators normally have maximum of 2 operands.

description

Expression Tree Draw the expression tree for expression ‘(A + B)’. Infix Expression:( A + B ) Step-1 Postfix Expression: Postfix A B + 1) A A ) B A B 3) + A B 4000 (Operator)(Operand) Evaluate it for A = 5, B = 6.

Transcript of Types of Binary Trees Expression Tree A binary tree can store anything where the number ‘2’...

Page 1: Types of Binary Trees Expression Tree A binary tree can store anything where the number ‘2’ comes into picture. In an arithmetic expression: So an arithmetic.

Types of Binary TreesExpression Tree

A binary tree can store anything where the number ‘2’ comes into picture.

In an arithmetic expression:

So an arithmetic expression can be easily represented using Binary Tree.

( A + B )

+

A B

OperandsOperator

Traversals?

InOrder

PreOrder

PostOrder

Infix

Prefix

Postfix

A + B

+ A B

A B +

Operators normally have maximum of 2 operands.

Page 2: Types of Binary Trees Expression Tree A binary tree can store anything where the number ‘2’ comes into picture. In an arithmetic expression: So an arithmetic.

Types of Binary Trees•Expression Tree:

–A binary tree which stores an,•Arithmetic expression where,•Operands are stored as,

–Leaves / Leaf nodes / External nodes of an expression tree and,

•Operators are stored as,–Internal nodes of an expression tree.

–Traversals:•InOrder:

–Gives Infix notation of arithmetic expression.•PreOrder:

–Gives Prefix notation.•PostOrder:

–Gives Postfix notation of arithmetic expression.

Page 3: Types of Binary Trees Expression Tree A binary tree can store anything where the number ‘2’ comes into picture. In an arithmetic expression: So an arithmetic.

Expression TreeDraw the expression tree for expression ‘(A + B)’.

Infix Expression: ( A + B )Step-1 Postfix Expression:Postfix

A B +

A B +

1) A

A

2000

2000

2) B

A

2000

20004000

B

4000

3) +

A

20006000

B

4000

(Operator)(Operand)(Operand)

+

60005 6

11

Evaluate it for A = 5, B = 6.

Page 4: Types of Binary Trees Expression Tree A binary tree can store anything where the number ‘2’ comes into picture. In an arithmetic expression: So an arithmetic.

Expression TreeDraw the expression tree for expression ‘(A + B)’ and evaluate it for A = 5, B = 6.

Infix Expression: ( A + B )Step-1 Postfix Expression:Postfix

A B +

A B +

A A B +

A B

Evaluation?

5 6

11

1) A 2) B 3) +

Page 5: Types of Binary Trees Expression Tree A binary tree can store anything where the number ‘2’ comes into picture. In an arithmetic expression: So an arithmetic.

Expression Tree( ( A + B ) * ( C – D ) ) A B + C D – *Postfix

A B + C D – *

A

[A = 1, B = 2, C = 4, D = 2]

A B +

A B

C+

A B

C+

A B

D

+

A B

-

C D

+

A B

-

C D

*

+

A B

-

C D

*

1 2 4 2

3 2

6

=

A B + C D

- *

Page 6: Types of Binary Trees Expression Tree A binary tree can store anything where the number ‘2’ comes into picture. In an arithmetic expression: So an arithmetic.

Expression Treea * ( b + c ) / d

( ( a * ( b + c ) ) / d )

( a * ( ( b + c ) / d ) )

a b c + * d /

a b c + d / *

a b c + * d /

a a b

a b

a b

c

c a +

+

b c

*

*

a +

b c

d

*

a +

b c

d

/

*

a +

b c

d

/

Postfix

Evaluate:a = 2, b = 1, c = 1, d = 2

2

1 1

2

4 2

2

Page 7: Types of Binary Trees Expression Tree A binary tree can store anything where the number ‘2’ comes into picture. In an arithmetic expression: So an arithmetic.

Expression Tree

Draw the expression tree for following expression:

( a + b ) * c – d ^ e

Evaluate the following expression tree for A = 4, B = 15, C = 5, D = 2, E = 3

+

/ *

CB A ^

D E

Page 8: Types of Binary Trees Expression Tree A binary tree can store anything where the number ‘2’ comes into picture. In an arithmetic expression: So an arithmetic.

Types of Binary Trees

A

B C

D

E F

G

Main Disadvantage?

7 nodes8 null links

Linked Representation of a Binary Tree

Page 9: Types of Binary Trees Expression Tree A binary tree can store anything where the number ‘2’ comes into picture. In an arithmetic expression: So an arithmetic.

Types of Binary Trees•Threaded Binary Tree:

–If there are n (n>0) nodes in a binary tree,•No. of null link fields in linked representation would be:

–n+1

–More than 50% of link fields are with null values and hence,

•Lot of memory space is wasted.

–One way to use these null link fields has been given by Perlis & Thornton.

•Store pointers / links of some nodes in these null link fields.•These extra pointers / links are called:

–Threads

Page 10: Types of Binary Trees Expression Tree A binary tree can store anything where the number ‘2’ comes into picture. In an arithmetic expression: So an arithmetic.

Threaded Binary Tree

A

B C

D

E F

G

NULLNULL

Inorder traversal: B A G E D F CInorder Threading

Page 11: Types of Binary Trees Expression Tree A binary tree can store anything where the number ‘2’ comes into picture. In an arithmetic expression: So an arithmetic.

Threaded Binary Tree

A

B C

D

E F

G

Preorder traversal: A B C D E G FPreorder Threading

NULL

Page 12: Types of Binary Trees Expression Tree A binary tree can store anything where the number ‘2’ comes into picture. In an arithmetic expression: So an arithmetic.

Types of Binary Trees•Threaded Binary Tree:

–3 ways to thread a binary tree:•Inorder threading:

–Left null link points to Inorder predecessor.–Right null link points to Inorder successor.

•Preorder threading:–Left null link points to Preorder predecessor.–Right null link points to Preorder successor.

•Postorder threading:–Left null link points to Postorder predecessor.–Right null link points to Postorder successor.

Page 13: Types of Binary Trees Expression Tree A binary tree can store anything where the number ‘2’ comes into picture. In an arithmetic expression: So an arithmetic.

Types of Binary Trees•Threaded Binary Tree:

–Advantages:•Traversal operation is faster.

–Non-recursive traversal can be easily implemented and,–Non-recursive algorithm will work faster than recursive algorithm.

•Predecessor and Successor (Inorder / Preorder / Postorder) can be easily determined for any node.

Page 14: Types of Binary Trees Expression Tree A binary tree can store anything where the number ‘2’ comes into picture. In an arithmetic expression: So an arithmetic.

Types of Binary Trees•Threaded Binary Tree:

–Disadvantage:•How to distinguish / identify a link from a thread?

–How to tell which is a link and which is a thread as both will contain pointers to nodes.

•2 ways to overcome this disadvantage:–1] Node structure with 2 extra fields.

»LTAG = 1: Pointer of LC is a Thread.»LTAG = 0: Pointer of LC is a Link.

–Disadvantage:»2 extra bytes for TAG’s are required in each node.

LTAG LC DATA RC RTAG

Page 15: Types of Binary Trees Expression Tree A binary tree can store anything where the number ‘2’ comes into picture. In an arithmetic expression: So an arithmetic.

Types of Binary Trees•Threaded Binary Tree:

–Disadvantage:•How to distinguish / identify a link from a thread?

–How to tell which is a link and which is a thread as both will contain pointers to nodes.

•2 ways to overcome this disadvantage:–2] Store +ve value for Link and –ve value for Thread.

»Get the actual pointer value by taking the magnitude / mod of the pointer.

–Disadvantage:»Not possible in a programming language that does not allow –ve valued pointers.

Page 16: Types of Binary Trees Expression Tree A binary tree can store anything where the number ‘2’ comes into picture. In an arithmetic expression: So an arithmetic.

Types of Binary Trees

30

50 40

20 10

50

30 40

20 10

50

30 40

20 10

Tree T1 Tree T2 Tree T3

Heap Tree

Page 17: Types of Binary Trees Expression Tree A binary tree can store anything where the number ‘2’ comes into picture. In an arithmetic expression: So an arithmetic.

Types of Binary Trees•Heap Tree:

–Suppose H is a complete binary tree.–It will be called a heap tree if it satisfies following property:

•For each and every node in H, –Value at N is greater than or equal to the value of each of children of N.

OR•For each and every node in H,

–N has a value which is greater than or equal to the value of every successor of N.

Page 18: Types of Binary Trees Expression Tree A binary tree can store anything where the number ‘2’ comes into picture. In an arithmetic expression: So an arithmetic.

Heap Tree

50

20 40

30 10

50

30 40

20 10

50

50 40

20 10

Tree T1 Tree T2 Tree T3No No Yes

Condition not satisfiedfor node 20. Not a complete binary tree.

Which is a heap tree?

Page 19: Types of Binary Trees Expression Tree A binary tree can store anything where the number ‘2’ comes into picture. In an arithmetic expression: So an arithmetic.

Heap Tree

50

30 40

20 10

Tree T1Yes

10

30 20

50 40

Tree T2Yes

Max Heap Min Heap

Max Value Min Value

Which is a heap tree?

Page 20: Types of Binary Trees Expression Tree A binary tree can store anything where the number ‘2’ comes into picture. In an arithmetic expression: So an arithmetic.

Types of Binary Trees•Heap Tree:

–Suppose H is a complete binary tree.–It will be called a heap tree if it satisfies following property:

•For each node in H, –Value at N is greater (or smaller) than or equal to the value of each of children of N.

OR•For each node in H,

–N has a value which is greater (or smaller) than or equal to the value of every successor of N.

Page 21: Types of Binary Trees Expression Tree A binary tree can store anything where the number ‘2’ comes into picture. In an arithmetic expression: So an arithmetic.

Heap Tree•Representation of Heap Tree:

–Any binary tree can be represented in the form of:•Array (Sequential Representation)•Linked List (Linked Representation)

–Which representation is better for a Heap tree?•‘Array representation’ has certain advantages over ‘Linked representation’ in case of Heap tree.

–There will be no / less memory wastage because,»A heap tree is a complete binary tree.

–No empty space between 2 non-null entries.»If there are NULL entries, they will be at the tail of the array.

Page 22: Types of Binary Trees Expression Tree A binary tree can store anything where the number ‘2’ comes into picture. In an arithmetic expression: So an arithmetic.

Heap Tree

50

30 40

20 10

10

30 20

50 40

Max Heap Min Heap

1 2 3 4 5 6 7 Index

Value

Array representation of Heap Tree

50 30 40 20 10 - -

1 2 3 4 5 6 7

10 30 20 50 40 - -

Page 23: Types of Binary Trees Expression Tree A binary tree can store anything where the number ‘2’ comes into picture. In an arithmetic expression: So an arithmetic.

Heap Tree

95

85 45

75 25

Max Heap

Max Value

35 15

55 65

Insert value 19

19

Insertion into a Heap Tree

Page 24: Types of Binary Trees Expression Tree A binary tree can store anything where the number ‘2’ comes into picture. In an arithmetic expression: So an arithmetic.

Heap Tree

95

85 45

75 25

Max Heap

Max Value

35 15

55 65

Insert value 111

19 111

111

25

85

111

111

95

Insertion into a Heap Tree

Page 25: Types of Binary Trees Expression Tree A binary tree can store anything where the number ‘2’ comes into picture. In an arithmetic expression: So an arithmetic.

Heap Tree

95

85 45

75 25

Max Heap

Max Value

35 15

55 65

Insert value 111

19 111

Insertion into a Heap Tree

Page 26: Types of Binary Trees Expression Tree A binary tree can store anything where the number ‘2’ comes into picture. In an arithmetic expression: So an arithmetic.

Heap Tree•Insertion into a Heap Tree:

–Exercise:•Form / Build a Max Heap with the following data:

–19, 55, 44, 98, 67, 48, 95, 66, 70, 69, 30, 24, 99, 82

Page 27: Types of Binary Trees Expression Tree A binary tree can store anything where the number ‘2’ comes into picture. In an arithmetic expression: So an arithmetic.

Types of Binary Trees

99

45 63

35 29

Max Heap

Max Value

57 42

27 12 24 26

Delete a valueAny node can bedeleted, but deletingroot node has somespecial importance.

Deletion from a Heap Tree

Page 28: Types of Binary Trees Expression Tree A binary tree can store anything where the number ‘2’ comes into picture. In an arithmetic expression: So an arithmetic.

Types of Binary Trees•Deletion from a Heap Tree:

–Importance of deletion of root node:•Deleting root node continuously from max heap gives:

–Data sorted in descending order.

•Deleting root node continuously from min heap gives:

–Data sorted in ascending order.

–This sorting of data using a heap tree is called:

•Heap Sort

Page 29: Types of Binary Trees Expression Tree A binary tree can store anything where the number ‘2’ comes into picture. In an arithmetic expression: So an arithmetic.

Heap Sort•Heap Sort:

–Works on the basic concept of:•Heap Tree

–It is always a complete binary tree.–Stored in the form of array.–2 types:

»Max Heap»Min Heap

–Basic steps in a heap sort (using max heap) are:•Create heap from the array which is to be sorted.•Repeat the following steps till heap tree is empty:

–Delete root node from heap tree.–Rebuild heap after deletion.–Place the deleted node in output.

Page 30: Types of Binary Trees Expression Tree A binary tree can store anything where the number ‘2’ comes into picture. In an arithmetic expression: So an arithmetic.

1. Create HeapArray A

1 2 3 4 5

Array B1 2 3 4 5

33 33

33 14 65 02 76

1 2 3 4 5

33

33

1414

1 2 3 4 5

33

33

1414

6565

1 2 3 4 5

65

65

14 1433 3302

02

1 2 3 4 5

65 6514

14

33

33

02

02

76

76

Output

1 2 3 4 5

76 65 33 02 14Array A

Page 31: Types of Binary Trees Expression Tree A binary tree can store anything where the number ‘2’ comes into picture. In an arithmetic expression: So an arithmetic.

2. Sort the Heap

76

65 33

02 14

Heap

1 2 3 4 5

76 65 33 02 14Array A

1 2 3 4 5

Array B

Array B would not be required

Directly swap the root with the last element in the heap tree.

Page 32: Types of Binary Trees Expression Tree A binary tree can store anything where the number ‘2’ comes into picture. In an arithmetic expression: So an arithmetic.

2. Sort the Heap 76

65 33

02 14

Heap

1 2 3 4 5

76 65 33 02 14Array A

76

65 33

02 141 2 3 4 5

76 65 33 02 14Iteration-1

N

ij

65

14 33

021 2 3 4 5

65 14 33 02 76Iteration-2

ij

Page 33: Types of Binary Trees Expression Tree A binary tree can store anything where the number ‘2’ comes into picture. In an arithmetic expression: So an arithmetic.

2. Sort the Heap 76

65 33

02 14

Heap

1 2 3 4 5

76 65 33 02 14Array A

33

14 021 2 3 4 5

33 14 02 65 76Iteration-3

N

ij

14

021 2 3 4 5

14 02 33 65 76Iteration-4

ijOUTPUT

Page 34: Types of Binary Trees Expression Tree A binary tree can store anything where the number ‘2’ comes into picture. In an arithmetic expression: So an arithmetic.

Heap Sort BuildMaxHeap(A)

For i = N down to 2

Swap( A[ 1 ], A[ i ] )

For j = 1 to i – 1

lchild = 2 * j

rchild = 2 * j + 1

If( A[ j ] < A[ lchild ]) and (A[ lchild ] > A[ rchild ] )

Swap(A[ j ], A[ lchild ])

j = lchild

Else

If( A[ j ] < A[ rchild ]) and (A[ rchild ] > A[ lchild] )

Swap(A[ j ], A[ rchild ])

j = rchild

Else

Break;

EndIf

EndFor

EndFor

Create Max Heap

1st iteration:Swap 1st with last.2nd iteration:Swap 1st with second last.And so on...

Check the position of newly placed element with leftchild and rightchild.

Rebuild the heap until the newly placed element comes to proper position.

Page 35: Types of Binary Trees Expression Tree A binary tree can store anything where the number ‘2’ comes into picture. In an arithmetic expression: So an arithmetic.

Heap Sort•Algorithm:

–HeapSort•Input:

–Array A which is to be sorted.•Output:

–Array A with elements sorted in ascending order.

•Data Structure:–Array.

Page 36: Types of Binary Trees Expression Tree A binary tree can store anything where the number ‘2’ comes into picture. In an arithmetic expression: So an arithmetic.

Heap SortSteps

BuildMaxHeap(A)

For i = N down to 2

Swap( A[ 1 ], A[ i ] )

For j = 1 to i – 1

lchild = 2 * j

rchild = 2 * j + 1

If( A[ j ] < A[ lchild ]) and (A[ lchild ] > A[ rchild ] )

Swap(A[ j ], A[ lchild ])

j = lchild

Else

If( A[ j ] < A[ rchild ]) and (A[ rchild ] > A[ lchild] )

Swap(A[ j ], A[ rchild ])

j = rchild

Else

Break;

EndIf

EndFor

EndFor

Page 37: Types of Binary Trees Expression Tree A binary tree can store anything where the number ‘2’ comes into picture. In an arithmetic expression: So an arithmetic.

Types of Binary Trees

50

30 40

20 10

50

30 60

10 40 80

50

30 60

10 55 80

Binary Search Tree (BST)

Heap Tree

Tree T1 Tree T2 Tree T3

Page 38: Types of Binary Trees Expression Tree A binary tree can store anything where the number ‘2’ comes into picture. In an arithmetic expression: So an arithmetic.

Types of Binary Trees•Binary Search Tree (BST):

–A binary tree is called a Binary Search Tree if,

•Each node N satisfies following property:•The value at N is,

–Greater than every value in the left sub-tree of N and is, –Less than every value in the right sub-tree of N.

Page 39: Types of Binary Trees Expression Tree A binary tree can store anything where the number ‘2’ comes into picture. In an arithmetic expression: So an arithmetic.

Binary Search Tree (BST)

10

20 30

50 40

50

30 60

10 80

50

30

35

55

Which is a BST?

No

Heap Tree(Min Heap)

No

Not satisfied fornode with value 30.

Tree T1 Tree T2 Tree T3

Yes

Page 40: Types of Binary Trees Expression Tree A binary tree can store anything where the number ‘2’ comes into picture. In an arithmetic expression: So an arithmetic.

Binary Search Tree (BST)Which is a BST?

Yes.

Lexicographical ordering.

Jan

Dec Mar

Aug Feb Jul Nov

Jun

(Not numerical ordering)

Page 41: Types of Binary Trees Expression Tree A binary tree can store anything where the number ‘2’ comes into picture. In an arithmetic expression: So an arithmetic.

Binary Search Tree (BST)Applications of BST

65

19 74

15 8828 69

8025 57

54

1. Smallest / Minimum value? 15

2. Largest / Maximum value? 88

3. Search Value 54? Found.

4. Search Value 90? NotFound.

5. InOrder Traversal:15 19 25 28 54 57 65 69 74 80 88

Data sorted in Ascending Order.

Hence also known as:Binary Sorted Tree.

Page 42: Types of Binary Trees Expression Tree A binary tree can store anything where the number ‘2’ comes into picture. In an arithmetic expression: So an arithmetic.

Types of Binary TreesCreation of BST / Insertion into BST

20, 10, 30 20, 30, 10 10, 20, 30

10, 30, 20 30, 10, 2030, 20, 10

20

10 30

20

10 30

10

20

30

10

30

20

30

10

20

30

20

10

For a different sequence of the same set of data, there will be a different BST.Conclusion:

Page 43: Types of Binary Trees Expression Tree A binary tree can store anything where the number ‘2’ comes into picture. In an arithmetic expression: So an arithmetic.

Binary Search Tree (BST)•Applications of BST:

–To find out the minimum / maximum value from a set of data.–To search a particular data from a set of data.–To sort the data and hence a BST is also called,

•Binary Sorted Tree.–To remove duplicate values from set of data.

Page 44: Types of Binary Trees Expression Tree A binary tree can store anything where the number ‘2’ comes into picture. In an arithmetic expression: So an arithmetic.

Binary Search Tree (BST)Create a BST for data '57, 28, 25, 69, 88, 81, 74, 65, 19, 15'.

57

57

57

28

28

57

25

28

25

57

69

28

25

69

Page 45: Types of Binary Trees Expression Tree A binary tree can store anything where the number ‘2’ comes into picture. In an arithmetic expression: So an arithmetic.

Types of Binary Trees•Binary Search Tree (BST):

–Consider the following set of data:•10, 20, 30, 40, 50, 60

–No. of ways to arrange this data:•6! = 720•Some ways to arrange the data are:

–10, 20, 30, 40, 50, 60–60, 50, 40, 30, 20, 10–30, 20, 40, 10, 50, 60–20, 40, 30, 10, 50, 60

–So number of Binary Search Trees (BST) that can be constructed are:

•720.

Page 46: Types of Binary Trees Expression Tree A binary tree can store anything where the number ‘2’ comes into picture. In an arithmetic expression: So an arithmetic.

Binary Search Tree (BST)

10

10, 20, 30, 40, 50, 60

20

30

40

50

60, 50, 40, 30, 20, 10

50

40

30

20

1060

60

Page 47: Types of Binary Trees Expression Tree A binary tree can store anything where the number ‘2’ comes into picture. In an arithmetic expression: So an arithmetic.

Binary Search Tree (BST)

30

30, 20, 40, 10, 50, 60 30, 20, 50, 10, 40, 60

20 40

10 50

30

20 50

10

60

6040

Page 48: Types of Binary Trees Expression Tree A binary tree can store anything where the number ‘2’ comes into picture. In an arithmetic expression: So an arithmetic.

Binary Search Tree (BST)10

20

30

40

50

50

40

30

20

1060

60

30

20 40

10 50

30

20 50

1060 6040

1 2

34

Question:In which tree, searching operation will be faster?

Looks more balanced.

Page 49: Types of Binary Trees Expression Tree A binary tree can store anything where the number ‘2’ comes into picture. In an arithmetic expression: So an arithmetic.

Binary Search Tree (BST)

30

20 50

10 6040

When can we say that a tree is balanced or not?

Depends on the balance factor of any node.

What is this balance factor of a node?Balance Factor (bf) = Height of left sub-tree (hL) – Height of right sub-tree (hR)

Page 50: Types of Binary Trees Expression Tree A binary tree can store anything where the number ‘2’ comes into picture. In an arithmetic expression: So an arithmetic.

Types of Binary TreesHeight Balanced Tree

Calculating Balance Factor of every node.

30

20 50

10 6040

hL = 2hR = 2

2 – 2 = 0

0

hL = 1hR = 0

1 – 0 = 1

1

1 -1 = 0

0

0 – 0 = 0

0

0 – 0 = 0

0

0 – 0 = 0

0

Page 51: Types of Binary Trees Expression Tree A binary tree can store anything where the number ‘2’ comes into picture. In an arithmetic expression: So an arithmetic.

Types of Binary Trees•Height Balanced Tree:

–A binary search tree is said to be a height balanced tree if,

•All its nodes have a balance factor of 1, 0 or -1.•That is:

–| bf | = | hL – hR | <= 1 for every node in the tree.

Page 52: Types of Binary Trees Expression Tree A binary tree can store anything where the number ‘2’ comes into picture. In an arithmetic expression: So an arithmetic.

Types of Binary TreesWhich is a Height Balanced Tree?

6

2 8

1

3

74

T1

3-2 = 1

1-2= -1

0-0 = 0 1-0 = 0

1-0 = 1

0-0 = 0

0-0 = 0

Yes

6

2 8

1

3 5

4

3-1 = 2

-1

0

0

00

0

T2

NoBalance Factor not satisfied for root node (6)

Page 53: Types of Binary Trees Expression Tree A binary tree can store anything where the number ‘2’ comes into picture. In an arithmetic expression: So an arithmetic.

Types of Binary Trees•Height Balanced Tree:

–Properties:•Every Height Balanced Tree is always a Binary Search Tree (BST) whereas,

–Every Binary Search Tree (BST) might not be a Height Balanced Tree.

•Complete Binary Search Tree (BST) is always a Height Balanced Tree.

–Note:•It is also called AVL tree after the name of the scientists:

–Adelson-Velskii, Lendis.

Page 54: Types of Binary Trees Expression Tree A binary tree can store anything where the number ‘2’ comes into picture. In an arithmetic expression: So an arithmetic.

Height Balanced TreeCreation of BST / Insertion into BST

Create different BSTs for set of data 10, 20, 30.

Arrangement of 3 elements can be done in:

1) 20, 10, 30

2) 20, 30, 10

3) 10, 20, 30

4) 30, 20, 10

5) 10, 30, 20

6) 30, 10, 20

3! = 6 ways

Page 55: Types of Binary Trees Expression Tree A binary tree can store anything where the number ‘2’ comes into picture. In an arithmetic expression: So an arithmetic.

Height Balanced Tree

20, 10, 30

20

10 30

Height Balanced Tree

0

0 0

Page 56: Types of Binary Trees Expression Tree A binary tree can store anything where the number ‘2’ comes into picture. In an arithmetic expression: So an arithmetic.

Height Balanced Tree

20, 30, 10

20

10 30

Height Balanced Tree

0

0 0

Page 57: Types of Binary Trees Expression Tree A binary tree can store anything where the number ‘2’ comes into picture. In an arithmetic expression: So an arithmetic.

Height Balanced TreeLeft Rotation

10, 20, 30

10

20

30

Not Height Balanced

-2

20

10 30

Left Rotation

Height Balanced Tree

Page 58: Types of Binary Trees Expression Tree A binary tree can store anything where the number ‘2’ comes into picture. In an arithmetic expression: So an arithmetic.

Height Balanced TreeRight Rotation

30, 20, 10

30

20

10

20

10 30

Right Rotation

2

Not Height Balanced Height Balanced Tree

Page 59: Types of Binary Trees Expression Tree A binary tree can store anything where the number ‘2’ comes into picture. In an arithmetic expression: So an arithmetic.

Height Balanced Tree

10, 30, 20

10

30

20

Not Height Balanced

-2

30

10 20

Left Rotation

Balanced

Not a Binary Search Tree

Not a Height Balanced Tree

Page 60: Types of Binary Trees Expression Tree A binary tree can store anything where the number ‘2’ comes into picture. In an arithmetic expression: So an arithmetic.

Height Balanced TreeLeft-Right Rotation

10, 30, 20

10

30

20

Not Height Balanced

-2

Right Rotation

Not Height Balanced

Binary Search Tree

10

20

30

-2

Left Rotation20

10 30

HeightBalanced

Tree

Page 61: Types of Binary Trees Expression Tree A binary tree can store anything where the number ‘2’ comes into picture. In an arithmetic expression: So an arithmetic.

Height Balanced Tree

30, 10, 20

30

10

20

10

20 30

Right Rotation

Balanced

Not a Binary Search Tree

Not a Height Balanced Tree

2

Not Height Balanced

Page 62: Types of Binary Trees Expression Tree A binary tree can store anything where the number ‘2’ comes into picture. In an arithmetic expression: So an arithmetic.

Height Balanced TreeRight-Left Rotation

30, 10, 20

30

10

20

2

Left Rotation

30

20

10

20

10 30

Right Rotation

2

Not Height BalancedNot Height Balanced

Binary Search Tree

HeightBalanced

Tree

Page 63: Types of Binary Trees Expression Tree A binary tree can store anything where the number ‘2’ comes into picture. In an arithmetic expression: So an arithmetic.

Height Balanced Tree•AVL Rotations:

–4 types:•Left Rotation:

–Also known as ‘LL Rotation’.–Involves only a single Left Rotation.

•Right Rotation:–Also known as ‘RR Rotation’.–Involves only a single Right Rotation.

•Left Right Rotation:–Also known as ‘LR Rotation’.–Involves 2 rotations:

»First a Right, then a Left.•Right Left Rotation:

–Also known as ‘RL Rotation’.–Involves 2 rotations:

»First a Left, then a Right.

Page 64: Types of Binary Trees Expression Tree A binary tree can store anything where the number ‘2’ comes into picture. In an arithmetic expression: So an arithmetic.

TreeGeneral Tree & Not a Binary Tree

Mono

DickSumo Kapil Ravi

Hari Liza Azu JohnMary

Prem TomKaran Peter

Page 65: Types of Binary Trees Expression Tree A binary tree can store anything where the number ‘2’ comes into picture. In an arithmetic expression: So an arithmetic.

Tree•Representation of Tree:

–A tree can be represented in a computer using:

•Array•Linked List

Page 66: Types of Binary Trees Expression Tree A binary tree can store anything where the number ‘2’ comes into picture. In an arithmetic expression: So an arithmetic.

TreeArray representation of Tree

Mono Sumo Kapil Ravi Dick Hari Liza - - Azu - - -

1 2 3 4 5 6 7 8 9 10 11 12 13

Mono Sumo Kapil Ravi Dick Hari Liza Azu Mary John Prem Karan …..

1 2 3 4 5 6 7 8 9 10 11 12 13

Will not work

Works

Page 67: Types of Binary Trees Expression Tree A binary tree can store anything where the number ‘2’ comes into picture. In an arithmetic expression: So an arithmetic.

TreeArray representation of Tree

Mono

Sumo

Kapil

Ravi

Dick

Hari

Liza

Azu

Mary

John

Prem

Karan

Peter

Tom

DATA Array

2

6

8

9

0

0

11

0

14

0

0

0

0

0

LEFT CHILD Array

0

3

4

5

0

7

0

0

10

0

12

13

0

0

SIBLING Array

1

2

3

4

5

6

7

8

9

10

11

12

13

14

INDEX

Page 68: Types of Binary Trees Expression Tree A binary tree can store anything where the number ‘2’ comes into picture. In an arithmetic expression: So an arithmetic.

TreeLinked representation of Tree

Fixed Size Node (Version 1)

Mono

Sumo Kapil Ravi Dick

Hari Liza Azu Mary John

Prem Karan Peter Tom

Advantage:Easy Algorithm

Disadvantage:Null Links

Page 69: Types of Binary Trees Expression Tree A binary tree can store anything where the number ‘2’ comes into picture. In an arithmetic expression: So an arithmetic.

TreeLinked representation of Tree

Variable Size Node (Version 2)

Mono

4

Sumo 2 Kapil 1 Ravi 2 Dick 0

Hari 0 Liza 3 Azu 0 Mary 1 John 0

Prem 0 Peter 0Karan 0 Tom 0

Advantage:Memory Saved

Disadvantage:Complex Algorithm

Page 70: Types of Binary Trees Expression Tree A binary tree can store anything where the number ‘2’ comes into picture. In an arithmetic expression: So an arithmetic.

TreeGeneral Tree using a Linked Representation of Binary Tree

Mono

Sumo

Left Child

Sibling

Kapil Ravi Dick

Hari Liza Azu Mary John

PeterPrem Karan Tom

Page 71: Types of Binary Trees Expression Tree A binary tree can store anything where the number ‘2’ comes into picture. In an arithmetic expression: So an arithmetic.

Tree

Mono

DickSumo Kapil Ravi

Hari Liza Azu JohnMary

Prem TomKaran Peter

Binary Tree representation of Tree

Mono

Sumo Kapil Ravi Dick

Hari Liza Azu Mary John

Prem Karan Peter Tom

Page 72: Types of Binary Trees Expression Tree A binary tree can store anything where the number ‘2’ comes into picture. In an arithmetic expression: So an arithmetic.

Forest

A

DCB

E F

W

ZYX

P

SRQT1

T2

T3

Page 73: Types of Binary Trees Expression Tree A binary tree can store anything where the number ‘2’ comes into picture. In an arithmetic expression: So an arithmetic.

Forest•Forest:

–Forest is a collection of disjoint trees.•It can also be represented in the form of:

–Linked Representation of Binary Tree.

Page 74: Types of Binary Trees Expression Tree A binary tree can store anything where the number ‘2’ comes into picture. In an arithmetic expression: So an arithmetic.

Forest

A

DCB

E F

W

ZYX

P

SRQ

T1

T2

T3

Conversion of Forest to a Binary Tree

A

DCB

E F

T1’

W

ZYX

P

SRQ

T2’

T3’

Page 75: Types of Binary Trees Expression Tree A binary tree can store anything where the number ‘2’ comes into picture. In an arithmetic expression: So an arithmetic.

ForestConversion of Forest to a Binary Tree

A

DCB

E F

T1’

W

ZYX

P

SRQ

T2’

T3’

Page 76: Types of Binary Trees Expression Tree A binary tree can store anything where the number ‘2’ comes into picture. In an arithmetic expression: So an arithmetic.

Trees

30

15

40

10

45

25

10

40

05

45

15

30

25

05

F F F F F F F F F F FF F

F

F F

Binary Search Trees / 2-way Search Trees

Height Balanced Trees

Failure Nodes are not on same level. All Failure nodes are on same level.

B Tree of Order 2

B Tree

Page 77: Types of Binary Trees Expression Tree A binary tree can store anything where the number ‘2’ comes into picture. In an arithmetic expression: So an arithmetic.

B Tree

20

40

F

10

15

25

30

45

50

35 XF F F F F F F

F F

3-way Search Tree.

Not a B Tree.

All Failure nodes arenot on the same level.

Is it a B Tree?

Page 78: Types of Binary Trees Expression Tree A binary tree can store anything where the number ‘2’ comes into picture. In an arithmetic expression: So an arithmetic.

B Tree

30 X

F

20 X

10

15

40 X

F F F F F F FF F

3-way Search Tree.

YesIs it a B Tree?

25 X 3

5 X 45

50

All Failure nodes areon the same level.

B Tree of Order 3

Page 79: Types of Binary Trees Expression Tree A binary tree can store anything where the number ‘2’ comes into picture. In an arithmetic expression: So an arithmetic.

B Tree

General Node Structure of B Tree of Order m.

P1 K1 P2 K2 P3 K3 P4 …. Km-1 Pm

Where:

Ki: Key values in the node.

Pi: Pointers to sub-trees.

Page 80: Types of Binary Trees Expression Tree A binary tree can store anything where the number ‘2’ comes into picture. In an arithmetic expression: So an arithmetic.

B Tree•B Tree:

–A ‘B Tree’ of order m is an,•m-way search tree where,•All failure nodes are at the same level where,

–A failure node is,»A node which can be reached during a search only if,»The value being searched for is not in the tree.

–Applications:•Used in index creation in database.

–Note:•Every B Tree will always be a Search Tree however,

–Every Search Tree might not be a B Tree.

Page 81: Types of Binary Trees Expression Tree A binary tree can store anything where the number ‘2’ comes into picture. In an arithmetic expression: So an arithmetic.

B TreeConstruct a B Tree of order 3 for following set of data:

10, 20, 30, 40, 50, 60, 70

10 X

1010

20

20 20 X

30

10 X 3

0 X20 X

40

10 X 3

040

40 X

70

20 X 6

0 X

10 X 3

0 X 50 X 7

0 X