+ All Categories
Home > Documents > 1 TK1924 Program Design & Problem Solving Session 2011/2012 L8: Binary Trees.

1 TK1924 Program Design & Problem Solving Session 2011/2012 L8: Binary Trees.

Date post: 12-Jan-2016
Category:
Upload: ilene-nichols
View: 223 times
Download: 2 times
Share this document with a friend
96
1 TK1924 Program Design & Problem Solving Session 2011/2012 L8: Binary Trees
Transcript
Page 1: 1 TK1924 Program Design & Problem Solving Session 2011/2012 L8: Binary Trees.

1

TK1924 Program Design & Problem Solving

Session 2011/2012

L8: Binary Trees

Page 2: 1 TK1924 Program Design & Problem Solving Session 2011/2012 L8: Binary Trees.

2

Objectives

• Learn about binary trees

• Explore various binary tree traversal algorithms

• Learn how to organize data in a binary search tree

• Discover how to insert and delete items in a binary search tree

Page 3: 1 TK1924 Program Design & Problem Solving Session 2011/2012 L8: Binary Trees.

3

What is a tree?

Root

Leaf

Page 4: 1 TK1924 Program Design & Problem Solving Session 2011/2012 L8: Binary Trees.

4

What is a tree?

Root

Leaf

Page 5: 1 TK1924 Program Design & Problem Solving Session 2011/2012 L8: Binary Trees.

5

What is a tree?

Root

Leaf

Page 6: 1 TK1924 Program Design & Problem Solving Session 2011/2012 L8: Binary Trees.

6

What is a tree?

Root

Leaf

Page 7: 1 TK1924 Program Design & Problem Solving Session 2011/2012 L8: Binary Trees.

7

Root

Node

What is a tree?

Page 8: 1 TK1924 Program Design & Problem Solving Session 2011/2012 L8: Binary Trees.

8

Family Tree Terms

Root

1

2 3

6

4

7

109

8

1211

13

5

Page 9: 1 TK1924 Program Design & Problem Solving Session 2011/2012 L8: Binary Trees.

9

1

2 3

6

4

7

109

8

1211

13

5

1: Root

Page 10: 1 TK1924 Program Design & Problem Solving Session 2011/2012 L8: Binary Trees.

10

1

2 3

6

4

7

109

8

1211

13

5

1: Root5, 9, 10, 12, 13: Leaf (no children)

Page 11: 1 TK1924 Program Design & Problem Solving Session 2011/2012 L8: Binary Trees.

11

1

2 3

6

4

7

109

8

1211

13

5

1: Root5, 9, 10, 12, 13: Leaf (no children)7 : child of 4 (accessed directly)

Page 12: 1 TK1924 Program Design & Problem Solving Session 2011/2012 L8: Binary Trees.

12

1

2 3

6

4

7

109

8

1211

13

5

1: Root5, 9, 10, 12, 13: Leaf (no children)7 : child of 4 (accessed directly)4 : parent of 7, parent of 8 (accessing node)

Page 13: 1 TK1924 Program Design & Problem Solving Session 2011/2012 L8: Binary Trees.

13

1

2 3

6

4

7

109

8

1211

13

5

1: Root5, 9, 10, 12, 13: Leaf (no children)7 : child of 4 (accessed directly)4 : parent of 7, parent of 8 (accessing node)7 and 8: siblings (same parent)

Page 14: 1 TK1924 Program Design & Problem Solving Session 2011/2012 L8: Binary Trees.

14

1

2 3

6

4

7

109

8

1211

13

5

1: Root5, 9, 10, 12, 13: Leaf (no children)7 : child of 4 (accessed directly)4 : parent of 7, parent of 8 (accessing node)7 and 8: siblings (same parent) : stem/branch

Page 15: 1 TK1924 Program Design & Problem Solving Session 2011/2012 L8: Binary Trees.

15

Binary Trees

1

2 3

6

4

7

109

8

1211

13

5

Condition: each node must not have More than 2 children

Page 16: 1 TK1924 Program Design & Problem Solving Session 2011/2012 L8: Binary Trees.

16

Binary Trees

Condition: Each node must not have more than 2 children

1

2

5

3

6

8

7

109

11

4

1

2

3

4

5

Page 17: 1 TK1924 Program Design & Problem Solving Session 2011/2012 L8: Binary Trees.

17

Binary Trees

• Definition: A binary tree, T, is either empty or such that:– T has a special node called the root node;– T has two sets of nodes, LT and RT, called the

left subtree and right subtree of T, respectively; – LT and RT are binary trees

Page 18: 1 TK1924 Program Design & Problem Solving Session 2011/2012 L8: Binary Trees.

18

Binary Tree

1

2

5

3

6

8

7

109

11

4

1

2

3

4

5

Page 19: 1 TK1924 Program Design & Problem Solving Session 2011/2012 L8: Binary Trees.

19

Binary Tree With One Node

The root node of the binary tree = A

LA = empty

RA = empty

A

Page 20: 1 TK1924 Program Design & Problem Solving Session 2011/2012 L8: Binary Trees.

20

Binary Trees With Two Nodes

A

B

Binary tree with two nodes; the right sub-tree of the rood node is empty.

A

C

Binary tree with two nodes; the left sub-tree of the rood node is empty.

Page 21: 1 TK1924 Program Design & Problem Solving Session 2011/2012 L8: Binary Trees.

21

Various Binary Trees With Three Nodes

A

B

D

A

B

E

A

C

G

A

C

F

(i) (ii) (iii) (iv)

Page 22: 1 TK1924 Program Design & Problem Solving Session 2011/2012 L8: Binary Trees.

22

Binary Trees

Following struct defines the node of a binary tree:

template<class elemType>

struct nodeType

{

elemType info;

nodeType<elemType> *llink;

nodeType<elemType> *rlink;

};

Page 23: 1 TK1924 Program Design & Problem Solving Session 2011/2012 L8: Binary Trees.

23

Nodes

• For each node:– Data is stored in info– The pointer to the left child is stored in llink– The pointer to the right child is stored in rlink

Page 24: 1 TK1924 Program Design & Problem Solving Session 2011/2012 L8: Binary Trees.

24

General Binary Tree

Page 25: 1 TK1924 Program Design & Problem Solving Session 2011/2012 L8: Binary Trees.

25

Binary Tree Definitions

• Leaf: node that has no left and right children• Parent: node with at least one child node• Level of a node: number of branches on the path

from root to node• Height of a binary tree: number of nodes on the

longest path from root to node• Width of a binary tree: the maximum number of

elements on one level of the tree

Page 26: 1 TK1924 Program Design & Problem Solving Session 2011/2012 L8: Binary Trees.

26

Binary Trees

1

2

5

3

6

8

7

109

11

4

Height of tree = 5Level

1

2

3

4

5

Width of tree = 4

Page 27: 1 TK1924 Program Design & Problem Solving Session 2011/2012 L8: Binary Trees.

27

Height of a Binary Tree

Recursive algorithm to find height of binary tree:

if(p is NULL)

height(p) = 0

else

height(p) = 1 + max(height(p->llink), height(p->rlink))

(height(p) denotes height of binary tree with root p):

Page 28: 1 TK1924 Program Design & Problem Solving Session 2011/2012 L8: Binary Trees.

28

Height of a Binary Tree

Function to implement above algorithm:

template<class elemType>int height(nodeType<elemType> *p){ if(p == NULL) return 0; else return 1 + max(height(p->llink),

height(p->rlink));}

Page 29: 1 TK1924 Program Design & Problem Solving Session 2011/2012 L8: Binary Trees.

29

Binary Tree Traversal

• Must start with the root, then– Visit the node first or– Visit the subtrees first

• Three different traversals– Inorder– Preorder– Postorder

Page 30: 1 TK1924 Program Design & Problem Solving Session 2011/2012 L8: Binary Trees.

30

Traversals

• Inorder – Traverse the left subtree– Visit the node– Traverse the right subtree

• Preorder– Visit the node– Traverse the left subtree– Traverse the right subtree

• Postorder– Traverse the left subtree

– Traverse the right subtree

– Visit the node

Page 31: 1 TK1924 Program Design & Problem Solving Session 2011/2012 L8: Binary Trees.

31

The order of traversal being discussed is as follows:• N : visit node• L : Traverse left subtree• R : Traverse right subtree

Traverse BST

Page 32: 1 TK1924 Program Design & Problem Solving Session 2011/2012 L8: Binary Trees.

32

Traverse BST

64

10

33

88

7 99

If NLR (Preorder): ??? If LNR (Inorder): ??? If LRN (Postorder): ???

64 10 7 33 88 997 10 33 64 88 99

7 33 10 99 88 64

Page 33: 1 TK1924 Program Design & Problem Solving Session 2011/2012 L8: Binary Trees.

33

Binary Tree: Inorder Traversal

template<class elemType>

void inorder(nodeType<elemType> *p)

{

if(p != NULL)

{

inorder(p->llink);

cout<<p->info<<” “;

inorder(p->rlink);

}

}

Page 34: 1 TK1924 Program Design & Problem Solving Session 2011/2012 L8: Binary Trees.

34

Binary Tree: preorder Traversal

template<class elemType>

void preorder(nodeType<elemType> *p)

{

if(p != NULL)

{

cout<<p->info<<” “;

preorder(p->llink);

preorder(p->rlink);

}

}

Page 35: 1 TK1924 Program Design & Problem Solving Session 2011/2012 L8: Binary Trees.

35

Binary Tree: postorder Traversals

template<class elemType>

void postorder(nodeType<elemType> *p)

{

if(p != NULL)

{

postorder(p->llink);

postorder(p->rlink);

cout<<p->info<<” “;

}

}1

Page 36: 1 TK1924 Program Design & Problem Solving Session 2011/2012 L8: Binary Trees.

36

Implementing Binary Trees: class binaryTreeType Functions

• Public– isEmpty

– inorderTraversal

– preorderTraversal

– postorderTraversal

– treeHeight

– treeNodeCount

– treeLeavesCount

– destroyTree

• Private• copyTree

• Destroy

• Inorder, preorder, postorder

• Height

• Max

• nodeCount

• leavesCount

Page 37: 1 TK1924 Program Design & Problem Solving Session 2011/2012 L8: Binary Trees.

37

Binary Search Trees

• Data in each node– Larger than the data in its left child– Smaller than the data in its right child

• A binary search tree,t, is either empty or:– T has a special node called the root node– T has two sets of nodes, LT and RT, called the left

subtree and right subtree of T, respectively– Key in root node larger than every key in left subtree

and smaller than every key in right subtree– LT and RT are binary search trees

Page 38: 1 TK1924 Program Design & Problem Solving Session 2011/2012 L8: Binary Trees.

38

Binary Search Trees

64

10

7 33

88

99 64

10

33

88

997

Page 39: 1 TK1924 Program Design & Problem Solving Session 2011/2012 L8: Binary Trees.

39

Operations Performed on Binary Search Trees

• Determine whether the binary search tree is empty

• Search the binary search tree for a particular item

• Insert an item in the binary search tree

• Delete an item from the binary search tree

Page 40: 1 TK1924 Program Design & Problem Solving Session 2011/2012 L8: Binary Trees.

40

Search BST

64

10

33

88

7 99

Find 33

Page 41: 1 TK1924 Program Design & Problem Solving Session 2011/2012 L8: Binary Trees.

41

Search BST

64

10

33

88

7 99

Find 33

Page 42: 1 TK1924 Program Design & Problem Solving Session 2011/2012 L8: Binary Trees.

42

Search BST

64

10

33

88

7 99

Find 3333 = 64?

Page 43: 1 TK1924 Program Design & Problem Solving Session 2011/2012 L8: Binary Trees.

43

Search BST

64

10

33

88

7 99

Find 3333 < 64?

Page 44: 1 TK1924 Program Design & Problem Solving Session 2011/2012 L8: Binary Trees.

44

Search BST

64

10

33

88

7 99

Find 33

Page 45: 1 TK1924 Program Design & Problem Solving Session 2011/2012 L8: Binary Trees.

45

Search BST

64

10

33

88

7 99

Find 33

33 = 10?

Page 46: 1 TK1924 Program Design & Problem Solving Session 2011/2012 L8: Binary Trees.

46

Search BST

64

10

33

88

7 99

Find 33

33 < 10?

Page 47: 1 TK1924 Program Design & Problem Solving Session 2011/2012 L8: Binary Trees.

47

Search BST

64

10

33

88

7 99

Find 33

33 = 33?

Page 48: 1 TK1924 Program Design & Problem Solving Session 2011/2012 L8: Binary Trees.

48

Search BST

64

10

33

88

7 99

Find 33

Page 49: 1 TK1924 Program Design & Problem Solving Session 2011/2012 L8: Binary Trees.

49

Search BST

64

10

33

88

7 99

Find 6

Page 50: 1 TK1924 Program Design & Problem Solving Session 2011/2012 L8: Binary Trees.

50

Search BST

64

10

33

88

7 99

Find 66 = 64?

Page 51: 1 TK1924 Program Design & Problem Solving Session 2011/2012 L8: Binary Trees.

51

Search BST

64

10

33

88

7 99

Find 66 < 64?

Page 52: 1 TK1924 Program Design & Problem Solving Session 2011/2012 L8: Binary Trees.

52

Search BST

64

10

33

88

7 99

Find 6

6 = 10?

Page 53: 1 TK1924 Program Design & Problem Solving Session 2011/2012 L8: Binary Trees.

53

Search BST

64

10

33

88

7 99

Find 6

6 < 10?

Page 54: 1 TK1924 Program Design & Problem Solving Session 2011/2012 L8: Binary Trees.

54

Search BST

64

10

33

88

7 99

Find 6

6 = 7?

Page 55: 1 TK1924 Program Design & Problem Solving Session 2011/2012 L8: Binary Trees.

55

Search BST

64

10

33

88

7 99

Find 6

6 < 7?

Page 56: 1 TK1924 Program Design & Problem Solving Session 2011/2012 L8: Binary Trees.

56

Search BST

64

10

33

88

7 99

Find 6

NULL

Page 57: 1 TK1924 Program Design & Problem Solving Session 2011/2012 L8: Binary Trees.

57

Insert Node

5 8 9 13 21 44 45 46

5 8 9 13 14 21 44 45 46

WHAT ARE THE FEATURES OF A SORTED LIST????

FEATURES OF A SORTED LIST IS PRESERVED

Given a sorted list:

If 14 is inserted, the list become:

Page 58: 1 TK1924 Program Design & Problem Solving Session 2011/2012 L8: Binary Trees.

58

Insert Node

64

10

33

88

7 99

How does the new BST look like ? How do you do it?

Given a BST:

If 14 to be inserted :

Page 59: 1 TK1924 Program Design & Problem Solving Session 2011/2012 L8: Binary Trees.

59

Insert Node

64

10

33

88

7 99

14

Are the features of a BST preserved???

Given a BST:

Page 60: 1 TK1924 Program Design & Problem Solving Session 2011/2012 L8: Binary Trees.

60

Insert Node

64

10

33

88

7 99

14 < 64?

Algorithm to insert 14:

Page 61: 1 TK1924 Program Design & Problem Solving Session 2011/2012 L8: Binary Trees.

61

Insert Node

64

10

33

88

7 99

14 < 10?

Algorithm to insert 14:

Page 62: 1 TK1924 Program Design & Problem Solving Session 2011/2012 L8: Binary Trees.

62

Insert Node

64

10

33

88

7 99

14 < 33?

Algorithm to insert 14:

Page 63: 1 TK1924 Program Design & Problem Solving Session 2011/2012 L8: Binary Trees.

63

Insert Node

64

10

33

88

7 99

NULL

Algorithm to insert 14:

Page 64: 1 TK1924 Program Design & Problem Solving Session 2011/2012 L8: Binary Trees.

64

Insert Node

64

10

33

88

7 99

Insert here

14

Algorithm to insert 14:

Page 65: 1 TK1924 Program Design & Problem Solving Session 2011/2012 L8: Binary Trees.

65

Insert Node

64

10

33

88

7 99

14

Algorithm to insert 14:

Page 66: 1 TK1924 Program Design & Problem Solving Session 2011/2012 L8: Binary Trees.

66

Insert Node

Try insert 99:

64

10

33

88

7 99

14

Page 67: 1 TK1924 Program Design & Problem Solving Session 2011/2012 L8: Binary Trees.

67

Insert Node

64

10

33

88

7 99

14

99 < 64?

Try insert 99:

Page 68: 1 TK1924 Program Design & Problem Solving Session 2011/2012 L8: Binary Trees.

68

Insert Node

64

10

33

88

7 99

14

99 < 88?

Try insert 99:

Page 69: 1 TK1924 Program Design & Problem Solving Session 2011/2012 L8: Binary Trees.

69

Insert Node

64

10

33

88

7 99

1499 < 99?

Try insert 99:

Page 70: 1 TK1924 Program Design & Problem Solving Session 2011/2012 L8: Binary Trees.

70

Insert Node

This insert algorithm is not very effective

Try building a BST using the same algorithm for the following sequence:

K N G I C U

K U C I N G

C G I K N U

It is difficult to produce a balanced Tree one way is using the AVL Tree algorithm

Page 71: 1 TK1924 Program Design & Problem Solving Session 2011/2012 L8: Binary Trees.

71

4 cases node deletion :

• The node to be deleted is a leafCase 1: The node to be deleted has no left and right subtrees

• The node to be deleted has 1 childCase 2: The node to be deleted has no left subtree

Case 3: The node to be deleted has no right subtree

• The node to be deleted has 2 childrenCase 4: The node to be deleted has nonempty left and right

subtrees

Delete Node

Page 72: 1 TK1924 Program Design & Problem Solving Session 2011/2012 L8: Binary Trees.

72

M

E

D

P

B VN

T ZA

To delete D:

Delete Node: First Case

Case 1: The node to be deleted has no left and right subtrees

parent

Page 73: 1 TK1924 Program Design & Problem Solving Session 2011/2012 L8: Binary Trees.

73

M

E

D

P

B VN

T ZA

To delete D

parent

Set the right child of parent as null

x

Delete Node: First Case

Page 74: 1 TK1924 Program Design & Problem Solving Session 2011/2012 L8: Binary Trees.

74

M

E

D

P

B VN

T ZA

To delete D

parent

x

Delete Node: First Case

Set the right child of parent as null

Page 75: 1 TK1924 Program Design & Problem Solving Session 2011/2012 L8: Binary Trees.

Delete Node: First Case

75

To delete D

Delete x

M

E

D

P

B VN

T ZA

parent

x

Set the right child of parent as null

Page 76: 1 TK1924 Program Design & Problem Solving Session 2011/2012 L8: Binary Trees.

76

M

E P

B VN

T ZA

To delete D

parent

Delete Node: First Case

Page 77: 1 TK1924 Program Design & Problem Solving Session 2011/2012 L8: Binary Trees.

77

To delete E

Delete Node: Second Case

Case 3: The node to be deleted has no right subtree

M

E P

B VN

T ZA D

Page 78: 1 TK1924 Program Design & Problem Solving Session 2011/2012 L8: Binary Trees.

78

Delete Node: Second Case

M

E P

B VN

T ZA

To delete E

D

x

parent

Page 79: 1 TK1924 Program Design & Problem Solving Session 2011/2012 L8: Binary Trees.

79

Delete Node: Second Case

M

E P

B VN

T ZA

To delete E

D

x

parent

Set the Lchild (@ Rchild) of x as the Lchild (@ Rchild) of parent

Page 80: 1 TK1924 Program Design & Problem Solving Session 2011/2012 L8: Binary Trees.

80

Delete Node: Second Case

M

E P

B VN

T ZA

To delete E

D

x

parent

Set the Lchild (@ Rchild) of x as the Lchild (@ Rchild) of parent

Page 81: 1 TK1924 Program Design & Problem Solving Session 2011/2012 L8: Binary Trees.

81

Delete Node: Second Case

M

E P

B VN

T ZA

To delete E

D

x

parent

Set the Lchild (@ Rchild) of x as the Lchild (@ Rchild) of parentFree x

Page 82: 1 TK1924 Program Design & Problem Solving Session 2011/2012 L8: Binary Trees.

82

Delete Node: Second Case

M

P

B VN

T ZA

To delete E

D

parent

Set the Lchild (@ Rchild) of x as the Lchild (@ Rchild) of parentFree x

Page 83: 1 TK1924 Program Design & Problem Solving Session 2011/2012 L8: Binary Trees.

83

Delete Node: Second Case

M

PB

VN

T Z

A

To delete E

D

parent

Set the Lchild (@ Rchild) of x as the Lchild (@ Rchild) of parentFree x

Page 84: 1 TK1924 Program Design & Problem Solving Session 2011/2012 L8: Binary Trees.

84

Delete Node: Third Case

M

E P

B VN

T ZA D

To delete P

Case 4: The node to be deleted has nonempty left and right subtrees

Page 85: 1 TK1924 Program Design & Problem Solving Session 2011/2012 L8: Binary Trees.

85

Delete Node: Third Case

To delete P

Case 4: The node to be deleted has nonempty left and right subtrees

M

E P

B VN

T ZA D

parent

x

Page 86: 1 TK1924 Program Design & Problem Solving Session 2011/2012 L8: Binary Trees.

86

Delete Node: Third Case

M

E P

B VN

T ZA D

To delete P

Determine the next node based on Inorder(LNR)

parent

x

*Usually, the Inorder node has only one child or no children at all

Page 87: 1 TK1924 Program Design & Problem Solving Session 2011/2012 L8: Binary Trees.

87

Delete Node: Third Case

M

E P

B VN

T ZA D

To delete P

Determine the next node based on Inorder(LNR)

parent

x

y

parent of y

Page 88: 1 TK1924 Program Design & Problem Solving Session 2011/2012 L8: Binary Trees.

88

Delete Node: Third Case

M

E P

B VN

T ZA D

To delete P

Copy

parent

x

y

parent of y

Page 89: 1 TK1924 Program Design & Problem Solving Session 2011/2012 L8: Binary Trees.

89

Delete Node: Third Case

M

E P

B VN

T ZA D

To delete P

Copy x->data = y->data

parent

x

y

parent of y

Page 90: 1 TK1924 Program Design & Problem Solving Session 2011/2012 L8: Binary Trees.

90

Delete Node: Third Case

M

E T

B VN

T ZA D

To delete P parent

x

y

Copy x->data = y->data

parent of y

Page 91: 1 TK1924 Program Design & Problem Solving Session 2011/2012 L8: Binary Trees.

91

Delete Node: Third Case

M

E T

B VN

T ZA D

To delete P parent

xy

Copy x->data = y->data x = y

parent of y

Page 92: 1 TK1924 Program Design & Problem Solving Session 2011/2012 L8: Binary Trees.

92

Delete Node: Third Case

M

E T

B VN

T ZA D

To delete P

Delete T

parent

y x

parent of y

Page 93: 1 TK1924 Program Design & Problem Solving Session 2011/2012 L8: Binary Trees.

93

Delete Node: Third Case

M

E T

B VN

T ZA D

To delete P

Delete T (as in case 1)parent_y->Lchild = NULL

parent

y

parent of y

Page 94: 1 TK1924 Program Design & Problem Solving Session 2011/2012 L8: Binary Trees.

94

Delete Node: Third Case

M

E T

B VN

ZA D

To delete P

Delete T

parent

parent of y

Page 95: 1 TK1924 Program Design & Problem Solving Session 2011/2012 L8: Binary Trees.

95

Operations Performed on Binary Search Trees

• Find the height of the binary search tree

• Find the number of nodes in the binary search tree

• Find the number of leaves in the binary search tree

• Traverse the binary search tree

• Copy the binary search tree

Page 96: 1 TK1924 Program Design & Problem Solving Session 2011/2012 L8: Binary Trees.

96

Summary

• Binary trees

• Binary search trees

• Recursive traversal algorithms


Recommended