Top Banner
CIS210 1 Topic Binary Trees (Non-Linear Data Structures)
113

Binary Trees (Non-Linear Data Structures)hilltop.bradley.edu/~young/CIS210old/topic08.pdf · CIS210 58 Operations on Binary Trees Create an empty binary tree. Create a one-node binary

May 21, 2020

Download

Documents

dariahiddleston
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Binary Trees (Non-Linear Data Structures)hilltop.bradley.edu/~young/CIS210old/topic08.pdf · CIS210 58 Operations on Binary Trees Create an empty binary tree. Create a one-node binary

CIS210 1

Topic

Binary Trees

(Non-Linear Data

Structures)

Page 2: Binary Trees (Non-Linear Data Structures)hilltop.bradley.edu/~young/CIS210old/topic08.pdf · CIS210 58 Operations on Binary Trees Create an empty binary tree. Create a one-node binary

CIS210 2

Linear Data Structures

Arrays

Linked lists

Skip lists

Self-organizing lists

Page 3: Binary Trees (Non-Linear Data Structures)hilltop.bradley.edu/~young/CIS210old/topic08.pdf · CIS210 58 Operations on Binary Trees Create an empty binary tree. Create a one-node binary

CIS210 3

Non-Linear Data Structures

Hierarchical representation?

Trees

General Trees

Binary Trees

Search Trees

Balanced Trees

Heaps

..

Page 4: Binary Trees (Non-Linear Data Structures)hilltop.bradley.edu/~young/CIS210old/topic08.pdf · CIS210 58 Operations on Binary Trees Create an empty binary tree. Create a one-node binary

CIS210 4

General Trees

Page 5: Binary Trees (Non-Linear Data Structures)hilltop.bradley.edu/~young/CIS210old/topic08.pdf · CIS210 58 Operations on Binary Trees Create an empty binary tree. Create a one-node binary

CIS210 5

A (Rooted) Tree?

A finite, nonempty set of nodes and edges s.t.

One special node (the root of the tree)

Each node may be associated (edge) with one or more

different nodes (its children).

Each node except the root has exactly one parent. The

root node has no parent (no incoming edge).

There exists a unique path from the root to any

other node!

Page 6: Binary Trees (Non-Linear Data Structures)hilltop.bradley.edu/~young/CIS210old/topic08.pdf · CIS210 58 Operations on Binary Trees Create an empty binary tree. Create a one-node binary

CIS210 6

General Rooted Trees

root

. . .

Page 7: Binary Trees (Non-Linear Data Structures)hilltop.bradley.edu/~young/CIS210old/topic08.pdf · CIS210 58 Operations on Binary Trees Create an empty binary tree. Create a one-node binary

CIS210 7

Example: (General Rooted) Trees?

Page 8: Binary Trees (Non-Linear Data Structures)hilltop.bradley.edu/~young/CIS210old/topic08.pdf · CIS210 58 Operations on Binary Trees Create an empty binary tree. Create a one-node binary

CIS210 8

More Terminology

Path

A sequence of nodes.

Level of a node

Height of a node

The number of nodes in the longest path from that

node to a leaf.

Height of a tree

The height of the root node.

Page 9: Binary Trees (Non-Linear Data Structures)hilltop.bradley.edu/~young/CIS210old/topic08.pdf · CIS210 58 Operations on Binary Trees Create an empty binary tree. Create a one-node binary

CIS210 9

An N-ary Tree?

Each node may be associated (edge) with exactly N

different nodes (its children).

If the set is empty (no node), then an empty N-ary

tree.

Page 10: Binary Trees (Non-Linear Data Structures)hilltop.bradley.edu/~young/CIS210old/topic08.pdf · CIS210 58 Operations on Binary Trees Create an empty binary tree. Create a one-node binary

CIS210 10

Example: N-ary Trees (N=3)

= Empty tree!* Note:

Page 11: Binary Trees (Non-Linear Data Structures)hilltop.bradley.edu/~young/CIS210old/topic08.pdf · CIS210 58 Operations on Binary Trees Create an empty binary tree. Create a one-node binary

CIS210 11

The World of Trees

General Trees

N-ary Trees

Exact N children

Page 12: Binary Trees (Non-Linear Data Structures)hilltop.bradley.edu/~young/CIS210old/topic08.pdf · CIS210 58 Operations on Binary Trees Create an empty binary tree. Create a one-node binary

CIS210 12

An Ordered Tree?

A rooted tree in which the children of each node are

ordered.

first child, second child, third child, etc. …

Most practical implementations of trees define an

implicit ordering of the subtrees.

Page 13: Binary Trees (Non-Linear Data Structures)hilltop.bradley.edu/~young/CIS210old/topic08.pdf · CIS210 58 Operations on Binary Trees Create an empty binary tree. Create a one-node binary

CIS210 13

Example: Ordered Trees

Page 14: Binary Trees (Non-Linear Data Structures)hilltop.bradley.edu/~young/CIS210old/topic08.pdf · CIS210 58 Operations on Binary Trees Create an empty binary tree. Create a one-node binary

CIS210 14

Different Views on Trees

We may view trees as

A mathematical construct.

A data structure.

An abstract data type.

Page 15: Binary Trees (Non-Linear Data Structures)hilltop.bradley.edu/~young/CIS210old/topic08.pdf · CIS210 58 Operations on Binary Trees Create an empty binary tree. Create a one-node binary

CIS210 15

Binary Trees

Page 16: Binary Trees (Non-Linear Data Structures)hilltop.bradley.edu/~young/CIS210old/topic08.pdf · CIS210 58 Operations on Binary Trees Create an empty binary tree. Create a one-node binary

CIS210 16

Binary Trees

A binary tree is an ordered N-ary

tree where N=2!

TrightTleft

root

Page 17: Binary Trees (Non-Linear Data Structures)hilltop.bradley.edu/~young/CIS210old/topic08.pdf · CIS210 58 Operations on Binary Trees Create an empty binary tree. Create a one-node binary

CIS210 17

Example: Binary Trees

= Empty tree!* Note:

Page 18: Binary Trees (Non-Linear Data Structures)hilltop.bradley.edu/~young/CIS210old/topic08.pdf · CIS210 58 Operations on Binary Trees Create an empty binary tree. Create a one-node binary

CIS210 18

Example: Binary Trees?

Page 19: Binary Trees (Non-Linear Data Structures)hilltop.bradley.edu/~young/CIS210old/topic08.pdf · CIS210 58 Operations on Binary Trees Create an empty binary tree. Create a one-node binary

CIS210 19

Two Different Binary Trees

Page 20: Binary Trees (Non-Linear Data Structures)hilltop.bradley.edu/~young/CIS210old/topic08.pdf · CIS210 58 Operations on Binary Trees Create an empty binary tree. Create a one-node binary

CIS210 20

Quiz

How many different binary trees with 3 nodes?

5

How many different binary trees with 4 nodes?

14

Page 21: Binary Trees (Non-Linear Data Structures)hilltop.bradley.edu/~young/CIS210old/topic08.pdf · CIS210 58 Operations on Binary Trees Create an empty binary tree. Create a one-node binary

CIS210 21

Quiz

What is the range of possible heights of a binary tree

with 3 nodes?

2 to 3

What is the range of possible heights of a binary tree

with 100 nodes?

7 to 100

Page 22: Binary Trees (Non-Linear Data Structures)hilltop.bradley.edu/~young/CIS210old/topic08.pdf · CIS210 58 Operations on Binary Trees Create an empty binary tree. Create a one-node binary

CIS210 22

The World of Trees

General Trees

N-ary Trees

Binary Trees

N=2

Page 23: Binary Trees (Non-Linear Data Structures)hilltop.bradley.edu/~young/CIS210old/topic08.pdf · CIS210 58 Operations on Binary Trees Create an empty binary tree. Create a one-node binary

CIS210 23

Different Shapes of Binary Trees

Short & Fat binary trees

A full binary tree

A complete binary tree

A balanced binary tree

Tall & Skinny binary trees

A skewed binary tree

Page 24: Binary Trees (Non-Linear Data Structures)hilltop.bradley.edu/~young/CIS210old/topic08.pdf · CIS210 58 Operations on Binary Trees Create an empty binary tree. Create a one-node binary

CIS210 24

A Full Binary Tree

A binary tree in which

All of the leaves are on the same level. (at level h for

the binary tree of height h.)

Every nonleaf node has exactly two children.

Page 25: Binary Trees (Non-Linear Data Structures)hilltop.bradley.edu/~young/CIS210old/topic08.pdf · CIS210 58 Operations on Binary Trees Create an empty binary tree. Create a one-node binary

CIS210 25

Shape of A Full Binary Tree

The basic shape of a full binary tree is triangular!

Page 26: Binary Trees (Non-Linear Data Structures)hilltop.bradley.edu/~young/CIS210old/topic08.pdf · CIS210 58 Operations on Binary Trees Create an empty binary tree. Create a one-node binary

CIS210 26

Example: Full Binary Trees

Page 27: Binary Trees (Non-Linear Data Structures)hilltop.bradley.edu/~young/CIS210old/topic08.pdf · CIS210 58 Operations on Binary Trees Create an empty binary tree. Create a one-node binary

CIS210 27

A Complete Binary Tree

A binary that is

Either full binary tree

Or full through the next-to-last level, with the leaves

on the last level as far to the left as possible (filled in

from left to right).

A binary tree in which all leaf nodes are at level n or

n-1, and all leaves at level n are towards the left.

A binary tree of height h that is full to level h-1 and

has level h filled from left to right.

Page 28: Binary Trees (Non-Linear Data Structures)hilltop.bradley.edu/~young/CIS210old/topic08.pdf · CIS210 58 Operations on Binary Trees Create an empty binary tree. Create a one-node binary

CIS210 28

Shape of A Complete Binary Tree

The basic shape of a complete binary tree is like

Page 29: Binary Trees (Non-Linear Data Structures)hilltop.bradley.edu/~young/CIS210old/topic08.pdf · CIS210 58 Operations on Binary Trees Create an empty binary tree. Create a one-node binary

CIS210 29

Example: Complete Binary Trees?

Page 30: Binary Trees (Non-Linear Data Structures)hilltop.bradley.edu/~young/CIS210old/topic08.pdf · CIS210 58 Operations on Binary Trees Create an empty binary tree. Create a one-node binary

CIS210 30

Example: Complete Binary Trees?

Page 31: Binary Trees (Non-Linear Data Structures)hilltop.bradley.edu/~young/CIS210old/topic08.pdf · CIS210 58 Operations on Binary Trees Create an empty binary tree. Create a one-node binary

CIS210 31

A (Height) Balanced Binary Tree

A binary tree in which the left and right subtrees of

any node have heights that differ by at most 1.

Page 32: Binary Trees (Non-Linear Data Structures)hilltop.bradley.edu/~young/CIS210old/topic08.pdf · CIS210 58 Operations on Binary Trees Create an empty binary tree. Create a one-node binary

CIS210 32

Example: (Height) Balanced Binary Trees

Page 33: Binary Trees (Non-Linear Data Structures)hilltop.bradley.edu/~young/CIS210old/topic08.pdf · CIS210 58 Operations on Binary Trees Create an empty binary tree. Create a one-node binary

CIS210 33

A Skewed Binary Tree

A degenerate binary tree

A skewed binary tree is expensive to process!

Page 34: Binary Trees (Non-Linear Data Structures)hilltop.bradley.edu/~young/CIS210old/topic08.pdf · CIS210 58 Operations on Binary Trees Create an empty binary tree. Create a one-node binary

CIS210 34

Example: Skewed Binary Trees

Page 35: Binary Trees (Non-Linear Data Structures)hilltop.bradley.edu/~young/CIS210old/topic08.pdf · CIS210 58 Operations on Binary Trees Create an empty binary tree. Create a one-node binary

CIS210 35

Quiz

Is a full binary tree complete?

Yes!

Is a complete binary tree full?

No!

Is a full binary tree balanced?

Yes!

Is a complete binary tree balanced?

Yes!

Page 36: Binary Trees (Non-Linear Data Structures)hilltop.bradley.edu/~young/CIS210old/topic08.pdf · CIS210 58 Operations on Binary Trees Create an empty binary tree. Create a one-node binary

CIS210 36

Properties of Binary Trees

What is the maximum height of a binary tree with n

nodes?

n

The maximum height of a binary tree

with n nodes is n.

Page 37: Binary Trees (Non-Linear Data Structures)hilltop.bradley.edu/~young/CIS210old/topic08.pdf · CIS210 58 Operations on Binary Trees Create an empty binary tree. Create a one-node binary

CIS210 37

Properties of Binary Trees

What is the minimum height of a binary tree with n

nodes?

log (n+1) where the ceiling of log (n+1) = log (n+1)

rounded up.

The minimum height of a binary tree

with n nodes is log (n+1) . (The ceiling of log (n+1) = log (n+1) rounded up.)

Page 38: Binary Trees (Non-Linear Data Structures)hilltop.bradley.edu/~young/CIS210old/topic08.pdf · CIS210 58 Operations on Binary Trees Create an empty binary tree. Create a one-node binary

CIS210 38

Properties of Binary Trees

The height of a binary tree with n nodes is

at least log (n+1) and at most n.

Page 39: Binary Trees (Non-Linear Data Structures)hilltop.bradley.edu/~young/CIS210old/topic08.pdf · CIS210 58 Operations on Binary Trees Create an empty binary tree. Create a one-node binary

CIS210 39

Properties of Binary Trees

What is the minimum number of nodes that a binary

tree of height h can have?

h

The minimum number of nodes that a

binary tree of height h can have is h.

Page 40: Binary Trees (Non-Linear Data Structures)hilltop.bradley.edu/~young/CIS210old/topic08.pdf · CIS210 58 Operations on Binary Trees Create an empty binary tree. Create a one-node binary

CIS210 40

Properties of Binary Trees

What is the maximum number of nodes that a full

binary tree of height h can have?

2h - 1

The maximum number of nodes that a

binary tree of height h can have is 2h - 1.

Page 41: Binary Trees (Non-Linear Data Structures)hilltop.bradley.edu/~young/CIS210old/topic08.pdf · CIS210 58 Operations on Binary Trees Create an empty binary tree. Create a one-node binary

CIS210 41

Properties of Binary Trees

Full binary trees and complete binary trees have

the minimum height!

Skewed (degenerate) binary trees have the

maximum height!

Page 42: Binary Trees (Non-Linear Data Structures)hilltop.bradley.edu/~young/CIS210old/topic08.pdf · CIS210 58 Operations on Binary Trees Create an empty binary tree. Create a one-node binary

CIS210 42

Representation of Binary Trees

Page 43: Binary Trees (Non-Linear Data Structures)hilltop.bradley.edu/~young/CIS210old/topic08.pdf · CIS210 58 Operations on Binary Trees Create an empty binary tree. Create a one-node binary

CIS210 43

How to Represent a Binary Tree?

An array-based representation

An array-based representation for complete binary

trees

A pointer-based representation

Page 44: Binary Trees (Non-Linear Data Structures)hilltop.bradley.edu/~young/CIS210old/topic08.pdf · CIS210 58 Operations on Binary Trees Create an empty binary tree. Create a one-node binary

CIS210 44

1. An Array-Based Representation

Represent a node in the binary tree as a structure

A data

Two indexes (One for each child)

Represent the binary tree as an array of structures.

Page 45: Binary Trees (Non-Linear Data Structures)hilltop.bradley.edu/~young/CIS210old/topic08.pdf · CIS210 58 Operations on Binary Trees Create an empty binary tree. Create a one-node binary

CIS210 45

An Array-Based Representation

const int MAX_NODES = 100;

struct BTnode

{ typedef string DataType;

DataType Data;

int Lchild;

int Rchild;

};

BTnode BT[MAX_NODES];

int Root;

int Free;

Page 46: Binary Trees (Non-Linear Data Structures)hilltop.bradley.edu/~young/CIS210old/topic08.pdf · CIS210 58 Operations on Binary Trees Create an empty binary tree. Create a one-node binary

CIS210 46

An Array-Based Representation

Jane

Bob Tom

Alan Ellen Nancy

0

1

2

3

4

5

6

.

.

.

Data Lchild Rchild Root

0

6

Free

Jane 1 2

Bob 3 4

Tom 5 -1

Alan -1 -1

Ellen -1 -1

Nancy -1 -1

BT

Page 47: Binary Trees (Non-Linear Data Structures)hilltop.bradley.edu/~young/CIS210old/topic08.pdf · CIS210 58 Operations on Binary Trees Create an empty binary tree. Create a one-node binary

CIS210 47

2. An Array-Based Representation of a Complete

Binary Tree

A better array-based representation for a complete

binary tree!

Represent a node in the binary tree as

A data

Represent the binary tree as an array.

Page 48: Binary Trees (Non-Linear Data Structures)hilltop.bradley.edu/~young/CIS210old/topic08.pdf · CIS210 58 Operations on Binary Trees Create an empty binary tree. Create a one-node binary

CIS210 48

An Array-Based Representation of a Complete Binary Tree

const int MAX_NODES = 100;

typedef string DataType;

DataType BT[MAX_NODES];

int Root;

Page 49: Binary Trees (Non-Linear Data Structures)hilltop.bradley.edu/~young/CIS210old/topic08.pdf · CIS210 58 Operations on Binary Trees Create an empty binary tree. Create a one-node binary

CIS210 49

An Array-Based Representation of a Complete Binary Tree

Jane

Bob Tom

Alan Ellen Nancy

0

1

2

3

4

5

6

.

.

.

Data

Jane

Bob

Tom

Alan

Ellen

Nancy

Root

BT[0]

BT

Page 50: Binary Trees (Non-Linear Data Structures)hilltop.bradley.edu/~young/CIS210old/topic08.pdf · CIS210 58 Operations on Binary Trees Create an empty binary tree. Create a one-node binary

CIS210 50

An Array-Based Representation of a Complete Binary Tree

Any node BT[ i ]

Its left child =

BT [ 2 * i + 1 ]

Its left child =

BT [ 2 * i + 2 ]

Its parent =

BT [ (i -1) / 2 ]

Must maintain it as a complete binary tree!

Limited delete!

Page 51: Binary Trees (Non-Linear Data Structures)hilltop.bradley.edu/~young/CIS210old/topic08.pdf · CIS210 58 Operations on Binary Trees Create an empty binary tree. Create a one-node binary

CIS210 51

3. A Pointer-Based Representation

Represent a node in the binary tree as a structure

A data

Two pointers (One for each child)

Represent the binary tree as a linked structure.

Page 52: Binary Trees (Non-Linear Data Structures)hilltop.bradley.edu/~young/CIS210old/topic08.pdf · CIS210 58 Operations on Binary Trees Create an empty binary tree. Create a one-node binary

CIS210 52

A Pointer-Based Representation

Jane

Bob Tom

Alan Ellen Nancy

Root

Jane

Bob Tom

Alan Ellen Nancy

Page 53: Binary Trees (Non-Linear Data Structures)hilltop.bradley.edu/~young/CIS210old/topic08.pdf · CIS210 58 Operations on Binary Trees Create an empty binary tree. Create a one-node binary

CIS210 53

A Pointer-Based Representation

struct BTnode

{ typedef string DataType;

DataType Data;

BTnode* LchildPtr;

BTnode* RchildPtr;

};

BTnode* rootBT;

Page 54: Binary Trees (Non-Linear Data Structures)hilltop.bradley.edu/~young/CIS210old/topic08.pdf · CIS210 58 Operations on Binary Trees Create an empty binary tree. Create a one-node binary

CIS210 54

A Pointer-Based Representation using Template

template<class DataType>

struct BTnode

{

DataType Data;

BTnode<DataType>* LchildPtr;

BTnode<DataType>* RchildPtr;

};

BTnode<DataType>* rootBT;

Page 55: Binary Trees (Non-Linear Data Structures)hilltop.bradley.edu/~young/CIS210old/topic08.pdf · CIS210 58 Operations on Binary Trees Create an empty binary tree. Create a one-node binary

CIS210 55

A Pointer-Based Representation using Template Class

template<class DataType>

class BTnode

{

Public:

BTnode();

BTnode(DataType D, BTnode<DataType>* l,

BTnode<DataType>* r)

:data(D), LchildPtr(l),

RchildPtr(r) { }

friend class BT<DataType>;

private:

DataType data;

BTnode<DataType>* LchildPtr;

BTnode<DataType>* RchildPtr;

};

Page 56: Binary Trees (Non-Linear Data Structures)hilltop.bradley.edu/~young/CIS210old/topic08.pdf · CIS210 58 Operations on Binary Trees Create an empty binary tree. Create a one-node binary

CIS210 56

A Pointer-Based Representation using Template Class

template<class DataType>

class BT

{

Public:

BT();

private:

BTnode<DataType>* rootBT;

};

Page 57: Binary Trees (Non-Linear Data Structures)hilltop.bradley.edu/~young/CIS210old/topic08.pdf · CIS210 58 Operations on Binary Trees Create an empty binary tree. Create a one-node binary

CIS210 57

Binary Trees as ADTs

Page 58: Binary Trees (Non-Linear Data Structures)hilltop.bradley.edu/~young/CIS210old/topic08.pdf · CIS210 58 Operations on Binary Trees Create an empty binary tree. Create a one-node binary

CIS210 58

Operations on Binary Trees

Create an empty binary tree.

Create a one-node binary tree, given an item.

Create a binary tree, given an item for its root and

two binary trees for the root’s left and right subtrees.

Attach a left or right child to the binary tree’s root.

Attach a left or right subtree to the binary tree’s root.

Detach a left or right subtree to the binary tree’s root.

Destroy a binary tree.

Page 59: Binary Trees (Non-Linear Data Structures)hilltop.bradley.edu/~young/CIS210old/topic08.pdf · CIS210 58 Operations on Binary Trees Create an empty binary tree. Create a one-node binary

CIS210 59

More Operations on Binary Trees

Determine whether a binary tree empty?

Determine or change the data in the binary tree’s

root.

Return a copy of the left or right subtree of the

binary tree’s root.

Traverse the nodes in a binary tree in preorder,

inorder or postorder.

...

Page 60: Binary Trees (Non-Linear Data Structures)hilltop.bradley.edu/~young/CIS210old/topic08.pdf · CIS210 58 Operations on Binary Trees Create an empty binary tree. Create a one-node binary

CIS210 60

Traversal Operations on Binary Trees

It is frequently necessary to examine every node

exactly once in a binary tree.

Binary tree traversal is the process of

Visiting systematically all the nodes of a binary tree

and

Performing a task (calling a visit procedure like

print).

Page 61: Binary Trees (Non-Linear Data Structures)hilltop.bradley.edu/~young/CIS210old/topic08.pdf · CIS210 58 Operations on Binary Trees Create an empty binary tree. Create a one-node binary

CIS210 61

Traversal Operations on Binary Trees

Two essential approaches:

Depth-first traversal

Breadth-first traversal

Page 62: Binary Trees (Non-Linear Data Structures)hilltop.bradley.edu/~young/CIS210old/topic08.pdf · CIS210 58 Operations on Binary Trees Create an empty binary tree. Create a one-node binary

CIS210 62

Possible Depth-First Traversals

Six possible ways to arrange those tasks:

1. Process a node, then left-child subtree, then right-child subtree.

2. Process left-child subtree, then a node, then right-child subtree.

3. Process left-child subtree, then right-child subtree, then a node.

4. Process a node, then right -child subtree, then left-child subtree.

5. Process right -child subtree, then a node, then left-child subtree.

6. Process right -child subtree, then left-child subtree, then a node.

In almost all cases, the subtrees are analyzed left to right!

Page 63: Binary Trees (Non-Linear Data Structures)hilltop.bradley.edu/~young/CIS210old/topic08.pdf · CIS210 58 Operations on Binary Trees Create an empty binary tree. Create a one-node binary

CIS210 63

Common Binary Tree Traversals

Three ways to arrange those tasks:

1. Process a node, then its left-child subtree, then its

right-child subtree.

Preorder Traversal

2. Process its left-child subtree, then a node, then its

right-child subtree.

Inorder Traversal

3. Process its left-child subtree, then its right-child

subtree, then a node.

Postorder Traversal

Page 64: Binary Trees (Non-Linear Data Structures)hilltop.bradley.edu/~young/CIS210old/topic08.pdf · CIS210 58 Operations on Binary Trees Create an empty binary tree. Create a one-node binary

CIS210 64

1. Pre-order Traversal

If the tree is not empty then

Visit the root

Preorder traverse the left subtree recursively

Preorder traverse the right subtree recursively

Page 65: Binary Trees (Non-Linear Data Structures)hilltop.bradley.edu/~young/CIS210old/topic08.pdf · CIS210 58 Operations on Binary Trees Create an empty binary tree. Create a one-node binary

CIS210 65

Pre-order Traversal - Processing Order

1

2 5

3 4 76

Page 66: Binary Trees (Non-Linear Data Structures)hilltop.bradley.edu/~young/CIS210old/topic08.pdf · CIS210 58 Operations on Binary Trees Create an empty binary tree. Create a one-node binary

CIS210 66

Example: Preorder traversal

A

B D

I

C HE

GF

A B C D E F G H I

Page 67: Binary Trees (Non-Linear Data Structures)hilltop.bradley.edu/~young/CIS210old/topic08.pdf · CIS210 58 Operations on Binary Trees Create an empty binary tree. Create a one-node binary

CIS210 67

Preorder Traversal & Print

void preorder_print(BTnode* rootBT)

{

if (rootBT != NULL)

{

cout << rootBT->Data << endl;

preorder_print(rootBT-> LchildPtr);

preorder_print(rootBT-> RchildPtr);

}

}

Page 68: Binary Trees (Non-Linear Data Structures)hilltop.bradley.edu/~young/CIS210old/topic08.pdf · CIS210 58 Operations on Binary Trees Create an empty binary tree. Create a one-node binary

CIS210 68

Preorder Traversal and Operation

typedef void (*fType)(DataType& AnItem);

void preorder(BTnode* rootBT, fType Op)

{

if (rootBT != NULL)

{

Op(rootBT->Data);

preorder(rootBT-> LchildPtr);

preorder(rootBT-> RchildPtr);

}

}

Page 69: Binary Trees (Non-Linear Data Structures)hilltop.bradley.edu/~young/CIS210old/topic08.pdf · CIS210 58 Operations on Binary Trees Create an empty binary tree. Create a one-node binary

CIS210 69

2. In-order Traversal

If the tree is not empty then

Inorder traverse the left subtree recursively

Visit the root

Inorder traverse the right subtree recursively

Page 70: Binary Trees (Non-Linear Data Structures)hilltop.bradley.edu/~young/CIS210old/topic08.pdf · CIS210 58 Operations on Binary Trees Create an empty binary tree. Create a one-node binary

CIS210 70

In-order Traversal - Processing Order

4

2 6

1 3 75

Page 71: Binary Trees (Non-Linear Data Structures)hilltop.bradley.edu/~young/CIS210old/topic08.pdf · CIS210 58 Operations on Binary Trees Create an empty binary tree. Create a one-node binary

CIS210 71

Example: Inorder traversal

A

B D

I

C HE

GF

B C A F E G D I H

Page 72: Binary Trees (Non-Linear Data Structures)hilltop.bradley.edu/~young/CIS210old/topic08.pdf · CIS210 58 Operations on Binary Trees Create an empty binary tree. Create a one-node binary

CIS210 72

Inorder Traversal & Print

void inorder_print(BTnode* rootBT)

{

if (rootBT != NULL)

{

inorder_print(rootBT-> LchildPtr);

cout << rootBT->Data << endl;

inorder_print(rootBT-> RchildPtr);

}

}

Page 73: Binary Trees (Non-Linear Data Structures)hilltop.bradley.edu/~young/CIS210old/topic08.pdf · CIS210 58 Operations on Binary Trees Create an empty binary tree. Create a one-node binary

CIS210 73

Inorder Traversal and Operation

typedef void (*fType)(DataType& AnItem);

void inorder(BTnode* rootBT, fType Op)

{

if (rootBT != NULL)

{

inorder(rootBT-> LchildPtr);

Op(rootBT->Data);

inorder(rootBT-> RchildPtr);

}

}

Page 74: Binary Trees (Non-Linear Data Structures)hilltop.bradley.edu/~young/CIS210old/topic08.pdf · CIS210 58 Operations on Binary Trees Create an empty binary tree. Create a one-node binary

CIS210 74

3. Post-order Traversal

If the tree is not empty then

Postorder traverse the left subtree recursively

Postorder traverse the right subtree recursively

Visit the root

Page 75: Binary Trees (Non-Linear Data Structures)hilltop.bradley.edu/~young/CIS210old/topic08.pdf · CIS210 58 Operations on Binary Trees Create an empty binary tree. Create a one-node binary

CIS210 75

Post-order Traversal - Processing Order

7

3 6

1 2 54

Page 76: Binary Trees (Non-Linear Data Structures)hilltop.bradley.edu/~young/CIS210old/topic08.pdf · CIS210 58 Operations on Binary Trees Create an empty binary tree. Create a one-node binary

CIS210 76

Example: Postorder traversal

A

B D

I

C HE

GF

C B F G E I H D A

Page 77: Binary Trees (Non-Linear Data Structures)hilltop.bradley.edu/~young/CIS210old/topic08.pdf · CIS210 58 Operations on Binary Trees Create an empty binary tree. Create a one-node binary

CIS210 77

Postorder Traversal & Print

void postorder_print(BTnode* rootBT)

{

if (rootBT != NULL)

{

postorder_print(rootBT-> LchildPtr);

postorder_print(rootBT-> RchildPtr);

cout << rootBT->Data << endl;

}

}

Page 78: Binary Trees (Non-Linear Data Structures)hilltop.bradley.edu/~young/CIS210old/topic08.pdf · CIS210 58 Operations on Binary Trees Create an empty binary tree. Create a one-node binary

CIS210 78

Postorder Traversal and Operation

typedef void (*fType)(DataType& AnItem);

void postorder(BTnode* rootBT, fType Op)

{

if (rootBT != NULL)

{

postorder(rootBT-> LchildPtr);

postorder(rootBT-> RchildPtr);

Op(rootBT->Data);

}

}

Page 79: Binary Trees (Non-Linear Data Structures)hilltop.bradley.edu/~young/CIS210old/topic08.pdf · CIS210 58 Operations on Binary Trees Create an empty binary tree. Create a one-node binary

CIS210 79

5. Backward In-order Traversal

5. Process right-child subtree, then a node, then left-

child subtree.

If the tree is not empty then

Backward Inorder traverse the right subtree

recursively

Visit the root

Backward Inorder traverse the left subtree recursively

Page 80: Binary Trees (Non-Linear Data Structures)hilltop.bradley.edu/~young/CIS210old/topic08.pdf · CIS210 58 Operations on Binary Trees Create an empty binary tree. Create a one-node binary

CIS210 80

Backward In-order Traversal -Processing Order

4

6 2

7 5 13

Page 81: Binary Trees (Non-Linear Data Structures)hilltop.bradley.edu/~young/CIS210old/topic08.pdf · CIS210 58 Operations on Binary Trees Create an empty binary tree. Create a one-node binary

CIS210 81

Example: Backward Inorder Traversal

A

B D

I

C HE

GF

H I D G E F A C B

Page 82: Binary Trees (Non-Linear Data Structures)hilltop.bradley.edu/~young/CIS210old/topic08.pdf · CIS210 58 Operations on Binary Trees Create an empty binary tree. Create a one-node binary

CIS210 82

Example: Backward Inorder Traversal

A

B D

I

C HE

GF

H

I

D

G

E

F

A

C

B

H

I

D

G

E

F

A

C

B

Page 83: Binary Trees (Non-Linear Data Structures)hilltop.bradley.edu/~young/CIS210old/topic08.pdf · CIS210 58 Operations on Binary Trees Create an empty binary tree. Create a one-node binary

CIS210 83

Example: Backward Inorder traversal

A

B D

I

C HE

GF

H

I

D

G

E

F

A

C

B

Page 84: Binary Trees (Non-Linear Data Structures)hilltop.bradley.edu/~young/CIS210old/topic08.pdf · CIS210 58 Operations on Binary Trees Create an empty binary tree. Create a one-node binary

CIS210 84

Example: Backward Inorder traversal

A

B D

I

C HE

GF

H

GF

E

D

C

A

I

B

Page 85: Binary Trees (Non-Linear Data Structures)hilltop.bradley.edu/~young/CIS210old/topic08.pdf · CIS210 58 Operations on Binary Trees Create an empty binary tree. Create a one-node binary

CIS210 85

Breadth-First Traversal of Binary Trees

Page 86: Binary Trees (Non-Linear Data Structures)hilltop.bradley.edu/~young/CIS210old/topic08.pdf · CIS210 58 Operations on Binary Trees Create an empty binary tree. Create a one-node binary

CIS210 86

Breadth-First (Level-order)Traversal

If the tree is not empty then then visit the nodes in

the order of their level (depth) in the tree.

Visit all the nodes at depth zero (the root).

Then, all the nodes from left to right at depth one

Then, all the nodes from left to right at depth two

Then, all the nodes from left to right at depth three

And so on ...

Page 87: Binary Trees (Non-Linear Data Structures)hilltop.bradley.edu/~young/CIS210old/topic08.pdf · CIS210 58 Operations on Binary Trees Create an empty binary tree. Create a one-node binary

CIS210 87

Level-order Traversal -Processing Order

1

2 3

4 5 76

Page 88: Binary Trees (Non-Linear Data Structures)hilltop.bradley.edu/~young/CIS210old/topic08.pdf · CIS210 58 Operations on Binary Trees Create an empty binary tree. Create a one-node binary

CIS210 88

Example: Levelorder traversal

A

B D

I

C HE

GF

A B D C E H F G I

Page 89: Binary Trees (Non-Linear Data Structures)hilltop.bradley.edu/~young/CIS210old/topic08.pdf · CIS210 58 Operations on Binary Trees Create an empty binary tree. Create a one-node binary

CIS210 89

Expression Tree ADT

Page 90: Binary Trees (Non-Linear Data Structures)hilltop.bradley.edu/~young/CIS210old/topic08.pdf · CIS210 58 Operations on Binary Trees Create an empty binary tree. Create a one-node binary

CIS210 90

Algebraic Expressions: Notations

Algebraic expressions

Fully parenthesized Infix notation

Not fully parenthesized Infix notation

Postfix notation

Prefix notation

Page 91: Binary Trees (Non-Linear Data Structures)hilltop.bradley.edu/~young/CIS210old/topic08.pdf · CIS210 58 Operations on Binary Trees Create an empty binary tree. Create a one-node binary

CIS210 91

1. Fully Parenthesized Infix Notation

We need to place parentheses around each pair of

operands together with their operator!

Examples:

(1+2)

(1+(2 * 3))

((1 + 2) * 3)

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

Inconvenient!

Page 92: Binary Trees (Non-Linear Data Structures)hilltop.bradley.edu/~young/CIS210old/topic08.pdf · CIS210 58 Operations on Binary Trees Create an empty binary tree. Create a one-node binary

CIS210 92

2. Not Fully Parenthesized Infix Notation

We can omit unnecessary parentheses!

Examples:

(1+2) 1 + 2

(1+ (2 * 3)) 1+ 2 * 3

((1 + 2) * 3) (1 + 2) * 3

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

Convenient, BUT, we need rules to interpret

correctly.

Page 93: Binary Trees (Non-Linear Data Structures)hilltop.bradley.edu/~young/CIS210old/topic08.pdf · CIS210 58 Operations on Binary Trees Create an empty binary tree. Create a one-node binary

CIS210 93

Not Fully Parenthesized Infix Notation

Operator precedence rule

* / higher than + -

Operator association rule

Associate from left to right

Examples: (Interpretation using rules)

1 + 2 (1+2)

1+ 2 * 3 (1+ (2 * 3))

(1 + 2) * 3 ((1 + 2) * 3)

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

Page 94: Binary Trees (Non-Linear Data Structures)hilltop.bradley.edu/~young/CIS210old/topic08.pdf · CIS210 58 Operations on Binary Trees Create an empty binary tree. Create a one-node binary

CIS210 94

3. Postfix Notation

Postfix Notation:

<postfix> := <id> | (<postfix> <postfix> <op> )

<op> := + | - | * | /

<id> := <variable> | <number>

Examples:

(1+2) 1+ 2 1 2 +

(1+ (2 * 3)) 1+ 2 * 3 1 2 3 * +

((1 + 2) * 3) (1 + 2) * 3 1 2 + 3 *

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

Page 95: Binary Trees (Non-Linear Data Structures)hilltop.bradley.edu/~young/CIS210old/topic08.pdf · CIS210 58 Operations on Binary Trees Create an empty binary tree. Create a one-node binary

CIS210 95

4. Prefix Notation

Postfix Notation:

<prefix> := <id> | (<op> <prefix> <prefix>)

<op> := + | - | * | /

<id> := <variable> | <number>

Examples:

(1+2) 1+ 2 + 1 2

(1+ (2 * 3)) 1+ 2 * 3 + 1 * 2 3

((1 + 2) * 3) (1 + 2) * 3 * + 1 2 3

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

Page 96: Binary Trees (Non-Linear Data Structures)hilltop.bradley.edu/~young/CIS210old/topic08.pdf · CIS210 58 Operations on Binary Trees Create an empty binary tree. Create a one-node binary

CIS210 96

Postfix and Prefix Notations

Postfix and prefix notations do not need!

Parentheses

Operator precedence rules

Operator association rules

Page 97: Binary Trees (Non-Linear Data Structures)hilltop.bradley.edu/~young/CIS210old/topic08.pdf · CIS210 58 Operations on Binary Trees Create an empty binary tree. Create a one-node binary

CIS210 97

Algebraic Expressions as Expression Trees

Algebraic expressions involving binary operations

can be represented by labeled binary trees.

Expression trees represent algebraic expressions!

Op

Opd1 Opd2

Opd1 Op Opd2

Page 98: Binary Trees (Non-Linear Data Structures)hilltop.bradley.edu/~young/CIS210old/topic08.pdf · CIS210 58 Operations on Binary Trees Create an empty binary tree. Create a one-node binary

CIS210 98

Algebraic Expressiona / b + (c - d) * e

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

a b / c d - e * +

+ / a b * - c d e

Page 99: Binary Trees (Non-Linear Data Structures)hilltop.bradley.edu/~young/CIS210old/topic08.pdf · CIS210 58 Operations on Binary Trees Create an empty binary tree. Create a one-node binary

CIS210 99

Expression Tree for a / b + (c - d) * e

+

/ *

a b e-

dc

Page 100: Binary Trees (Non-Linear Data Structures)hilltop.bradley.edu/~young/CIS210old/topic08.pdf · CIS210 58 Operations on Binary Trees Create an empty binary tree. Create a one-node binary

CIS210 100

Expression Tree ADT

// exprtree.h

// Class declarations for the linked implementation of the

// Expression Tree ADT

//--------------------------------------------------------------------

class ExprTree; // Forward declaration of the ExprTree class

class ExprTreeNode // Facilitator class for the ExprTree class

{

private:

// Constructor

ExprTreeNode ( char elem,

ExprTreeNode *leftPtr, ExprTreeNode *rightPtr );

// Data members

char dataItem; // Expression tree data item

ExprTreeNode *left, // Pointer to the left child

*right; // Pointer to the right child

friend class ExprTree;

};

Page 101: Binary Trees (Non-Linear Data Structures)hilltop.bradley.edu/~young/CIS210old/topic08.pdf · CIS210 58 Operations on Binary Trees Create an empty binary tree. Create a one-node binary

CIS210 101

Expression Tree ADT

class ExprTree

{

public:

// Constructor

ExprTree ();

// Destructor

~ExprTree ();

// Expression tree manipulation operations

void build (); // Build tree from prefix expression

void expression () const; // Output expression in infix form

float evaluate () const; // Evaluate expression

void clear (); // Clear tree

Page 102: Binary Trees (Non-Linear Data Structures)hilltop.bradley.edu/~young/CIS210old/topic08.pdf · CIS210 58 Operations on Binary Trees Create an empty binary tree. Create a one-node binary

CIS210 102

Expression Tree ADT

// Output the tree structure -- used in testing/debugging

void showStructure () const;

ExprTree ( const ExprTree &valueTree ); // Copy constructor

Page 103: Binary Trees (Non-Linear Data Structures)hilltop.bradley.edu/~young/CIS210old/topic08.pdf · CIS210 58 Operations on Binary Trees Create an empty binary tree. Create a one-node binary

CIS210 103

Expression Tree ADT

private:

// Recursive partners of the public member functions -- insert

// prototypes of these functions here.

void buildSub ( ExprTreeNode *&p );

void exprSub ( ExprTreeNode *p ) const;

float evaluateSub ( ExprTreeNode *p ) const;

void clearSub ( ExprTreeNode *p );

void showSub ( ExprTreeNode *p, int level ) const;

// Data member

ExprTreeNode *root; // Pointer to the root node

};

Page 104: Binary Trees (Non-Linear Data Structures)hilltop.bradley.edu/~young/CIS210old/topic08.pdf · CIS210 58 Operations on Binary Trees Create an empty binary tree. Create a one-node binary

CIS210 104

Expression Tree ADT

//--------------------------------------------------------------------

// exprtree.cpp

//--------------------------------------------------------------------

ExprTreeNode:: ExprTreeNode ( char nodeDataItem,

ExprTreeNode *leftPtr,

ExprTreeNode *rightPtr )

// Creates an expreesion tree node containing

// data item nodeDataItem,

// left child pointer leftPtr, and right child pointer rightPtr.

: dataItem(nodeDataItem),

left(leftPtr),

right(rightPtr)

{}

Page 105: Binary Trees (Non-Linear Data Structures)hilltop.bradley.edu/~young/CIS210old/topic08.pdf · CIS210 58 Operations on Binary Trees Create an empty binary tree. Create a one-node binary

CIS210 105

Expression Tree ADT

ExprTree:: ExprTree ()

// Creates an empty expression tree.

: root(0)

{}

Page 106: Binary Trees (Non-Linear Data Structures)hilltop.bradley.edu/~young/CIS210old/topic08.pdf · CIS210 58 Operations on Binary Trees Create an empty binary tree. Create a one-node binary

CIS210 106

Expression Tree ADT

//--------------------------------------------------------------------

ExprTree:: ~ExprTree ()

// Frees the memory used by an expression tree.

{

clear();

}

void ExprTree:: clear ()

// Removes all the nodes from an expression tree.

{

clearSub(root);

root = 0;

}

Page 107: Binary Trees (Non-Linear Data Structures)hilltop.bradley.edu/~young/CIS210old/topic08.pdf · CIS210 58 Operations on Binary Trees Create an empty binary tree. Create a one-node binary

CIS210 107

Expression Tree ADT

void ExprTree:: clearSub ( ExprTreeNode *p )

// Recursive partner of the clear() function. Clears the subtree

// pointed to by p.

{

if ( p != 0 )

{

clearSub(p->left);

clearSub(p->right);

delete p;

}

}

Page 108: Binary Trees (Non-Linear Data Structures)hilltop.bradley.edu/~young/CIS210old/topic08.pdf · CIS210 58 Operations on Binary Trees Create an empty binary tree. Create a one-node binary

CIS210 108

Expression Tree ADT

void ExprTree:: build ()

// Reads a prefix expression (consisting of single-digit, nonnegative

// integers and arithmetic operators) from the keyboard and

// builds the corresponding expression tree.

{

buildSub(root);

}

Page 109: Binary Trees (Non-Linear Data Structures)hilltop.bradley.edu/~young/CIS210old/topic08.pdf · CIS210 58 Operations on Binary Trees Create an empty binary tree. Create a one-node binary

CIS210 109

Expression Tree ADT

void ExprTree:: buildSub ( ExprTreeNode *&p )

// Recursive partner of the build() function. Builds a subtree and

// sets p to point to its root.

{

char ch; // Input operator or number

cin >> ch;

p = new ExprTreeNode(ch,0,0); // Link in node

if ( !isdigit(ch) ) // Operator -- construct subtrees

{

buildSub(p->left);

buildSub(p->right);

}

}

Page 110: Binary Trees (Non-Linear Data Structures)hilltop.bradley.edu/~young/CIS210old/topic08.pdf · CIS210 58 Operations on Binary Trees Create an empty binary tree. Create a one-node binary

CIS210 110

Expression Tree ADT

void ExprTree:: expression () const

// Outputs the corresponding arithmetic expression in fully

// parenthesized infix form.

{

exprSub(root);

}

Page 111: Binary Trees (Non-Linear Data Structures)hilltop.bradley.edu/~young/CIS210old/topic08.pdf · CIS210 58 Operations on Binary Trees Create an empty binary tree. Create a one-node binary

CIS210 111

Expression Tree ADT

void ExprTree:: exprSub ( ExprTreeNode *p ) const

// Recursive partner of the expression() function.

// Outputs the subtree pointed to by p.

{

if ( p != 0 )

{

if ( !isdigit(p->dataItem) ) cout << '(';

exprSub(p->left);

cout << p->dataItem;

exprSub(p->right);

if ( !isdigit(p->dataItem) ) cout << ')';

}

}

Page 112: Binary Trees (Non-Linear Data Structures)hilltop.bradley.edu/~young/CIS210old/topic08.pdf · CIS210 58 Operations on Binary Trees Create an empty binary tree. Create a one-node binary

CIS210 112

Expression Tree ADT

float ExprTree:: evaluate ()

// Returns the value of the corresponding arithmetic expression.

{

// Requires that the tree is not empty

return evaluateSub(root);

}

Page 113: Binary Trees (Non-Linear Data Structures)hilltop.bradley.edu/~young/CIS210old/topic08.pdf · CIS210 58 Operations on Binary Trees Create an empty binary tree. Create a one-node binary

CIS210 113

Expression Tree ADTfloat ExprTree:: evaluateSub ( ExprTreeNode *p ) const

// Recursive partner of the evaluate() function. Returns the value of

// subtree pointed to by p.

{

float l, r, // Intermediate results

result; // Result returned

if ( isdigit(p->dataItem) )

result = p->dataItem - '0'; // Convert from char to number

else

{

l = evaluateSub(p->left); // Evaluate subtrees

r = evaluateSub(p->right);

switch ( p->dataItem ) // Combine results

{

case '+' : result = l + r; break;

case '-' : result = l - r; break;

case '*' : result = l * r; break;

case '/' : result = l / r;

}

}

return result;

}