Data Structures - CSCI 102 Binary Tree In binary trees, each Node can point to two other Nodes and...

63
Data Structures - CSCI 102 Binary Tree In binary trees, each Node can point to two other Nodes and looks something like this: template <typename T> class BTNode { public: //The data section BTNode data T data; left right //The links BTNode<T>* left; BTNode<T>* right; }; 6 Copyright © William C. Cheng

Transcript of Data Structures - CSCI 102 Binary Tree In binary trees, each Node can point to two other Nodes and...

Page 1: Data Structures - CSCI 102 Binary Tree In binary trees, each Node can point to two other Nodes and looks something like this: template class BTNode { public:

Data Structures - CSCI 102

Binary TreeIn binary trees, each Node can point to two other Nodesand looks something like this:

template <typename T>class BTNode {

public://The data section

BTNode

data

T data;left right

//The linksBTNode<T>* left;BTNode<T>* right;

};

6

Copyright © William C. Cheng

Page 2: Data Structures - CSCI 102 Binary Tree In binary trees, each Node can point to two other Nodes and looks something like this: template class BTNode { public:

7

Copyright © William C. Cheng

G

D

H

E

I

B

Data Structures - CSCI 102

Binary Tree

A

C

F

Page 3: Data Structures - CSCI 102 Binary Tree In binary trees, each Node can point to two other Nodes and looks something like this: template class BTNode { public:

Trees grown downward in computer science

8

Data Structures - CSCI 102

Copyright © William C. Cheng

Binary Tree Terminology

Each BTNode has 0, 1 or 2 Child Nodes that are below it inthe tree

A BTNode that has a child is called that child’s Parent Node

Parent nodes point to child nodes via a branch (child nodesdon’t need to point to parents, but they can if they want)

Page 4: Data Structures - CSCI 102 Binary Tree In binary trees, each Node can point to two other Nodes and looks something like this: template class BTNode { public:

9

Copyright © William C. Cheng

Data Structures - CSCI 102

Binary TreesA is the Parent of B and C

B is the Left Child of A

C is the Right Child of A

A

Branch

B

Branch

C

Page 5: Data Structures - CSCI 102 Binary Tree In binary trees, each Node can point to two other Nodes and looks something like this: template class BTNode { public:

The Root Node is the only BTNode in the tree that has noparent node

10

Data Structures - CSCI 102

Copyright © William C. Cheng

Binary Tree Terminology

A BTNode is a Leaf Node if it has no children

The Level of a BTNode in the tree is the number ofbranches from the root to the node

The Height of the tree is the number of nodes on thelongest path from root to leaf

Page 6: Data Structures - CSCI 102 Binary Tree In binary trees, each Node can point to two other Nodes and looks something like this: template class BTNode { public:

11

Copyright © William C. Cheng

Data Structures - CSCI 102

Binary Trees

B

A

C

D E

A is the Root Node

C, D, E are Leaf Nodes

Level(A) = 0

Level(B) = 1Level(D) = 2

The Height of the tree is 3

Page 7: Data Structures - CSCI 102 Binary Tree In binary trees, each Node can point to two other Nodes and looks something like this: template class BTNode { public:

What kind of operations do we need to perform on a binarytree?

12

Data Structures - CSCI 102

Copyright © William C. Cheng

Binary Trees

height()size()search()insert()delete()print()

NOTE: Pretty much every operation requires a way totraverse the tree

Page 8: Data Structures - CSCI 102 Binary Tree In binary trees, each Node can point to two other Nodes and looks something like this: template class BTNode { public:

14

Copyright © William C. Cheng

E

H I

D

G

B C

F

Data Structures - CSCI 102

Binary Tree HeightIf you had to calculate the height of a binary tree, howwould you do it?

Root

A

Page 9: Data Structures - CSCI 102 Binary Tree In binary trees, each Node can point to two other Nodes and looks something like this: template class BTNode { public:

15

Copyright © William C. Cheng

E

H I

D

G

B C

F

Data Structures - CSCI 102

Binary Tree HeightHeight = 1 + max(Height(Root->left),Height(Root->right))

Root

A

Height ofRoot->left

Root->right

1

Height of

1

Page 10: Data Structures - CSCI 102 Binary Tree In binary trees, each Node can point to two other Nodes and looks something like this: template class BTNode { public:

18

Copyright © William C. Cheng

E

H I

D

G

B C

F

Data Structures - CSCI 102

Binary Tree TraversalIf you had to print out all the nodes in a binary tree, howwould you do it?

Root

A

Page 11: Data Structures - CSCI 102 Binary Tree In binary trees, each Node can point to two other Nodes and looks something like this: template class BTNode { public:

If you had to print out all the nodes in a binary tree, howwould you do it?

19

Data Structures - CSCI 102

Copyright © William C. Cheng

Binary Tree Traversal

At each node in the tree, you have three choices:

These choices leads to different kinds of traversals

Print the current nodeGo to the left childGo to the right child

In Order: print left, print current, print rightPre Order: print current, print left, print rightPost Order: print left, print right, print current

Page 12: Data Structures - CSCI 102 Binary Tree In binary trees, each Node can point to two other Nodes and looks something like this: template class BTNode { public:

20

Copyright © William C. Cheng

Data Structures - CSCI 102

Binary Tree TraversalIn Order Traversal:

Traverse A’s left subtreePrint ATraverse A’s right subtree

Order of output:GDBHEIACF

E

I

D

G

B

H

A

C

F

Root

Page 13: Data Structures - CSCI 102 Binary Tree In binary trees, each Node can point to two other Nodes and looks something like this: template class BTNode { public:

Pre Order Traversal:Print ATraverse A’s left subtreeTraverse A’s right subtree

Order of output:ABDGEHICF

E

I

D

G

B

H

A

C

F

Root

22

Copyright © William C. Cheng

Data Structures - CSCI 102

Binary Tree Traversal

Page 14: Data Structures - CSCI 102 Binary Tree In binary trees, each Node can point to two other Nodes and looks something like this: template class BTNode { public:

Post Order Traversal:Traverse A’s left subtreeTraverse A’s right subtreePrint A

Order of output:GDHIEBFCA

E

I

D

G

B

H

A

C

F

Root

24

Copyright © William C. Cheng

Data Structures - CSCI 102

Binary Tree Traversal

Page 15: Data Structures - CSCI 102 Binary Tree In binary trees, each Node can point to two other Nodes and looks something like this: template class BTNode { public:

26

Copyright © William C. Cheng

E

H I

D

G

B C

F

Data Structures - CSCI 102

Binary Tree SearchIf you had to find a particular value in a binary tree, howwould you do it?

Root

A

Page 16: Data Structures - CSCI 102 Binary Tree In binary trees, each Node can point to two other Nodes and looks something like this: template class BTNode { public:

Search still requires us to potentially look at everysingle node in the treeStill O(n)

So far binary trees are interesting, but they don’t provideany extra benefit

27

Data Structures - CSCI 102

Copyright © William C. Cheng

Binary Tree Issues

If you’re going to insert a new Node, where should youput it in the tree?

What’s missing here is some extra organization that makesit easier to find things

Page 17: Data Structures - CSCI 102 Binary Tree In binary trees, each Node can point to two other Nodes and looks something like this: template class BTNode { public:

template <typename T, typename U>class BTNode {

public:T key; //The key dataU data; //The data

left right//The linksBTNode<T,U>* left;BTNode<T,U>* right;

};

8

Copyright © William C. Cheng

Data Structures - CSCI 102

Binary TreeBTNode can also have a separate key and data

key is used to order the nodes in the treedata actually holds the stuff we care about

BTNode

key

Page 18: Data Structures - CSCI 102 Binary Tree In binary trees, each Node can point to two other Nodes and looks something like this: template class BTNode { public:

To speed up insertion, removal and search, modify the ideaof a Binary Tree to create a Binary Search Tree (BST)

9

Data Structures - CSCI 102

Copyright © William C. Cheng

Binary Search Trees

Binary Search Trees have one major organizationalproperty (the binary search tree property):

Let X be a Node in a binary search tree

Every Node X is comparable to every other Node via afield we call the key (for now, "data" is the key)If Y is a Node in the left subtree of X:key[Y] <= key[X]

key[Y] > key[X]

If keys must be unique, then we have key[Y] < key[X]You can implement your BST to enforce thisWe will assume that this is the case here

If Y is a Node in the right subtree of X:

Page 19: Data Structures - CSCI 102 Binary Tree In binary trees, each Node can point to two other Nodes and looks something like this: template class BTNode { public:

11

Copyright © William C. Cheng

Data Structures - CSCI 102

Binary Search Tree Search

58

7746

30

If you had to find a particular value in abinary search tree, how would you do it?

Ex: find 58

50

60

70

80

Root

Page 20: Data Structures - CSCI 102 Binary Tree In binary trees, each Node can point to two other Nodes and looks something like this: template class BTNode { public:

If you had to find a particular value in abinary search tree, how would you do it?

12

Copyright © William C. Cheng

Data Structures - CSCI 102

Binary Search Tree Search

5830

50

46

60

70

77

80

Root

Ex: find 58Start at root

58 == 60? No58 < 60? Yes, go left

Current

Page 21: Data Structures - CSCI 102 Binary Tree In binary trees, each Node can point to two other Nodes and looks something like this: template class BTNode { public:

13

Copyright © William C. Cheng

Data Structures - CSCI 102

Binary Search Tree Search

58

7746

30

50

60

70

80

RootIf you had to find a particular value in abinary search tree, how would you do it?

Ex: find 58Start at root

58 == 60? No58 < 60? Yes, go left

Current

58 == 50? No58 < 50? No58 > 50? Yes, go right

Page 22: Data Structures - CSCI 102 Binary Tree In binary trees, each Node can point to two other Nodes and looks something like this: template class BTNode { public:

14

Copyright © William C. Cheng

Data Structures - CSCI 102

Binary Search Tree Search

58

7746

30

50

60

70

80

RootIf you had to find a particular value in abinary search tree, how would you do it?

Ex: find 58Start at root

58 == 60? No58 < 60? Yes, go left58 == 50? No58 < 50? No

Current

58 > 50? Yes, go right58 == 58? Yes

Page 23: Data Structures - CSCI 102 Binary Tree In binary trees, each Node can point to two other Nodes and looks something like this: template class BTNode { public:

Start at the rootWhat will our search algorithm be?

If the key we want is less than the key of the currentnode, search the left subtree

15

Data Structures - CSCI 102

Copyright © William C. Cheng

Binary Search Tree Search

Is the current node what we want?

Otherwise, search the right subtree

If yes, we’re doneIf no...

Should our solution be recursive or iterative?

Page 24: Data Structures - CSCI 102 Binary Tree In binary trees, each Node can point to two other Nodes and looks something like this: template class BTNode { public:

18

Copyright © William C. Cheng

58

7746

30

50 70

80

Data Structures - CSCI 102

Binary Search Tree Min/MaxHow would you find the minimum/maximum values in abinary search tree?

Root

60

Page 25: Data Structures - CSCI 102 Binary Tree In binary trees, each Node can point to two other Nodes and looks something like this: template class BTNode { public:

Start at the rootWhat will our findMin algorithm be?

19

Data Structures - CSCI 102

Copyright © William C. Cheng

Binary Search Tree Min/Max

Does the current Node have a left child?If yes, go to the left childIf no, we’re at the minimum

Start at the rootWhat will our findMax algorithm be?

Does the current Node have a right child?If yes, go to the right childIf no, we’re at the maximum

Page 26: Data Structures - CSCI 102 Binary Tree In binary trees, each Node can point to two other Nodes and looks something like this: template class BTNode { public:

Order of output:30 46 50 58 60 70 77 80

discussed?In Order Traversal

Visits all the nodesin the tree in order

21

Copyright © William C. Cheng

Data Structures - CSCI 102

Binary Search Tree TraversalNow that we have an implicit ordering to our tree, whathappens with the traversal algorithms we previously

5830

50

46

60

70

77

80

Root

Page 27: Data Structures - CSCI 102 Binary Tree In binary trees, each Node can point to two other Nodes and looks something like this: template class BTNode { public:

22

Data Structures - CSCI 102

Copyright © William C. Cheng

Binary Search Tree Traversal

Traverse the binary search tree with an in-ordertraversal

Tree SortGiven an unordered list of elements, add them all to abinary search tree

All the nodes come back out sorted!

Page 28: Data Structures - CSCI 102 Binary Tree In binary trees, each Node can point to two other Nodes and looks something like this: template class BTNode { public:

Binary Search Tree Insertion

24

Data Structures - CSCI 102

Copyright © William C. Cheng

Binary Search Tree Insertion

New nodes are always added in the place of an existingNULL...they never reposition other nodes

1) Create the new node with its data and set both of itspointers to NULL2) If the tree is empty, make the new node the root3) Walk down the tree node by node like you were doinga search

If new value is less than current node, move to leftsubtree, otherwise move to right subtreePotentially reject duplicates

Page 29: Data Structures - CSCI 102 Binary Tree In binary trees, each Node can point to two other Nodes and looks something like this: template class BTNode { public:

25

Copyright © William C. Cheng

Data Structures - CSCI 102

Binary Search Tree InsertionHow do you insert a new value into a binary search tree?

58

7746

30

50

60

70

80

RootEx: insert 59

Page 30: Data Structures - CSCI 102 Binary Tree In binary trees, each Node can point to two other Nodes and looks something like this: template class BTNode { public:

Ex: insert 59Make new node

26

Copyright © William C. Cheng

Data Structures - CSCI 102

Binary Search Tree InsertionHow do you insert a new value into a binary search tree?

58

7746

30

50

60

70

80

Root

59New Node

Page 31: Data Structures - CSCI 102 Binary Tree In binary trees, each Node can point to two other Nodes and looks something like this: template class BTNode { public:

27

Copyright © William C. Cheng

Data Structures - CSCI 102

Binary Search Tree InsertionHow do you insert a new value into a binary search tree?

58

7746

30

50

60

70

80

Root

59New Node

Current

Ex: insert 59Make new nodeStart at root59 < 60; go left

Page 32: Data Structures - CSCI 102 Binary Tree In binary trees, each Node can point to two other Nodes and looks something like this: template class BTNode { public:

59 > 50; go right

28

Copyright © William C. Cheng

Data Structures - CSCI 102

Binary Search Tree InsertionHow do you insert a new value into a binary search tree?

58

7746

30

50

60

70

80

Root

59New Node

Current

Ex: insert 59Make new nodeStart at root59 < 60; go left

Page 33: Data Structures - CSCI 102 Binary Tree In binary trees, each Node can point to two other Nodes and looks something like this: template class BTNode { public:

29

Copyright © William C. Cheng

Data Structures - CSCI 102

Binary Search Tree InsertionHow do you insert a new value into a binary search tree?

7746

30

50

60

70

80

Root

59New Node

Current

58

Ex: insert 59Make new nodeStart at root59 < 60; go left

59 > 50; go right59 > 58; go right

Page 34: Data Structures - CSCI 102 Binary Tree In binary trees, each Node can point to two other Nodes and looks something like this: template class BTNode { public:

30

Copyright © William C. Cheng

Data Structures - CSCI 102

Binary Search Tree InsertionHow do you insert a new value into a binary search tree?

58

7746

30

50

60

70

80

Root

59

Ex: insert 59Make new nodeStart at root59 < 60; go left

59 > 50; go right59 > 58; go rightRight is NULL

Insert new Node

Page 35: Data Structures - CSCI 102 Binary Tree In binary trees, each Node can point to two other Nodes and looks something like this: template class BTNode { public:

31

Copyright © William C. Cheng

Data Structures - CSCI 102

Binary Search Tree InsertionHow do you insert a new value into a binary search tree?

58

7746

30

50

60

70

80

Root

59

Ex: insert 59Make new nodeStart at root59 < 60; go left

59 > 50; go right59 > 58; go rightRight is NULL

Insert new Node

Page 36: Data Structures - CSCI 102 Binary Tree In binary trees, each Node can point to two other Nodes and looks something like this: template class BTNode { public:

Binary Search Tree Removal

32

Data Structures - CSCI 102

Copyright © William C. Cheng

Binary Search Tree Remove

Search the tree for the node that you want to removeWhen node is found, removal breaks down into 3 distinctcases

1) Remove a node with no children2) Remove a node with one child3) Remove a node with two children

Page 37: Data Structures - CSCI 102 Binary Tree In binary trees, each Node can point to two other Nodes and looks something like this: template class BTNode { public:

Find the node and its parentRemove a node with no children (case #1)

33

Data Structures - CSCI 102

Copyright © William C. Cheng

Binary Search Tree Remove

Set the parent pointer to the node to NULLDelete the node

Page 38: Data Structures - CSCI 102 Binary Tree In binary trees, each Node can point to two other Nodes and looks something like this: template class BTNode { public:

Case #1 Ex: remove 46

34

Copyright © William C. Cheng

Data Structures - CSCI 102

Binary Search Tree RemoveHow do you remove a value from a binary search tree?

58

7746

30

50

60

70

80

Root

Page 39: Data Structures - CSCI 102 Binary Tree In binary trees, each Node can point to two other Nodes and looks something like this: template class BTNode { public:

35

Copyright © William C. Cheng

Data Structures - CSCI 102

Binary Search Tree RemoveHow do you remove a value from a binary search tree?

58

7746

30

50

60

70

80

Root

To remove

Case #1 Ex: remove 46Search for the value

Page 40: Data Structures - CSCI 102 Binary Tree In binary trees, each Node can point to two other Nodes and looks something like this: template class BTNode { public:

36

Copyright © William C. Cheng

Data Structures - CSCI 102

Binary Search Tree RemoveHow do you remove a value from a binary search tree?

58

7746

30

50

60

70

80

Root

To remove

Case #1 Ex: remove 46Search for the valueSet parent’s pointer to NULL

Page 41: Data Structures - CSCI 102 Binary Tree In binary trees, each Node can point to two other Nodes and looks something like this: template class BTNode { public:

37

Copyright © William C. Cheng

Data Structures - CSCI 102

Binary Search Tree RemoveHow do you remove a value from a binary search tree?

58

7746

30

50

60

70

80

Root

To remove

Case #1 Ex: remove 46Search for the valueSet parent’s pointer to NULLDelete the child

Page 42: Data Structures - CSCI 102 Binary Tree In binary trees, each Node can point to two other Nodes and looks something like this: template class BTNode { public:

Data Structures - CSCI 102

Binary Search Tree RemoveHow do you remove a value from a binary search tree?

58

77

38

Copyright © William C. Cheng

30

50

60

70

80

RootCase #1 Ex: remove 46Search for the valueSet parent’s pointer to NULLDelete the child

Page 43: Data Structures - CSCI 102 Binary Tree In binary trees, each Node can point to two other Nodes and looks something like this: template class BTNode { public:

Find the node and its parentRemove a node with one child (case #2)

Set the parent pointer to the node’s only childDelete the node

39

Data Structures - CSCI 102

Copyright © William C. Cheng

Binary Search Tree Remove

Page 44: Data Structures - CSCI 102 Binary Tree In binary trees, each Node can point to two other Nodes and looks something like this: template class BTNode { public:

58

7746

30

50

60

70

80

Root

40

Copyright © William C. Cheng

Data Structures - CSCI 102

Binary Search Tree RemoveHow do you remove a value from a binary search tree?

Case #2 Ex: remove 30

Page 45: Data Structures - CSCI 102 Binary Tree In binary trees, each Node can point to two other Nodes and looks something like this: template class BTNode { public:

58

7746

30

50

60

70

80

Root

41

Copyright © William C. Cheng

Data Structures - CSCI 102

Binary Search Tree RemoveHow do you remove a value from a binary search tree?

To remove

Case #2 Ex: remove 30Search for the value

Page 46: Data Structures - CSCI 102 Binary Tree In binary trees, each Node can point to two other Nodes and looks something like this: template class BTNode { public:

58

7746

30

50

60

70

80

Root

42

Copyright © William C. Cheng

Data Structures - CSCI 102

Binary Search Tree RemoveHow do you remove a value from a binary search tree?

To remove

Case #2 Ex: remove 30Search for the valuePointer value’s parent tovalue’s only child

Page 47: Data Structures - CSCI 102 Binary Tree In binary trees, each Node can point to two other Nodes and looks something like this: template class BTNode { public:

58

7746

30

50

60

70

80

Root

43

Copyright © William C. Cheng

Data Structures - CSCI 102

Binary Search Tree RemoveHow do you remove a value from a binary search tree?

To remove

Case #2 Ex: remove 30Search for the valuePointer value’s parent tovalue’s only child

Delete the node

Page 48: Data Structures - CSCI 102 Binary Tree In binary trees, each Node can point to two other Nodes and looks something like this: template class BTNode { public:

58

50

46

60

70

77

80

Root

44

Copyright © William C. Cheng

Data Structures - CSCI 102

Binary Search Tree RemoveHow do you remove a value from a binary search tree?

Case #2 Ex: remove 30Search for the valuePointer value’s parent tovalue’s only child

Delete the node

Page 49: Data Structures - CSCI 102 Binary Tree In binary trees, each Node can point to two other Nodes and looks something like this: template class BTNode { public:

Find the nodeRemove a node with two children (case #3)

45

Data Structures - CSCI 102

Copyright © William C. Cheng

Binary Search Tree Remove

Remove node’s successor or predecessorReplace node’s data with successor/predecessor data

The node in the tree with the biggest value less than xPredecessor(x)

The node in the tree with the smallest value greaterthan x

Successor(x)

Page 50: Data Structures - CSCI 102 Binary Tree In binary trees, each Node can point to two other Nodes and looks something like this: template class BTNode { public:

58

7746

30

50

60

70

80

RootCase #3 Ex: remove 50

46

Copyright © William C. Cheng

Data Structures - CSCI 102

Binary Search Tree RemoveHow do you remove a value from a binary search tree?

Page 51: Data Structures - CSCI 102 Binary Tree In binary trees, each Node can point to two other Nodes and looks something like this: template class BTNode { public:

58

7746

30

50

60

70

80

RootCase #3 Ex: remove 50

47

Copyright © William C. Cheng

Data Structures - CSCI 102

Binary Search Tree RemoveHow do you remove a value from a binary search tree?

Search for the valueTo remove

Page 52: Data Structures - CSCI 102 Binary Tree In binary trees, each Node can point to two other Nodes and looks something like this: template class BTNode { public:

5830

50

46

60

70

77

80

RootCase #3 Ex: remove 50

48

Copyright © William C. Cheng

Data Structures - CSCI 102

Binary Search Tree RemoveHow do you remove a value from a binary search tree?

Search for the valueFind predecessor

Rightmost value in

To remove

left subtree of node

Predecessor

Page 53: Data Structures - CSCI 102 Binary Tree In binary trees, each Node can point to two other Nodes and looks something like this: template class BTNode { public:

58

77

30

60

70

80

RootCase #3 Ex: remove 50

49

Copyright © William C. Cheng

Data Structures - CSCI 102

Binary Search Tree RemoveHow do you remove a value from a binary search tree?

Predecessor

46

To remove

Search for the valueFind predecessor

Rightmost value inleft subtree of node

Swap values of nodeand its predecessor

Swap

50

Page 54: Data Structures - CSCI 102 Binary Tree In binary trees, each Node can point to two other Nodes and looks something like this: template class BTNode { public:

58

7750

30

46

60

70

80

Root

50

Copyright © William C. Cheng

Data Structures - CSCI 102

Binary Search Tree RemoveHow do you remove a value from a binary search tree?

Case #3 Ex: remove 50Search for the valueFind predecessor

Rightmost value in

To remove

left subtree of nodeSwap values of nodeand its predecessor

Delete the node viacase #1 or case #2

Page 55: Data Structures - CSCI 102 Binary Tree In binary trees, each Node can point to two other Nodes and looks something like this: template class BTNode { public:

5830

46

60

70

80

Root

Data Structures - CSCI 102

Binary Search Tree RemoveHow do you remove a value from a binary search tree?

Case #3 Ex: remove 50Search for the valueFind predecessor

Rightmost value in

left subtree of nodeSwap values of nodeand its predecessor

Delete the node viacase #1 or case #2

77

51

Copyright © William C. Cheng

Page 56: Data Structures - CSCI 102 Binary Tree In binary trees, each Node can point to two other Nodes and looks something like this: template class BTNode { public:

60, 46, 77, 45, 50, 63, 91Insert the following values in an empty tree (in this order)

52

Data Structures - CSCI 102

Copyright © William C. Cheng

Binary Search Tree Construction

What does the resulting tree look like?

Page 57: Data Structures - CSCI 102 Binary Tree In binary trees, each Node can point to two other Nodes and looks something like this: template class BTNode { public:

53

Copyright © William C. Cheng

Data Structures - CSCI 102

Binary Search Tree ConstructionInsert the following values in an empty tree (in this order)

60, 46, 77, 45, 50, 63, 91

50 6345

What does the resulting tree look like?Tree is perfectly balanced!

Height = log(n+1)

46

60

91

Root

77

Page 58: Data Structures - CSCI 102 Binary Tree In binary trees, each Node can point to two other Nodes and looks something like this: template class BTNode { public:

91, 77, 63, 60, 50, 46, 45

Insert the following values in an empty tree (same values aslast time, but different order)

54

Data Structures - CSCI 102

Copyright © William C. Cheng

Binary Search Tree Construction

What does the resulting tree look like?

Page 59: Data Structures - CSCI 102 Binary Tree In binary trees, each Node can point to two other Nodes and looks something like this: template class BTNode { public:

91Root

Data Structures - CSCI 102

Binary Search Tree ConstructionInsert the following values in an empty tree (same values as

last time, but different order)91, 77, 63, 60, 50, 46, 45

What does the resulting tree look like?Insert 91

55

Copyright © William C. Cheng

Page 60: Data Structures - CSCI 102 Binary Tree In binary trees, each Node can point to two other Nodes and looks something like this: template class BTNode { public:

91Root

77

Data Structures - CSCI 102

Binary Search Tree ConstructionInsert the following values in an empty tree (same values as

last time, but different order)91, 77, 63, 60, 50, 46, 45

What does the resulting tree look like?

Insert 91Insert 77

56

Copyright © William C. Cheng

Page 61: Data Structures - CSCI 102 Binary Tree In binary trees, each Node can point to two other Nodes and looks something like this: template class BTNode { public:

91Root

77

Data Structures - CSCI 102

Binary Search Tree ConstructionInsert the following values in an empty tree (same values as

last time, but different order)91, 77, 63, 60, 50, 46, 45

What does the resulting tree look like?Insert 91

63Insert 77

Insert 63

57

Copyright © William C. Cheng

Page 62: Data Structures - CSCI 102 Binary Tree In binary trees, each Node can point to two other Nodes and looks something like this: template class BTNode { public:

58

Copyright © William C. Cheng

91Root

77

Data Structures - CSCI 102

Binary Search Tree ConstructionInsert the following values in an empty tree (same values as

last time, but different order)91, 77, 63, 60, 50, 46, 45

What does the resulting tree look like?Insert 91

63Insert 77Insert 63 60Insert 60

Page 63: Data Structures - CSCI 102 Binary Tree In binary trees, each Node can point to two other Nodes and looks something like this: template class BTNode { public:

91Root

77

60

50

46

45

59

Copyright © William C. Cheng

Data Structures - CSCI 102

Binary Search Tree ConstructionInsert the following values in an empty tree (same values as

last time, but different order)91, 77, 63, 60, 50, 46, 45

What does the resulting tree look like?Tree is completely unbalanced

63Height = n