No Slide Titlegercal/cs340/slides/review/binary-yolum.pdf · 12 Terminology A node that has...

108
1 5 Linked Structures Based on Levent Akın’s CmpE160 Lecture Slides 3 Binary Trees

Transcript of No Slide Titlegercal/cs340/slides/review/binary-yolum.pdf · 12 Terminology A node that has...

Page 1: No Slide Titlegercal/cs340/slides/review/binary-yolum.pdf · 12 Terminology A node that has subtrees is the parent of the roots of the subtrees. The roots of these subtrees are the

1

5 Linked Structures

Based on Levent Akın’s CmpE160

Lecture Slides

3

Binary Trees

Page 2: No Slide Titlegercal/cs340/slides/review/binary-yolum.pdf · 12 Terminology A node that has subtrees is the parent of the roots of the subtrees. The roots of these subtrees are the

2

Owner

Jake

Manager Chef

Brad Carol

Waitress Waiter Cook Helper Joyce Chris Max Len

Jake‟s Pizza Shop

Page 3: No Slide Titlegercal/cs340/slides/review/binary-yolum.pdf · 12 Terminology A node that has subtrees is the parent of the roots of the subtrees. The roots of these subtrees are the

3

Definition of Tree

A tree is a finite set of one or more nodes such that:

There is a specially designated node called the root.

The remaining nodes are partitioned into n>=0 disjoint sets T1, ..., Tn, where each of these sets is a tree.

We call T1, ..., Tn the subtrees of the root.

Definition

Page 4: No Slide Titlegercal/cs340/slides/review/binary-yolum.pdf · 12 Terminology A node that has subtrees is the parent of the roots of the subtrees. The roots of these subtrees are the

4

Owner

Jake

Manager Chef

Brad Carol

Waitress Waiter Cook Helper Joyce Chris Max Len

ROOT NODE

A Tree Has a Root Node

Page 5: No Slide Titlegercal/cs340/slides/review/binary-yolum.pdf · 12 Terminology A node that has subtrees is the parent of the roots of the subtrees. The roots of these subtrees are the

5

Owner

Jake

Manager Chef

Brad Carol

Waitress Waiter Cook Helper Joyce Chris Max Len

LEAF NODES

Leaf Nodes have No Children

Page 6: No Slide Titlegercal/cs340/slides/review/binary-yolum.pdf · 12 Terminology A node that has subtrees is the parent of the roots of the subtrees. The roots of these subtrees are the

6

Owner

Jake

Manager Chef

Brad Carol

Waitress Waiter Cook Helper Joyce Chris Max Len

LEVEL 0

A Tree Has Leaves

Page 7: No Slide Titlegercal/cs340/slides/review/binary-yolum.pdf · 12 Terminology A node that has subtrees is the parent of the roots of the subtrees. The roots of these subtrees are the

7

Owner

Jake

Manager Chef

Brad Carol

Waitress Waiter Cook Helper Joyce Chris Max Len

LEVEL 1

Level One

Page 8: No Slide Titlegercal/cs340/slides/review/binary-yolum.pdf · 12 Terminology A node that has subtrees is the parent of the roots of the subtrees. The roots of these subtrees are the

8

Owner

Jake

Manager Chef

Brad Carol

Waitress Waiter Cook Helper Joyce Chris Max Len

LEVEL 2

Level Two

Page 9: No Slide Titlegercal/cs340/slides/review/binary-yolum.pdf · 12 Terminology A node that has subtrees is the parent of the roots of the subtrees. The roots of these subtrees are the

9

Owner

Jake

Manager Chef

Brad Carol

Waitress Waiter Cook Helper Joyce Chris Max Len

LEFT SUBTREE OF ROOT NODE

A Subtree

Page 10: No Slide Titlegercal/cs340/slides/review/binary-yolum.pdf · 12 Terminology A node that has subtrees is the parent of the roots of the subtrees. The roots of these subtrees are the

10

Owner

Jake

Manager Chef

Brad Carol

Waitress Waiter Cook Helper Joyce Chris Max Len

RIGHT SUBTREE

OF ROOT NODE

Another Subtree

Page 11: No Slide Titlegercal/cs340/slides/review/binary-yolum.pdf · 12 Terminology A node that has subtrees is the parent of the roots of the subtrees. The roots of these subtrees are the

11

Terminology

The degree of a node is the number of

subtrees of the node

The node with degree 0 is a leaf or

terminal node. All other nodes are

nonterminals.

The degree of a tree is the maximum

degree of the nodes of a tree.

Page 12: No Slide Titlegercal/cs340/slides/review/binary-yolum.pdf · 12 Terminology A node that has subtrees is the parent of the roots of the subtrees. The roots of these subtrees are the

12

Terminology

A node that has subtrees is the parent of the roots of the subtrees.

The roots of these subtrees are the children of the node.

Children of the same parent are siblings.

The ancestors of a node are all the nodes along the path from the root to the node

The height or depth of a tree is the maximum level of any node in the tree.

Page 13: No Slide Titlegercal/cs340/slides/review/binary-yolum.pdf · 12 Terminology A node that has subtrees is the parent of the roots of the subtrees. The roots of these subtrees are the

13

Level and Depth

K L

E F

B

G

C

M

H I J

D

A

Level

1

2

3

4

3

2 1 3

2 0 0 1 0 0

0 0 0

1

2 2 2

3 3 3 3 3 3

4 4 4

Sample Tree

Page 14: No Slide Titlegercal/cs340/slides/review/binary-yolum.pdf · 12 Terminology A node that has subtrees is the parent of the roots of the subtrees. The roots of these subtrees are the

Tree Representations

For a tree of degree k (also called a k-ary

tree), we could define a node structure that

contains a data field plus k link fields.

Advantage: uniform node structure (can

insert/delete nodes without having to change node

structure)

Disadvantage: many null link fields

data link 1 link 2 ... link k

Page 15: No Slide Titlegercal/cs340/slides/review/binary-yolum.pdf · 12 Terminology A node that has subtrees is the parent of the roots of the subtrees. The roots of these subtrees are the

Tree Representations

(continued)

In general, for a tree of degree k with n nodes, we

know that the total number of link fields in the tree

is kn and that exactly n-1 of them are not null.

Therefore the number of null links is

kn - (n-1) = (k-1)n + 1.

degree proportion of links that are null 2 (n+1)/2n, or about 1/2 3 (2n+1)/3n, or about 2/3 4 (3n+1)/4n, or about 3/4

Page 16: No Slide Titlegercal/cs340/slides/review/binary-yolum.pdf · 12 Terminology A node that has subtrees is the parent of the roots of the subtrees. The roots of these subtrees are the

16

Percentage of Null links

Page 17: No Slide Titlegercal/cs340/slides/review/binary-yolum.pdf · 12 Terminology A node that has subtrees is the parent of the roots of the subtrees. The roots of these subtrees are the

Tree Representations

(continued)

Clearly, to reduce the proportion of null

links, we need to reduce the degree of

the tree.

An alternative representation for a tree

of degree k:

Use a left child-right sibling representation.

Each node has two pointers, one to its left

child and one to its right sibling.

Page 18: No Slide Titlegercal/cs340/slides/review/binary-yolum.pdf · 12 Terminology A node that has subtrees is the parent of the roots of the subtrees. The roots of these subtrees are the

18

Left Child - Right Sibling link structure

A

B C D

E F G H I J

K L M

data

left child right sibling

Page 19: No Slide Titlegercal/cs340/slides/review/binary-yolum.pdf · 12 Terminology A node that has subtrees is the parent of the roots of the subtrees. The roots of these subtrees are the

19

The actual tree represented by it

A

B C D

E F G H I J

K L M

Page 20: No Slide Titlegercal/cs340/slides/review/binary-yolum.pdf · 12 Terminology A node that has subtrees is the parent of the roots of the subtrees. The roots of these subtrees are the

20

A binary tree is a structure in which:

Each node can have at most two children, and in which a unique path exists from the root to every other node.

The two children of a node are called the left child and the right child, if they exist.

Binary Tree

Page 21: No Slide Titlegercal/cs340/slides/review/binary-yolum.pdf · 12 Terminology A node that has subtrees is the parent of the roots of the subtrees. The roots of these subtrees are the

21

A Binary Tree

Q

V

T

K S

A E

L

Page 22: No Slide Titlegercal/cs340/slides/review/binary-yolum.pdf · 12 Terminology A node that has subtrees is the parent of the roots of the subtrees. The roots of these subtrees are the

22

How many leaf nodes?

Q

V

T

K S

A E

L

Page 23: No Slide Titlegercal/cs340/slides/review/binary-yolum.pdf · 12 Terminology A node that has subtrees is the parent of the roots of the subtrees. The roots of these subtrees are the

23

How many descendants of Q?

Q

V

T

K S

A E

L

Page 24: No Slide Titlegercal/cs340/slides/review/binary-yolum.pdf · 12 Terminology A node that has subtrees is the parent of the roots of the subtrees. The roots of these subtrees are the

24

How many ancestors of K?

Q

V

T

K S

A E

L

Page 25: No Slide Titlegercal/cs340/slides/review/binary-yolum.pdf · 12 Terminology A node that has subtrees is the parent of the roots of the subtrees. The roots of these subtrees are the

25

Implementing a Binary Tree with

Pointers and Dynamic Data

Q

V

T

K S

A E

L

Page 26: No Slide Titlegercal/cs340/slides/review/binary-yolum.pdf · 12 Terminology A node that has subtrees is the parent of the roots of the subtrees. The roots of these subtrees are the

26

Node Terminology for a Tree Node

Page 27: No Slide Titlegercal/cs340/slides/review/binary-yolum.pdf · 12 Terminology A node that has subtrees is the parent of the roots of the subtrees. The roots of these subtrees are the

27

A

B

A

B

A

B C

G E

I

D

H

F

Complete Binary Tree

Skewed Binary Tree

E

C

D

1

2

3

4 5

Samples of Binary Trees

Page 28: No Slide Titlegercal/cs340/slides/review/binary-yolum.pdf · 12 Terminology A node that has subtrees is the parent of the roots of the subtrees. The roots of these subtrees are the

28

The maximum number of nodes on level i of

a binary tree is 2i-1, i≥1.

The maximum number of nodes in a binary

tree of depth k is 2k-1, k ≥ 1.

Prove by induction.

2 2 11

1

i

i

kk

Maximum Number of Nodes in BT

Page 29: No Slide Titlegercal/cs340/slides/review/binary-yolum.pdf · 12 Terminology A node that has subtrees is the parent of the roots of the subtrees. The roots of these subtrees are the

29

For any nonempty binary tree, T, if n0 is the

number of leaf nodes and n2 the number of

nodes of degree 2, then n0=n2+1

proof:

Let n and B denote the total number of

nodes & branches in T.

Let n0, n1, n2 represent the nodes with no

children, single child, and two children

respectively.

n= n0+n1+n2, B+1=n, B=n1+2n2 ==> n1+2n2+1= n, n1+2n2+1= n0+n1+n2 ==> n0=n2+1

Relations between Number of

Leaf Nodes and Nodes of Degree 2

Page 30: No Slide Titlegercal/cs340/slides/review/binary-yolum.pdf · 12 Terminology A node that has subtrees is the parent of the roots of the subtrees. The roots of these subtrees are the

30

Definitions

Full Binary Tree: A binary tree in which

all of the leaves are on the same level

and every nonleaf node has two

children

Page 31: No Slide Titlegercal/cs340/slides/review/binary-yolum.pdf · 12 Terminology A node that has subtrees is the parent of the roots of the subtrees. The roots of these subtrees are the

31

Definitions (cont.)

Complete Binary Tree: A binary tree

that is either full or full through the next-

to-last level, with the leaves on the last

level as far to the left as possible

Page 32: No Slide Titlegercal/cs340/slides/review/binary-yolum.pdf · 12 Terminology A node that has subtrees is the parent of the roots of the subtrees. The roots of these subtrees are the

32

Examples of Different Types of

Binary Trees

Page 33: No Slide Titlegercal/cs340/slides/review/binary-yolum.pdf · 12 Terminology A node that has subtrees is the parent of the roots of the subtrees. The roots of these subtrees are the

33

A full binary tree of depth k is a binary tree of

depth k having 2 -1 nodes, k ≥ 0.

A binary tree with n nodes and depth k is

complete iff its nodes correspond to the nodes

numbered from 1 to n in the full binary tree of

depth k.

k

A

B C

G E

I

D

H

F

A

B C

G E

K

D

J

F

I H O N M L

Full binary tree of depth 4 Complete binary tree

Full BT VS Complete BT

Page 34: No Slide Titlegercal/cs340/slides/review/binary-yolum.pdf · 12 Terminology A node that has subtrees is the parent of the roots of the subtrees. The roots of these subtrees are the

34

A Binary Tree and Its

Array Representation

Page 35: No Slide Titlegercal/cs340/slides/review/binary-yolum.pdf · 12 Terminology A node that has subtrees is the parent of the roots of the subtrees. The roots of these subtrees are the

35

With Array Representation

For any node tree.nodes[index]

its left child is in tree.nodes[index*2

+ 1]

right child is in tree.nodes[index*2 +

2]

its parent is in tree.nodes[(index –

1)/2].

Page 36: No Slide Titlegercal/cs340/slides/review/binary-yolum.pdf · 12 Terminology A node that has subtrees is the parent of the roots of the subtrees. The roots of these subtrees are the

36

Sequential representation

A

B

--

C

--

--

--

D

--

.

E

[0]

[1]

[2]

[3]

[4]

[5]

[6]

[7]

[8]

.

[15]

[0]

[1]

[2]

[3]

[4]

[5]

[6]

[7]

[8]

A

B

C

D

E

F

G

H

I

A

B

E

C

D

A

B C

G E

I

D

H

F

(1) space waste

(2) insertion/deletion

problem

Page 37: No Slide Titlegercal/cs340/slides/review/binary-yolum.pdf · 12 Terminology A node that has subtrees is the parent of the roots of the subtrees. The roots of these subtrees are the

37

typedef struct node *treePointer;

typedef struct node {

ItemType data;

treePointer leftChild;

treePointer rightChild;

};

data leftChild rightChild

data

leftChild rightChild

Linked Representation

Page 38: No Slide Titlegercal/cs340/slides/review/binary-yolum.pdf · 12 Terminology A node that has subtrees is the parent of the roots of the subtrees. The roots of these subtrees are the

38

Let L, V, and R stand for moving left, visiting

the node, and moving right.

There are six possible combinations of

traversal

LVR, LRV, VLR, VRL, RVL, RLV

Adopt convention that we traverse left

before right, only 3 traversals remain

LVR, LRV, VLR

inorder, postorder, preorder

Binary Tree Traversals

Page 39: No Slide Titlegercal/cs340/slides/review/binary-yolum.pdf · 12 Terminology A node that has subtrees is the parent of the roots of the subtrees. The roots of these subtrees are the

39

+

*

A

*

/

E

D

C

B

Arithmetic Expression Using BT

Page 40: No Slide Titlegercal/cs340/slides/review/binary-yolum.pdf · 12 Terminology A node that has subtrees is the parent of the roots of the subtrees. The roots of these subtrees are the

40

void inorder(treePointer ptr)

// inorder tree traversal

{

if (ptr!=NULL) {

inorder(ptr->leftChild);

visit(ptr->data);

inorder(ptr->rightChild);

}

}

A / B * C * D + E

Inorder Traversal (recursive version)

Page 41: No Slide Titlegercal/cs340/slides/review/binary-yolum.pdf · 12 Terminology A node that has subtrees is the parent of the roots of the subtrees. The roots of these subtrees are the

41

void preorder(treePointer ptr)

// preorder tree traversal

{

if (ptr!=NULL) {

visit(ptr->data);

preorder(ptr->leftChild);

preorder(ptr->rightChild);

}

}

+ * * / A B C D E

Preorder Traversal (recursive version)

Page 42: No Slide Titlegercal/cs340/slides/review/binary-yolum.pdf · 12 Terminology A node that has subtrees is the parent of the roots of the subtrees. The roots of these subtrees are the

42

void postorder(treePointer ptr)

// postorder tree traversal

{

if (ptr!=NULL) {

postorder(ptr->leftChild);

postorder(ptr->rightChild);

visit(ptr->data);

}

}

A B / C * D * E +

Postorder Traversal (recursive version)

Page 43: No Slide Titlegercal/cs340/slides/review/binary-yolum.pdf · 12 Terminology A node that has subtrees is the parent of the roots of the subtrees. The roots of these subtrees are the

43

O(n)

Iterative Inorder Traversal (using stack)

void iter_inorder(treePointer node)

{

StackType<treePointer> NodeStack;

bool completed=false;

while (!completed) {

while(node!=NULL){

NodeStack.push(node); // add to stack

node=node->leftChild

}

NodeStack.pop(node); // delete from stack

if (node!=NULL){

cout << node->data;

node = node->rightChild;

}

else completed=true;

}

}

Page 44: No Slide Titlegercal/cs340/slides/review/binary-yolum.pdf · 12 Terminology A node that has subtrees is the parent of the roots of the subtrees. The roots of these subtrees are the

44

Call of inorder Value in root Action Call of inorder Value in root Action

1 + 11 C

2 * 12 NULL

3 * 11 C printf

4 / 13 NULL

5 A 2 * printf

6 NULL 14 D

5 A printf 15 NULL

7 NULL 14 D printf

4 / printf 16 NULL

8 B 1 + printf

9 NULL 17 E

8 B printf 18 NULL

10 NULL 17 E printf

3 * printf 19 NULL

Trace Operations of Inorder Traversal

Page 45: No Slide Titlegercal/cs340/slides/review/binary-yolum.pdf · 12 Terminology A node that has subtrees is the parent of the roots of the subtrees. The roots of these subtrees are the

45

Level Order Traversal (using queue)

void level_order(treePointer ptr)

/* level order tree traversal */

{

QueueType<treePointer> NodeQueue;

if (ptr!=NULL) {

NodeQueue.enqueue(ptr);

for (;;) {

NodeQueue.dequeue(ptr);

Page 46: No Slide Titlegercal/cs340/slides/review/binary-yolum.pdf · 12 Terminology A node that has subtrees is the parent of the roots of the subtrees. The roots of these subtrees are the

46

if (ptr!=NULL) {

cout << ptr->data;

if (ptr->leftChild)

NodeQueue.enqueue(ptr->leftChild);

if (ptr->rightChild!=NULL)

NodeQueue.enqueue(ptr->rightChild);

}

else break;

}

}

+ * E * D / C A B

Page 47: No Slide Titlegercal/cs340/slides/review/binary-yolum.pdf · 12 Terminology A node that has subtrees is the parent of the roots of the subtrees. The roots of these subtrees are the

47

+

*

A

*

/

E

D

C

B

inorder traversal

A / B * C * D + E

infix expression

preorder traversal

+ * * / A B C D E

prefix expression

postorder traversal

A B / C * D * E +

postfix expression

level order traversal

+ * E * D / C A B

Arithmetic Expression Using BT

Page 48: No Slide Titlegercal/cs340/slides/review/binary-yolum.pdf · 12 Terminology A node that has subtrees is the parent of the roots of the subtrees. The roots of these subtrees are the

48

A special kind of binary tree in which:

1. Each node contains a distinct data value,

2. The key values in the tree can be compared using

“greater than” and “less than”, and

3. The key value of each node in the tree is

less than every key value in its right subtree, and

greater than every key value in its left subtree.

A Binary Search Tree (BST) is . . .

Page 49: No Slide Titlegercal/cs340/slides/review/binary-yolum.pdf · 12 Terminology A node that has subtrees is the parent of the roots of the subtrees. The roots of these subtrees are the

49

Depends on its key values and their order of insertion.

Insert the elements „J‟ „E‟ „F‟ „T‟ „A‟ in that order.

The first value to be inserted is put into the root node.

Shape of a binary search tree . . .

„J‟

Page 50: No Slide Titlegercal/cs340/slides/review/binary-yolum.pdf · 12 Terminology A node that has subtrees is the parent of the roots of the subtrees. The roots of these subtrees are the

50

Thereafter, each value to be inserted begins by

comparing itself to the value in the root node,

moving left it is less, or moving right if it is greater.

This continues at each level until it can be inserted

as a new leaf.

Inserting „E‟ into the BST

„J‟

„E‟

Page 51: No Slide Titlegercal/cs340/slides/review/binary-yolum.pdf · 12 Terminology A node that has subtrees is the parent of the roots of the subtrees. The roots of these subtrees are the

51

Begin by comparing „F‟ to the value in the root node,

moving left it is less, or moving right if it is greater.

This continues until it can be inserted as a leaf.

Inserting „F‟ into the BST

„J‟

„E‟

„F‟

Page 52: No Slide Titlegercal/cs340/slides/review/binary-yolum.pdf · 12 Terminology A node that has subtrees is the parent of the roots of the subtrees. The roots of these subtrees are the

52

Begin by comparing „T‟ to the value in the root node,

moving left it is less, or moving right if it is greater.

This continues until it can be inserted as a leaf.

Inserting „T‟ into the BST

„J‟

„E‟

„F‟

„T‟

Page 53: No Slide Titlegercal/cs340/slides/review/binary-yolum.pdf · 12 Terminology A node that has subtrees is the parent of the roots of the subtrees. The roots of these subtrees are the

53

Begin by comparing „A‟ to the value in the root node,

moving left it is less, or moving right if it is greater.

This continues until it can be inserted as a leaf.

Inserting „A‟ into the BST

„J‟

„E‟

„F‟

„T‟

„A‟

Page 54: No Slide Titlegercal/cs340/slides/review/binary-yolum.pdf · 12 Terminology A node that has subtrees is the parent of the roots of the subtrees. The roots of these subtrees are the

54

is obtained by inserting

the elements „A‟ „E‟ „F‟ „J‟ „T‟ in that order?

What binary search tree . . .

„A‟

Page 55: No Slide Titlegercal/cs340/slides/review/binary-yolum.pdf · 12 Terminology A node that has subtrees is the parent of the roots of the subtrees. The roots of these subtrees are the

55

obtained by inserting

the elements „A‟ „E‟ „F‟ „J‟ „T‟ in that order.

Binary search tree . . .

„A‟

„E‟

„F‟

„J‟

„T‟

Page 56: No Slide Titlegercal/cs340/slides/review/binary-yolum.pdf · 12 Terminology A node that has subtrees is the parent of the roots of the subtrees. The roots of these subtrees are the

56

Another binary search tree

Add nodes containing these values in this order:

„D‟ „B‟ „L‟ „Q‟ „S‟ „V‟ „Z‟

„J‟

„E‟

„A‟ „H‟

„T‟

„M‟

„K‟ „P‟

Page 57: No Slide Titlegercal/cs340/slides/review/binary-yolum.pdf · 12 Terminology A node that has subtrees is the parent of the roots of the subtrees. The roots of these subtrees are the

57

Is „F‟ in the binary search tree?

„J‟

„E‟

„A‟ „H‟

„T‟

„M‟

„K‟

„V‟

„P‟ „Z‟ „D‟

„Q‟ „L‟ „B‟

„S‟

Page 58: No Slide Titlegercal/cs340/slides/review/binary-yolum.pdf · 12 Terminology A node that has subtrees is the parent of the roots of the subtrees. The roots of these subtrees are the

58

Class TreeType

// Assumptions: Relational operators overloaded

class TreeType

{

public:

// Constructor, destructor, copy constructor

...

// Overloads assignment

...

// Observer functions

...

// Transformer functions

...

// Iterator pair

...

void Print(std::ofstream& outFile) const;

private:

TreeNode* root;

};

Page 59: No Slide Titlegercal/cs340/slides/review/binary-yolum.pdf · 12 Terminology A node that has subtrees is the parent of the roots of the subtrees. The roots of these subtrees are the

59

bool TreeType::IsFull() const

{

NodeType* location;

try

{

location = new NodeType;

delete location;

return false;

}

catch(std::bad_alloc exception)

{

return true;

}

}

bool TreeType::IsEmpty() const

{

return root == NULL;

}

Page 60: No Slide Titlegercal/cs340/slides/review/binary-yolum.pdf · 12 Terminology A node that has subtrees is the parent of the roots of the subtrees. The roots of these subtrees are the

60 60

Tree Recursion

CountNodes Version 1

if (Left(tree) is NULL) AND (Right(tree) is NULL)

return 1

else

return CountNodes(Left(tree)) +

CountNodes(Right(tree)) + 1

What happens when Left(tree) is NULL?

Page 61: No Slide Titlegercal/cs340/slides/review/binary-yolum.pdf · 12 Terminology A node that has subtrees is the parent of the roots of the subtrees. The roots of these subtrees are the

61 61

Tree Recursion

CountNodes Version 2

if (Left(tree) is NULL) AND (Right(tree) is NULL)

return 1

else if Left(tree) is NULL

return CountNodes(Right(tree)) + 1

else if Right(tree) is NULL

return CountNodes(Left(tree)) + 1

else return CountNodes(Left(tree)) + CountNodes(Right(tree)) + 1

What happens when the initial tree is NULL?

Page 62: No Slide Titlegercal/cs340/slides/review/binary-yolum.pdf · 12 Terminology A node that has subtrees is the parent of the roots of the subtrees. The roots of these subtrees are the

62 62

Tree Recursion

CountNodes Version 3

if tree is NULL

return 0

else if (Left(tree) is NULL) AND (Right(tree) is NULL)

return 1

else if Left(tree) is NULL

return CountNodes(Right(tree)) + 1

else if Right(tree) is NULL

return CountNodes(Left(tree)) + 1

else return CountNodes(Left(tree)) +

CountNodes(Right(tree)) + 1

Can we simplify this algorithm?

Page 63: No Slide Titlegercal/cs340/slides/review/binary-yolum.pdf · 12 Terminology A node that has subtrees is the parent of the roots of the subtrees. The roots of these subtrees are the

63

Tree Recursion

CountNodes Version 4

if tree is NULL

return 0

else

return CountNodes(Left(tree)) +

CountNodes(Right(tree)) + 1

Is that all there is?

Page 64: No Slide Titlegercal/cs340/slides/review/binary-yolum.pdf · 12 Terminology A node that has subtrees is the parent of the roots of the subtrees. The roots of these subtrees are the

64

// Implementation of Final Version

int CountNodes(TreeNode* tree); // Pototype

int TreeType::LengthIs() const

// Class member function

{

return CountNodes(root);

}

int CountNodes(TreeNode* tree)

// Recursive function that counts the nodes

{

if (tree == NULL)

return 0;

else

return CountNodes(tree->left) +

CountNodes(tree->right) + 1;

}

Page 65: No Slide Titlegercal/cs340/slides/review/binary-yolum.pdf · 12 Terminology A node that has subtrees is the parent of the roots of the subtrees. The roots of these subtrees are the

65

Retrieval Operation

Page 66: No Slide Titlegercal/cs340/slides/review/binary-yolum.pdf · 12 Terminology A node that has subtrees is the parent of the roots of the subtrees. The roots of these subtrees are the

66

Retrieval Operation

void TreeType::RetrieveItem(ItemType& item, bool& found)

{

Retrieve(root, item, found);

}

void Retrieve(TreeNode* tree,

ItemType& item, bool& found)

{

if (tree == NULL)

found = false;

else if (item < tree->info)

Retrieve(tree->left, item, found);

Page 67: No Slide Titlegercal/cs340/slides/review/binary-yolum.pdf · 12 Terminology A node that has subtrees is the parent of the roots of the subtrees. The roots of these subtrees are the

67

Retrieval Operation, cont.

else if (item > tree->info)

Retrieve(tree->right, item, found);

else

{

item = tree->info;

found = true;

}

}

Page 68: No Slide Titlegercal/cs340/slides/review/binary-yolum.pdf · 12 Terminology A node that has subtrees is the parent of the roots of the subtrees. The roots of these subtrees are the

68

The Insert Operation

A new node is always inserted into its

appropriate position in the tree as a leaf.

Page 69: No Slide Titlegercal/cs340/slides/review/binary-yolum.pdf · 12 Terminology A node that has subtrees is the parent of the roots of the subtrees. The roots of these subtrees are the

69

Insertions into a Binary Search Tree

Page 70: No Slide Titlegercal/cs340/slides/review/binary-yolum.pdf · 12 Terminology A node that has subtrees is the parent of the roots of the subtrees. The roots of these subtrees are the

70

The recursive InsertItem operation

Page 71: No Slide Titlegercal/cs340/slides/review/binary-yolum.pdf · 12 Terminology A node that has subtrees is the parent of the roots of the subtrees. The roots of these subtrees are the

71

The tree parameter

is a pointer within the tree

Page 72: No Slide Titlegercal/cs340/slides/review/binary-yolum.pdf · 12 Terminology A node that has subtrees is the parent of the roots of the subtrees. The roots of these subtrees are the

72

Recursive Insert

void Insert(TreeNode*& tree, ItemType item)

{

if (tree == NULL)

{// Insertion place found.

tree = new TreeNode;

tree->right = NULL;

tree->left = NULL;

tree->info = item;

}

else if (item < tree->info)

Insert(tree->left, item);

else

Insert(tree->right, item);

}

Page 73: No Slide Titlegercal/cs340/slides/review/binary-yolum.pdf · 12 Terminology A node that has subtrees is the parent of the roots of the subtrees. The roots of these subtrees are the

73

Deleting a Leaf Node

Page 74: No Slide Titlegercal/cs340/slides/review/binary-yolum.pdf · 12 Terminology A node that has subtrees is the parent of the roots of the subtrees. The roots of these subtrees are the

74

Deleting a Node with One Child

Page 75: No Slide Titlegercal/cs340/slides/review/binary-yolum.pdf · 12 Terminology A node that has subtrees is the parent of the roots of the subtrees. The roots of these subtrees are the

75

Deleting a Node with Two Children

Page 76: No Slide Titlegercal/cs340/slides/review/binary-yolum.pdf · 12 Terminology A node that has subtrees is the parent of the roots of the subtrees. The roots of these subtrees are the

76

DeleteNode Algorithm

if (Left(tree) is NULL) AND (Right(tree) is NULL)

Set tree to NULL

else if Left(tree) is NULL

Set tree to Right(tree)

else if Right(tree) is NULL

Set tree to Left(tree)

else

Find predecessor

Set Info(tree) to Info(predecessor)

Delete predecessor

Page 77: No Slide Titlegercal/cs340/slides/review/binary-yolum.pdf · 12 Terminology A node that has subtrees is the parent of the roots of the subtrees. The roots of these subtrees are the

77

Code for DeleteNode

void DeleteNode(TreeNode*& tree)

{

ItemType data;

TreeNode* tempPtr;

tempPtr = tree;

if (tree->left == NULL) {

tree = tree->right;

delete tempPtr; }

else if (tree->right == NULL){

tree = tree->left;

delete tempPtr;}

else

{

GetPredecessor(tree->left, data);

tree->info = data;

Delete(tree->left, data);

}

}

Page 78: No Slide Titlegercal/cs340/slides/review/binary-yolum.pdf · 12 Terminology A node that has subtrees is the parent of the roots of the subtrees. The roots of these subtrees are the

78

Definition of Recursive Delete

Definition: Removes item from tree

Size: The number of nodes in the path from the

root to the node to be deleted.

Base Case: If item's key matches key in Info(tree),

delete node pointed to by tree.

General Case: If item < Info(tree),

Delete(Left(tree), item);

else

Delete(Right(tree), item).

Page 79: No Slide Titlegercal/cs340/slides/review/binary-yolum.pdf · 12 Terminology A node that has subtrees is the parent of the roots of the subtrees. The roots of these subtrees are the

79

Code for Recursive Delete

void Delete(TreeNode*& tree, ItemType item)

{

if (item < tree->info)

Delete(tree->left, item);

else if (item > tree->info)

Delete(tree->right, item);

else

DeleteNode(tree); // Node found

}

Page 80: No Slide Titlegercal/cs340/slides/review/binary-yolum.pdf · 12 Terminology A node that has subtrees is the parent of the roots of the subtrees. The roots of these subtrees are the

80

Code for GetPredecessor

void GetPredecessor(TreeNode* tree,

ItemType& data)

{

while (tree->right != NULL)

tree = tree->right;

data = tree->info;

}

Why is the code not recursive?

Page 81: No Slide Titlegercal/cs340/slides/review/binary-yolum.pdf · 12 Terminology A node that has subtrees is the parent of the roots of the subtrees. The roots of these subtrees are the

81

Printing all the Nodes in Order

Page 82: No Slide Titlegercal/cs340/slides/review/binary-yolum.pdf · 12 Terminology A node that has subtrees is the parent of the roots of the subtrees. The roots of these subtrees are the

82

Function Print

Function Print

Definition: Prints the items in the binary search

tree in order from smallest to largest.

Size: The number of nodes in the tree whose

root is tree

Base Case: If tree = NULL, do nothing.

General Case: Traverse the left subtree in order.

Then print Info(tree).

Then traverse the right subtree in order.

Page 83: No Slide Titlegercal/cs340/slides/review/binary-yolum.pdf · 12 Terminology A node that has subtrees is the parent of the roots of the subtrees. The roots of these subtrees are the

83

Code for Recursive InOrder Print

void PrintTree(TreeNode* tree,

std::ofstream& outFile)

{

if (tree != NULL)

{

PrintTree(tree->left, outFile);

outFile << tree->info;

PrintTree(tree->right, outFile);

}

}

Is that all there is?

Page 84: No Slide Titlegercal/cs340/slides/review/binary-yolum.pdf · 12 Terminology A node that has subtrees is the parent of the roots of the subtrees. The roots of these subtrees are the

84

Destructor

void Destroy(TreeNode*& tree);

TreeType::~TreeType()

{

Destroy(root);

}

void Destroy(TreeNode*& tree)

{

if (tree != NULL)

{

Destroy(tree->left);

Destroy(tree->right);

delete tree;

} }

Page 85: No Slide Titlegercal/cs340/slides/review/binary-yolum.pdf · 12 Terminology A node that has subtrees is the parent of the roots of the subtrees. The roots of these subtrees are the

85

Algorithm for Copying a Tree

if (originalTree is NULL)

Set copy to NULL

else

Set Info(copy) to Info(originalTree)

Set Left(copy) to Left(originalTree)

Set Right(copy) to Right(originalTree)

Page 86: No Slide Titlegercal/cs340/slides/review/binary-yolum.pdf · 12 Terminology A node that has subtrees is the parent of the roots of the subtrees. The roots of these subtrees are the

86

Code for CopyTree

void CopyTree(TreeNode*& copy,

const TreeNode* originalTree)

{

if (originalTree == NULL)

copy = NULL;

else

{

copy = new TreeNode;

copy->info = originalTree->info;

CopyTree(copy->left, originalTree->left);

CopyTree(copy->right, originalTree->right);

}

}

Page 87: No Slide Titlegercal/cs340/slides/review/binary-yolum.pdf · 12 Terminology A node that has subtrees is the parent of the roots of the subtrees. The roots of these subtrees are the

87

Inorder(tree)

if tree is not NULL

Inorder(Left(tree))

Visit Info(tree)

Inorder(Right(tree))

To print in alphabetical order

Page 88: No Slide Titlegercal/cs340/slides/review/binary-yolum.pdf · 12 Terminology A node that has subtrees is the parent of the roots of the subtrees. The roots of these subtrees are the

88

Postorder(tree)

if tree is not NULL

Postorder(Left(tree))

Postorder(Right(tree))

Visit Info(tree)

Visits leaves first

(good for deletion)

Page 89: No Slide Titlegercal/cs340/slides/review/binary-yolum.pdf · 12 Terminology A node that has subtrees is the parent of the roots of the subtrees. The roots of these subtrees are the

89

Preorder(tree)

if tree is not NULL

Visit Info(tree)

Preorder(Left(tree))

Preorder(Right(tree))

Useful with binary trees

(not binary search trees)

Page 90: No Slide Titlegercal/cs340/slides/review/binary-yolum.pdf · 12 Terminology A node that has subtrees is the parent of the roots of the subtrees. The roots of these subtrees are the

90

Three Tree Traversals

Page 91: No Slide Titlegercal/cs340/slides/review/binary-yolum.pdf · 12 Terminology A node that has subtrees is the parent of the roots of the subtrees. The roots of these subtrees are the

91

Our Iteration Approach

The client program passes the ResetTree and

GetNextItem functions a parameter indicating

which of the three traversals to use

ResetTree generates a queues of node contents in the indicated order

GetNextItem processes the node contents from the appropriate queue: inQue, preQue, postQue.

Page 92: No Slide Titlegercal/cs340/slides/review/binary-yolum.pdf · 12 Terminology A node that has subtrees is the parent of the roots of the subtrees. The roots of these subtrees are the

92

Code for ResetTree

void TreeType::ResetTree(OrderType order)

// Calls function to create a queue of the tree

// elements in the desired order.

{

switch (order)

{

case PRE_ORDER : PreOrder(root, preQue);

break;

case IN_ORDER : InOrder(root, inQue);

break;

case POST_ORDER: PostOrder(root, postQue);

break;

}

}

Page 93: No Slide Titlegercal/cs340/slides/review/binary-yolum.pdf · 12 Terminology A node that has subtrees is the parent of the roots of the subtrees. The roots of these subtrees are the

93

void TreeType::GetNextItem(ItemType& item,

OrderType order,bool& finished)

{

finished = false;

switch (order)

{

case PRE_ORDER : preQue.Dequeue(item);

if (preQue.IsEmpty())

finished = true;

break;

case IN_ORDER : inQue.Dequeue(item);

if (inQue.IsEmpty())

finished = true;

break;

case POST_ORDER: postQue.Dequeue(item);

if (postQue.IsEmpty())

finished = true;

break;

}

}

Code for GetNextItem

Page 94: No Slide Titlegercal/cs340/slides/review/binary-yolum.pdf · 12 Terminology A node that has subtrees is the parent of the roots of the subtrees. The roots of these subtrees are the

94

Iterative Versions

FindNode

Set nodePtr to tree

Set parentPtr to NULL

Set found to false

while more elements to search AND NOT found

if item < Info(nodePtr)

Set parentPtr to nodePtr

Set nodePtr to Left(nodePtr)

else if item > Info(nodePtr)

Set parentPtr to nodePtr

Set nodePtr to Right(nodePtr)

else

Set found to true

Page 95: No Slide Titlegercal/cs340/slides/review/binary-yolum.pdf · 12 Terminology A node that has subtrees is the parent of the roots of the subtrees. The roots of these subtrees are the

95

void FindNode(TreeNode* tree, ItemType item,

TreeNode*& nodePtr, TreeNode*& parentPtr)

{

nodePtr = tree;

parentPtr = NULL;

bool found = false;

while (nodePtr != NULL && !found)

{ if (item < nodePtr->info)

{

parentPtr = nodePtr;

nodePtr = nodePtr->left;

}

else if (item > nodePtr->info)

{

parentPtr = nodePtr;

nodePtr = nodePtr->right;

}

else found = true;

}

}

Code for

FindNode

Page 96: No Slide Titlegercal/cs340/slides/review/binary-yolum.pdf · 12 Terminology A node that has subtrees is the parent of the roots of the subtrees. The roots of these subtrees are the

96

InsertItem

Create a node to contain the new item.

Find the insertion place.

Attach new node.

Find the insertion place

FindNode(tree, item, nodePtr, parentPtr);

Page 97: No Slide Titlegercal/cs340/slides/review/binary-yolum.pdf · 12 Terminology A node that has subtrees is the parent of the roots of the subtrees. The roots of these subtrees are the

97

Using function FindNode to find

the insertion point

Page 98: No Slide Titlegercal/cs340/slides/review/binary-yolum.pdf · 12 Terminology A node that has subtrees is the parent of the roots of the subtrees. The roots of these subtrees are the

98

Using function FindNode to find

the insertion point

Page 99: No Slide Titlegercal/cs340/slides/review/binary-yolum.pdf · 12 Terminology A node that has subtrees is the parent of the roots of the subtrees. The roots of these subtrees are the

99

Using function FindNode to find

the insertion point

Page 100: No Slide Titlegercal/cs340/slides/review/binary-yolum.pdf · 12 Terminology A node that has subtrees is the parent of the roots of the subtrees. The roots of these subtrees are the

100

Using function FindNode to find

the insertion point

Page 101: No Slide Titlegercal/cs340/slides/review/binary-yolum.pdf · 12 Terminology A node that has subtrees is the parent of the roots of the subtrees. The roots of these subtrees are the

101

Using function FindNode to find

the insertion point

Page 102: No Slide Titlegercal/cs340/slides/review/binary-yolum.pdf · 12 Terminology A node that has subtrees is the parent of the roots of the subtrees. The roots of these subtrees are the

102

AttachNewNode

if item < Info(parentPtr)

Set Left(parentPtr) to newNode

else

Set Right(parentPtr) to newNode

Page 103: No Slide Titlegercal/cs340/slides/review/binary-yolum.pdf · 12 Terminology A node that has subtrees is the parent of the roots of the subtrees. The roots of these subtrees are the

103

AttachNewNode(revised)

if parentPtr equals NULL

Set tree to newNode

else if item < Info(parentPtr)

Set Left(parentPtr) to newNode

else

Set Right(parentPtr) to newNode

Page 104: No Slide Titlegercal/cs340/slides/review/binary-yolum.pdf · 12 Terminology A node that has subtrees is the parent of the roots of the subtrees. The roots of these subtrees are the

104

Code for InsertItem

void TreeType::InsertItem(ItemType item)

{

TreeNode* newNode;

TreeNode* nodePtr;

TreeNode* parentPtr;

newNode = new TreeNode;

newNode->info = item;

newNode->left = NULL;

newNode->right = NULL;

FindNode(root, item, nodePtr, parentPtr);

if (parentPtr == NULL)

root = newNode;

else if (item < parentPtr->info)

parentPtr->left = newNode;

else parentPtr->right = newNode;

}

Page 105: No Slide Titlegercal/cs340/slides/review/binary-yolum.pdf · 12 Terminology A node that has subtrees is the parent of the roots of the subtrees. The roots of these subtrees are the

105

Code for DeleteItem

void TreeType::DeleteItem(ItemType item)

{

TreeNode* nodePtr;

TreeNode* parentPtr;

FindNode(root, item, nodePtr, parentPtr);

if (nodePtr == root)

DeleteNode(root);

else

if (parentPtr->left == nodePtr)

DeleteNode(parentPtr->left);

else DeleteNode(parentPtr->right);

}

Page 106: No Slide Titlegercal/cs340/slides/review/binary-yolum.pdf · 12 Terminology A node that has subtrees is the parent of the roots of the subtrees. The roots of these subtrees are the

106

PointersnodePtr and parentPtr

Are External to the Tree

Page 107: No Slide Titlegercal/cs340/slides/review/binary-yolum.pdf · 12 Terminology A node that has subtrees is the parent of the roots of the subtrees. The roots of these subtrees are the

107

Pointer parentPtr is External to the Tree, but

parentPtr-> left is an Actual Pointer in the Tree

Page 108: No Slide Titlegercal/cs340/slides/review/binary-yolum.pdf · 12 Terminology A node that has subtrees is the parent of the roots of the subtrees. The roots of these subtrees are the

108

A Binary Search Tree Stored in an

Array with Dummy Values