CSC212 Data Structure - Section AB Lecture 15 Trees and Tree Traversals Instructor: Edgardo Molina...

91
CSC212 Data Structure - Section AB Lecture 15 Trees and Tree Traversals Instructor: Edgardo Molina Department of Computer Science

Transcript of CSC212 Data Structure - Section AB Lecture 15 Trees and Tree Traversals Instructor: Edgardo Molina...

Page 1: CSC212 Data Structure - Section AB Lecture 15 Trees and Tree Traversals Instructor: Edgardo Molina Department of Computer Science City College of New York.

CSC212 Data Structure - Section AB

CSC212 Data Structure - Section AB

Lecture 15

Trees and Tree Traversals

Instructor: Edgardo Molina

Department of Computer Science

City College of New York

Page 2: CSC212 Data Structure - Section AB Lecture 15 Trees and Tree Traversals Instructor: Edgardo Molina Department of Computer Science City College of New York.

MotivationMotivation

Linear structures arrays dynamic arrays linked lists

Nonlinear Structures trees - Hierarchical Structures Graphs

Why???

Page 3: CSC212 Data Structure - Section AB Lecture 15 Trees and Tree Traversals Instructor: Edgardo Molina Department of Computer Science City College of New York.

Application: Mailing AddressesApplication: Mailing Addresses

Zhigang Zhu, CS Dept, CCNY, New York, NY 10031, USA

6 billion = 6,000,000,000 people in the world

What kind of structure is the best for a postman to locate me?

Array ?

Linked list ?

Tree ?

Page 4: CSC212 Data Structure - Section AB Lecture 15 Trees and Tree Traversals Instructor: Edgardo Molina Department of Computer Science City College of New York.

A Tree for all the mailing addressesA Tree for all the mailing addresses

China

Earth

USA

... ...Korea

NY ... ...

... ...

NYC

MA ... ...

CCNY

Z. Zhu

Albany ... ...

... ...

... ...

... ...

CS

Page 5: CSC212 Data Structure - Section AB Lecture 15 Trees and Tree Traversals Instructor: Edgardo Molina Department of Computer Science City College of New York.

Chapter 10 introduces trees. This presentation illustrates basic

terminology for binary trees and focuses on

Complete Binary Trees: the simplest kind of trees

Binary Tree Traversals: any kind of binary trees

Trees and Binary TreesTrees and Binary Trees

Data Structuresand Other ObjectsUsing C++

Page 6: CSC212 Data Structure - Section AB Lecture 15 Trees and Tree Traversals Instructor: Edgardo Molina Department of Computer Science City College of New York.

Binary TreesBinary Trees

A binary tree has nodes, similar to nodes in a linked list structure.

Data of one sort or another may be stored at each node.

But it is the connections between the nodes which characterize a binary tree.

Page 7: CSC212 Data Structure - Section AB Lecture 15 Trees and Tree Traversals Instructor: Edgardo Molina Department of Computer Science City College of New York.

Binary TreesBinary Trees

A binary tree has nodes, similar to nodes in a linked list structure.

Data of one sort or another may be stored at each node.

But it is the connections between the nodes which characterize a binary tree.

An example canillustrate how theconnections work

An example canillustrate how theconnections work

Page 8: CSC212 Data Structure - Section AB Lecture 15 Trees and Tree Traversals Instructor: Edgardo Molina Department of Computer Science City College of New York.

A Binary Tree of StatesA Binary Tree of States

In this example, the data contained at each node is one of the 50 states.

Washington

Colorado

Oklahoma

Arkansas

Mass.

Florida

New

Ham

pshi

re

Arizona

Nebraska

Page 9: CSC212 Data Structure - Section AB Lecture 15 Trees and Tree Traversals Instructor: Edgardo Molina Department of Computer Science City College of New York.

A Binary Tree of StatesA Binary Tree of States

Each tree has a special node called its root, usually drawn at the top.

Page 10: CSC212 Data Structure - Section AB Lecture 15 Trees and Tree Traversals Instructor: Edgardo Molina Department of Computer Science City College of New York.

A Binary Tree of StatesA Binary Tree of States

Each tree has a special node called its root, usually drawn at the top. The example tree

has Washingtonas its root.

The example treehas Washington

as its root.

Page 11: CSC212 Data Structure - Section AB Lecture 15 Trees and Tree Traversals Instructor: Edgardo Molina Department of Computer Science City College of New York.

A Binary Tree of StatesA Binary Tree of States

Each node is permitted to have two links to other nodes, called the left child and the right child.

Page 12: CSC212 Data Structure - Section AB Lecture 15 Trees and Tree Traversals Instructor: Edgardo Molina Department of Computer Science City College of New York.

A Binary Tree of StatesA Binary Tree of States

Each node is permitted to have two links to other nodes, called the left child and the right child.

Page 13: CSC212 Data Structure - Section AB Lecture 15 Trees and Tree Traversals Instructor: Edgardo Molina Department of Computer Science City College of New York.

A Binary Tree of StatesA Binary Tree of States

Children are usually drawn below a node.

The right child ofWashington is

Colorado.

The right child ofWashington is

Colorado.

The left child ofWashington is

Arkansas.

The left child ofWashington is

Arkansas.

Page 14: CSC212 Data Structure - Section AB Lecture 15 Trees and Tree Traversals Instructor: Edgardo Molina Department of Computer Science City College of New York.

A Binary Tree of StatesA Binary Tree of States

Some nodes have only one child.

Arkansas has aleft child, but no

right child.

Arkansas has aleft child, but no

right child.

Page 15: CSC212 Data Structure - Section AB Lecture 15 Trees and Tree Traversals Instructor: Edgardo Molina Department of Computer Science City College of New York.

A QuizA Quiz

Some nodes have only one child.

Which node hasonly a right child?Which node has

only a right child?

Page 16: CSC212 Data Structure - Section AB Lecture 15 Trees and Tree Traversals Instructor: Edgardo Molina Department of Computer Science City College of New York.

A QuizA Quiz

Some nodes have only one child.

Florida hasonly a right child.

Florida hasonly a right child.

Page 17: CSC212 Data Structure - Section AB Lecture 15 Trees and Tree Traversals Instructor: Edgardo Molina Department of Computer Science City College of New York.

A Binary Tree of StatesA Binary Tree of States

A node with no children is called a leaf.

Page 18: CSC212 Data Structure - Section AB Lecture 15 Trees and Tree Traversals Instructor: Edgardo Molina Department of Computer Science City College of New York.

A Binary Tree of StatesA Binary Tree of States

Each node is called the parent of its children.

Washington is theparent of Arkansas

and Colorado.

Washington is theparent of Arkansas

and Colorado.

Page 19: CSC212 Data Structure - Section AB Lecture 15 Trees and Tree Traversals Instructor: Edgardo Molina Department of Computer Science City College of New York.

A Binary Tree of StatesA Binary Tree of States

Two rules about parents:

The root has no parent.

Every other node has exactly one parent.

Page 20: CSC212 Data Structure - Section AB Lecture 15 Trees and Tree Traversals Instructor: Edgardo Molina Department of Computer Science City College of New York.

A Binary Tree of StatesA Binary Tree of States

Two nodes with the same parent are called siblings.

Arkansasand Coloradoare siblings.

Arkansasand Coloradoare siblings.

Page 21: CSC212 Data Structure - Section AB Lecture 15 Trees and Tree Traversals Instructor: Edgardo Molina Department of Computer Science City College of New York.

Complete Binary TreesComplete Binary Trees

A complete binary tree is a special kind of binary tree which will be useful to us.

Page 22: CSC212 Data Structure - Section AB Lecture 15 Trees and Tree Traversals Instructor: Edgardo Molina Department of Computer Science City College of New York.

Complete Binary TreesComplete Binary Trees

A complete binary tree is a special kind of binary tree which will be useful to us.

When a completebinary tree is built,

its first node must bethe root.

When a completebinary tree is built,

its first node must bethe root.

Page 23: CSC212 Data Structure - Section AB Lecture 15 Trees and Tree Traversals Instructor: Edgardo Molina Department of Computer Science City College of New York.

Complete Binary TreesComplete Binary Trees

The second node of a complete binary tree is always the left child of the root...

Page 24: CSC212 Data Structure - Section AB Lecture 15 Trees and Tree Traversals Instructor: Edgardo Molina Department of Computer Science City College of New York.

Complete Binary TreesComplete Binary Trees

The second node of a complete binary tree is always the left child of the root...

... and the third node is always the right child of the root.

Page 25: CSC212 Data Structure - Section AB Lecture 15 Trees and Tree Traversals Instructor: Edgardo Molina Department of Computer Science City College of New York.

Complete Binary TreesComplete Binary Trees

The next nodes must always fill the next level from left to right.

Page 26: CSC212 Data Structure - Section AB Lecture 15 Trees and Tree Traversals Instructor: Edgardo Molina Department of Computer Science City College of New York.

Complete Binary TreesComplete Binary Trees

The next nodes must always fill the next level from left to right.

Page 27: CSC212 Data Structure - Section AB Lecture 15 Trees and Tree Traversals Instructor: Edgardo Molina Department of Computer Science City College of New York.

Complete Binary TreesComplete Binary Trees

The next nodes must always fill the next level from left to right.

Page 28: CSC212 Data Structure - Section AB Lecture 15 Trees and Tree Traversals Instructor: Edgardo Molina Department of Computer Science City College of New York.

Complete Binary TreesComplete Binary Trees

The next nodes must always fill the next level from left to right.

Page 29: CSC212 Data Structure - Section AB Lecture 15 Trees and Tree Traversals Instructor: Edgardo Molina Department of Computer Science City College of New York.

Complete Binary TreesComplete Binary Trees

The next nodes must always fill the next level from left to right.

Page 30: CSC212 Data Structure - Section AB Lecture 15 Trees and Tree Traversals Instructor: Edgardo Molina Department of Computer Science City College of New York.

Complete Binary TreesComplete Binary Trees

The next nodes must always fill the next level from left to right.

Page 31: CSC212 Data Structure - Section AB Lecture 15 Trees and Tree Traversals Instructor: Edgardo Molina Department of Computer Science City College of New York.

Is This Complete?Is This Complete?

Page 32: CSC212 Data Structure - Section AB Lecture 15 Trees and Tree Traversals Instructor: Edgardo Molina Department of Computer Science City College of New York.

Is This Complete?Is This Complete?

Page 33: CSC212 Data Structure - Section AB Lecture 15 Trees and Tree Traversals Instructor: Edgardo Molina Department of Computer Science City College of New York.

Is This Complete?Is This Complete?

Page 34: CSC212 Data Structure - Section AB Lecture 15 Trees and Tree Traversals Instructor: Edgardo Molina Department of Computer Science City College of New York.

Is This Complete?Is This Complete?

Page 35: CSC212 Data Structure - Section AB Lecture 15 Trees and Tree Traversals Instructor: Edgardo Molina Department of Computer Science City College of New York.

Is This Complete?Is This Complete?

Yes! It is called the empty

tree, and it has no nodes, not even a root.

Page 36: CSC212 Data Structure - Section AB Lecture 15 Trees and Tree Traversals Instructor: Edgardo Molina Department of Computer Science City College of New York.

Full Binary TreesFull Binary Trees

A full binary tree is a special kind of complete binary tree

When a fullbinary tree is built,

its first node must bethe root.

When a fullbinary tree is built,

its first node must bethe root.

FULL

Page 37: CSC212 Data Structure - Section AB Lecture 15 Trees and Tree Traversals Instructor: Edgardo Molina Department of Computer Science City College of New York.

Full Binary TreesFull Binary Trees

The second node of a full binary tree is always the left child of the root... not FULL yet

Page 38: CSC212 Data Structure - Section AB Lecture 15 Trees and Tree Traversals Instructor: Edgardo Molina Department of Computer Science City College of New York.

Full Binary TreesFull Binary Trees

The second node of a full binary tree is always the left child of the root...

... and you MUST have the third node which always the right child of the root.

FULL

Page 39: CSC212 Data Structure - Section AB Lecture 15 Trees and Tree Traversals Instructor: Edgardo Molina Department of Computer Science City College of New York.

Full Binary TreesFull Binary Trees

The next nodes must always fill the next level from left to right.

not FULL yet

Page 40: CSC212 Data Structure - Section AB Lecture 15 Trees and Tree Traversals Instructor: Edgardo Molina Department of Computer Science City College of New York.

Full Binary TreesFull Binary Trees

The next nodes must always fill the next level from left to right.

not FULL yet

Page 41: CSC212 Data Structure - Section AB Lecture 15 Trees and Tree Traversals Instructor: Edgardo Molina Department of Computer Science City College of New York.

Full Binary TreesFull Binary Trees

The next nodes must always fill the next level from left to right.

not FULL yet

Page 42: CSC212 Data Structure - Section AB Lecture 15 Trees and Tree Traversals Instructor: Edgardo Molina Department of Computer Science City College of New York.

Full Binary TreesFull Binary Trees

The next nodes must always fill the next level from left to right...until every leaf has the same depth (2)

FULL!

Page 43: CSC212 Data Structure - Section AB Lecture 15 Trees and Tree Traversals Instructor: Edgardo Molina Department of Computer Science City College of New York.

Full Binary TreesFull Binary Trees

The next nodes must always fill the next level from left to right.

Page 44: CSC212 Data Structure - Section AB Lecture 15 Trees and Tree Traversals Instructor: Edgardo Molina Department of Computer Science City College of New York.

Full Binary TreesFull Binary Trees

The next nodes must always fill the next level from left to right.

Page 45: CSC212 Data Structure - Section AB Lecture 15 Trees and Tree Traversals Instructor: Edgardo Molina Department of Computer Science City College of New York.

Is This Full?Is This Full?

Page 46: CSC212 Data Structure - Section AB Lecture 15 Trees and Tree Traversals Instructor: Edgardo Molina Department of Computer Science City College of New York.

Is This Full?Is This Full?

Page 47: CSC212 Data Structure - Section AB Lecture 15 Trees and Tree Traversals Instructor: Edgardo Molina Department of Computer Science City College of New York.

Is This Full?Is This Full?

Page 48: CSC212 Data Structure - Section AB Lecture 15 Trees and Tree Traversals Instructor: Edgardo Molina Department of Computer Science City College of New York.

Is This Full?Is This Full?

Page 49: CSC212 Data Structure - Section AB Lecture 15 Trees and Tree Traversals Instructor: Edgardo Molina Department of Computer Science City College of New York.

Is This Full?Is This Full?

Yes! It is called the empty

tree, and it has no nodes, not even a root.

Page 50: CSC212 Data Structure - Section AB Lecture 15 Trees and Tree Traversals Instructor: Edgardo Molina Department of Computer Science City College of New York.

Implementing a Complete Binary TreeImplementing a Complete Binary Tree

We will store the data from the nodes in a partially-filled array.

An array of dataWe don't care what's in

this part of the array.

An integer to keeptrack of how many nodes are in the tree

3

Page 51: CSC212 Data Structure - Section AB Lecture 15 Trees and Tree Traversals Instructor: Edgardo Molina Department of Computer Science City College of New York.

Implementing a Complete Binary Tree Using an ArrayImplementing a Complete Binary Tree Using an Array

We will store the data from the nodes in a partially-filled array.

An array of dataWe don't care what's in

this part of the array.

An integer to keeptrack of how many nodes are in the tree

3

Read Section 10.2 tosee details of how

the entries are stored.

Read Section 10.2 tosee details of how

the entries are stored.

Page 52: CSC212 Data Structure - Section AB Lecture 15 Trees and Tree Traversals Instructor: Edgardo Molina Department of Computer Science City College of New York.

Implementing a Complete Binary Tree Using an ArrayImplementing a Complete Binary Tree Using an Array

Root is at component [0] Parent of node in [i] is at [(i-1)/2) Children (if exist) of node [i] is at [2i+1]

and [2i+2]

Total node number 20+21+22+…+2d-1+r, r <= 2d, d is the depth

Page 53: CSC212 Data Structure - Section AB Lecture 15 Trees and Tree Traversals Instructor: Edgardo Molina Department of Computer Science City College of New York.

Binary trees contain nodes. Each node may have a left child and a right child. If you start from any node and move upward, you

will eventually reach the root. Every node except the root has one parent. The

root has no parent. Complete binary trees require the nodes to fill in

each level from left-to-right before starting the next level.

Binary Tree Summary Binary Tree Summary

Page 54: CSC212 Data Structure - Section AB Lecture 15 Trees and Tree Traversals Instructor: Edgardo Molina Department of Computer Science City College of New York.

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 BasicsBinary Tree Basics

Page 55: CSC212 Data Structure - Section AB Lecture 15 Trees and Tree Traversals Instructor: Edgardo Molina Department of Computer Science City College of New York.

A Binary Tree ExerciseA Binary Tree Exercise

Q

V

T

K S

A E

L

Page 56: CSC212 Data Structure - Section AB Lecture 15 Trees and Tree Traversals Instructor: Edgardo Molina Department of Computer Science City College of New York.

How many leaf nodes?How many leaf nodes?

Q

V

T

K S

A E

L

Page 57: CSC212 Data Structure - Section AB Lecture 15 Trees and Tree Traversals Instructor: Edgardo Molina Department of Computer Science City College of New York.

How many descendants of Q?How many descendants of Q?

Q

V

T

K S

A E

L

Page 58: CSC212 Data Structure - Section AB Lecture 15 Trees and Tree Traversals Instructor: Edgardo Molina Department of Computer Science City College of New York.

How many ancestors of K?How many ancestors of K?

Q

V

T

K S

A E

L

Question: How to implement a general binary tree ?

Page 59: CSC212 Data Structure - Section AB Lecture 15 Trees and Tree Traversals Instructor: Edgardo Molina Department of Computer Science City College of New York.

Implementing a Binary Tree with a Class for NodesImplementing a Binary Tree with a Class for Nodes

Q

V

T

K S

A E

L

Root

Page 60: CSC212 Data Structure - Section AB Lecture 15 Trees and Tree Traversals Instructor: Edgardo Molina Department of Computer Science City College of New York.

Binary Tree NodesBinary Tree Nodes

Each node of a binary tree is stored in an object of a new binary_tree_node class that we are going to define

Each node contains data as well as pointers to its children (nodes)

An entire tree is represented as a pointer to the root node

Page 61: CSC212 Data Structure - Section AB Lecture 15 Trees and Tree Traversals Instructor: Edgardo Molina Department of Computer Science City College of New York.

binary_tree_node Classbinary_tree_node Class

variables functions template <class Item> class binary_tree_node { public:

...... private:

Item data_field;binary_tree_node *left_field;binary_tree_node *right_field;

};

bintree

//retrievalsdataleftright//setset_dataset_leftset_right//boolean

is_leaf

Page 62: CSC212 Data Structure - Section AB Lecture 15 Trees and Tree Traversals Instructor: Edgardo Molina Department of Computer Science City College of New York.

Creating and Manipulating TreesCreating and Manipulating Trees

Consider only two functions Clearing a tree

Return nodes of a tree to the heap Copying a tree

The Implementation is easier than it seems if we use recursive thinking

Page 63: CSC212 Data Structure - Section AB Lecture 15 Trees and Tree Traversals Instructor: Edgardo Molina Department of Computer Science City College of New York.

Clearing a TreeClearing a Tree

Q

V

T

K S

A E

L

Root

Page 64: CSC212 Data Structure - Section AB Lecture 15 Trees and Tree Traversals Instructor: Edgardo Molina Department of Computer Science City College of New York.

Clearing a TreeClearing a Tree

Q

V

T

K S

A E

L

Root

Clear LEFT SUBTREE

Page 65: CSC212 Data Structure - Section AB Lecture 15 Trees and Tree Traversals Instructor: Edgardo Molina Department of Computer Science City College of New York.

Clearing a TreeClearing a Tree

V

S

A

L

Root

Clear RIGHT SUBTREE

Page 66: CSC212 Data Structure - Section AB Lecture 15 Trees and Tree Traversals Instructor: Edgardo Molina Department of Computer Science City College of New York.

Clearing a TreeClearing a Tree

V Root

Return root node to the heap

Page 67: CSC212 Data Structure - Section AB Lecture 15 Trees and Tree Traversals Instructor: Edgardo Molina Department of Computer Science City College of New York.

Clearing a TreeClearing a TreeNULL Root

Set the root pointer to NULL

Page 68: CSC212 Data Structure - Section AB Lecture 15 Trees and Tree Traversals Instructor: Edgardo Molina Department of Computer Science City College of New York.

Clear a TreeClear a Tree

key: recursive thinking

template <class Item> void tree_clear(binary_tree_node<Item>*& root_ptr) // Library facilities used: cstdlib {

if (root_ptr != NULL){ tree_clear( root_ptr->left( ) ); // clear left sub_tree tree_clear( root_ptr->right( ) ); // clear right sub_tree delete root_ptr; // return root node to the heap root_ptr = NULL; // set root pointer to the null}

}

bintree

Page 69: CSC212 Data Structure - Section AB Lecture 15 Trees and Tree Traversals Instructor: Edgardo Molina Department of Computer Science City College of New York.

Copy a TreeCopy a Tree

Can you implement the copy? (p 467) template <class Item> binary_tree_node<Item>* tree_copy(const binary_tree_node<Item>* root_ptr) // Library facilities used: cstdlib {

binary_tree_node<Item> *l_ptr;binary_tree_node<Item> *r_ptr;

if (root_ptr == NULL) return NULL;else{ l_ptr = tree_copy( root_ptr->left( ) ); // copy the left sub_tree r_ptr = tree_copy( root_ptr->right( ) ); // copy the right sub_tree return

new binary_tree_node<Item>( root_ptr->data( ), l_ptr, r_ptr);} // copy the root node and set the the root pointer

}

bintree

Page 70: CSC212 Data Structure - Section AB Lecture 15 Trees and Tree Traversals Instructor: Edgardo Molina Department of Computer Science City College of New York.

Binary Tree TraversalsBinary Tree Traversals

pre-order traversal root (left sub_tree) (right sub_tree)

in-order traversal (left sub_tree) root (right sub_tree)

post-order traversal (left sub_tree) (right sub_tree) root

backward in-order traversal (right sub_tree) root (left sub_tree)

bintree

Page 71: CSC212 Data Structure - Section AB Lecture 15 Trees and Tree Traversals Instructor: Edgardo Molina Department of Computer Science City College of New York.

Preorder Traversal: J E A H T M YPreorder Traversal: J E A H T M Y

‘J’

‘E’

‘A’ ‘H’

‘T’

‘M’ ‘Y’

tree

Print left subtree second Print right subtree last

Print first

Page 72: CSC212 Data Structure - Section AB Lecture 15 Trees and Tree Traversals Instructor: Edgardo Molina Department of Computer Science City College of New York.

Preorder TraversalPreorder Traversal

Example: print the contents of each node

template <class Item> void preorder_print(const binary_tree_node<Item>* node_ptr) // Library facilities used: cstdlib, iostream { if (node_ptr != NULL) { std::cout << node_ptr->data( ) << std::endl; preorder_print(node_ptr->left( )); preorder_print(node_ptr->right( )); } }

Page 73: CSC212 Data Structure - Section AB Lecture 15 Trees and Tree Traversals Instructor: Edgardo Molina Department of Computer Science City College of New York.

Inorder Traversal: A E H J M T Y Inorder Traversal: A E H J M T Y

‘J’

‘E’

‘A’ ‘H’

‘T’

‘M’ ‘Y’

tree

Print left subtree first Print right subtree last

Print second

Page 74: CSC212 Data Structure - Section AB Lecture 15 Trees and Tree Traversals Instructor: Edgardo Molina Department of Computer Science City College of New York.

Inorder TraversalInorder Traversal

Example: print the contents of each node

template <class Item> void inorder_print(const binary_tree_node<Item>* node_ptr) // Library facilities used: cstdlib, iostream { if (node_ptr != NULL) { inorder_print(node_ptr->left( )); std::cout << node_ptr->data( ) << std::endl; inorder_print(node_ptr->right( )); } }

Page 75: CSC212 Data Structure - Section AB Lecture 15 Trees and Tree Traversals Instructor: Edgardo Molina Department of Computer Science City College of New York.

‘J’

‘E’

‘A’ ‘H’

‘T’

‘M’ ‘Y’

tree

Print left subtree first Print right subtree second

Print last

Postorder Traversal: A H E M Y T JPostorder Traversal: A H E M Y T J

Page 76: CSC212 Data Structure - Section AB Lecture 15 Trees and Tree Traversals Instructor: Edgardo Molina Department of Computer Science City College of New York.

Postorder TraversalPostorder Traversal

Example: print the contents of each node

template <class Item> void postorder_print(const binary_tree_node<Item>* node_ptr) // Library facilities used: cstdlib, iostream { if (node_ptr != NULL) { postorder_print(node_ptr->left( )); postorder_print(node_ptr->right( )); std::cout << node_ptr->data( ) << std::endl; } }

Page 77: CSC212 Data Structure - Section AB Lecture 15 Trees and Tree Traversals Instructor: Edgardo Molina Department of Computer Science City College of New York.

Backward Inorder Traversal: Y T M J H E A Backward Inorder Traversal: Y T M J H E A

‘J’

‘E’

‘A’ ‘H’

‘T’

‘M’ ‘Y’

tree

Print right subtree firstPrint left subtree last

Print second

Page 78: CSC212 Data Structure - Section AB Lecture 15 Trees and Tree Traversals Instructor: Edgardo Molina Department of Computer Science City College of New York.

Backward Inorder Traversal: Y T M J H E A Backward Inorder Traversal: Y T M J H E A

‘J’

‘E

‘A’

‘H’

‘T’

‘M

’‘Y

tree

Print right subtree first

Print left subtree last

Print second

Page 79: CSC212 Data Structure - Section AB Lecture 15 Trees and Tree Traversals Instructor: Edgardo Molina Department of Computer Science City College of New York.

A Useful BackwardInorder TraversalA Useful BackwardInorder Traversal

Indent each number according its depth

template <class Item, class SizeType> void print(binary_tree_node<Item>* node_ptr, SizeType depth) // Library facilities used: iomanip, iostream, stdlib { if (node_ptr != NULL) { print(node_ptr->right( ), depth+1); std::cout << std::setw(4*depth) << ""; // Indent 4*depth spaces. std::cout << node_ptr->data( ) << std::endl; print(node_ptr->left( ), depth+1); } }

bintree

Page 80: CSC212 Data Structure - Section AB Lecture 15 Trees and Tree Traversals Instructor: Edgardo Molina Department of Computer Science City College of New York.

A Challenging Question:A Challenging Question:

For the traversals we have seen, the “processing” was simply printing the values of the node

But we’d like to do any kind of processing We can replace “cout” with some other form of

“processing” But how about 1000 kinds?

Can template be helpful?

Solution::::::::> (pages 501 – 507)

Page 81: CSC212 Data Structure - Section AB Lecture 15 Trees and Tree Traversals Instructor: Edgardo Molina Department of Computer Science City College of New York.

A parameter can be a functionA parameter can be a function

write one function capable of doing anything A parameter to a function may be a function. Such

a parameter is declared by the name of the function’s return type (or void), then the name of the parameter (i.e. the function), and finally a pair of parentheses (). Inside () is a list of parameter types of that parameter

function Example

int sum ( void f (int&, double), int i,...);

Page 82: CSC212 Data Structure - Section AB Lecture 15 Trees and Tree Traversals Instructor: Edgardo Molina Department of Computer Science City College of New York.

Preorder Traversal – print onlyPreorder Traversal – print only

Example: print the contents of each node

template <class Item> void preorder_print(const binary_tree_node<Item>* node_ptr) // Library facilities used: cstdlib, iostream { if (node_ptr != NULL) { std::cout << node_ptr->data( ) << std::endl; preorder_print(node_ptr->left( )); preorder_print(node_ptr->right( )); } }

Page 83: CSC212 Data Structure - Section AB Lecture 15 Trees and Tree Traversals Instructor: Edgardo Molina Department of Computer Science City College of New York.

Preorder Traversal – general formPreorder Traversal – general form

A template function for tree traversals

template <class Item> void preorder(void f(Item&), binary_tree_node<Item>* node_ptr) // Library facilities used: cstdlib { if (node_ptr != NULL) { f( node_ptr->data( ) ); // node_ptr->data() return reference ! preorder(f, node_ptr->left( )); preorder(f, node_ptr->right( )); } }

Page 84: CSC212 Data Structure - Section AB Lecture 15 Trees and Tree Traversals Instructor: Edgardo Molina Department of Computer Science City College of New York.

Preorder Traversal – how to usePreorder Traversal – how to use

Define a real function before calling

void printout(int & it) // Library facilities used: iostream { std::cout << it << std::endl; }

Can you print out all the node of a tree pointed by root ?

binary_tree_node<int> *root; ....preorder(printout, root); Yes!!!

Page 85: CSC212 Data Structure - Section AB Lecture 15 Trees and Tree Traversals Instructor: Edgardo Molina Department of Computer Science City College of New York.

Preorder Traversal – another functionsPreorder Traversal – another functions

Can define other functions...

void assign_default(int& it) // Library facilities used: iostream {

it = 0; } // unfortunately template does not work here for function parameters

You can assign a default value to all the node of a tree pointed by root:

binary_tree_node<int> *root; ....preorder(assign_default, root);

Page 86: CSC212 Data Structure - Section AB Lecture 15 Trees and Tree Traversals Instructor: Edgardo Molina Department of Computer Science City College of New York.

Preorder Traversal – how to usePreorder Traversal – how to use

Can the function-arguments be template?

template <class Item> void printout(Item& it) // Library facilities used: iostream { std::cout << it << std::endl; }

Can you print out all the node of a tree pointed by root ?

binary_tree_node<string> *root; ....preorder(print_out, root); X ! print_out should have real types

Page 87: CSC212 Data Structure - Section AB Lecture 15 Trees and Tree Traversals Instructor: Edgardo Molina Department of Computer Science City College of New York.

Preorder Traversal – how to usePreorder Traversal – how to use

The function-arguments may be template if...

template <class Item> void printout(Item& it) // Library facilities used: iostream { std::cout << it << std::endl; }

Can you print out all the node of a tree pointed by root ?

binary_tree_node<string> *root; ....preorder(print_out<string>, root);

But you may do the instantiation like this

Page 88: CSC212 Data Structure - Section AB Lecture 15 Trees and Tree Traversals Instructor: Edgardo Molina Department of Computer Science City College of New York.

Preorder Traversal – a more general formPreorder Traversal – a more general form

An extremely general implementation (p 505) template <class Process, class BTNode> void preorder(Process f, BTNode* node_ptr)// Note: BTNode may be a binary_tree_node or a const binary tree node.// Process is the type of a function f that may be called with a single// Item argument (using the Item type from the node),// as determined by the actual f in the following.// Library facilities used: cstdlib { if (node_ptr != NULL) { f( node_ptr->data( ) ); preorder(f, node_ptr->left( )); preorder(f, node_ptr->right( )); } }

bintree

Page 89: CSC212 Data Structure - Section AB Lecture 15 Trees and Tree Traversals Instructor: Edgardo Molina Department of Computer Science City College of New York.

Functions as ParametersFunctions as Parameters

We can define a template function X with functions as parameters – which are called function parameters

A function parameter can be simply written as Process f ( where Process is a template), and the forms and number of parameters for f are determined by the actual call of f inside the template function X

The real function argument for f when calling the the template function X cannot be a template function, it must be instantiated in advance or right in the function call

Page 90: CSC212 Data Structure - Section AB Lecture 15 Trees and Tree Traversals Instructor: Edgardo Molina Department of Computer Science City College of New York.

SummarySummary

Tree, Binary Tree, Complete Binary Tree child, parent, sibling, root, leaf, ancestor,...

Array Representation for Complete Binary Tree Difficult if not complete binary tree

A Class of binary_tree_node each node with two link fields

Tree Traversals recursive thinking makes things much easier

A general Tree Traversal A Function as a parameter of another function

Page 91: CSC212 Data Structure - Section AB Lecture 15 Trees and Tree Traversals Instructor: Edgardo Molina Department of Computer Science City College of New York.

THE ENDTHE END

Presentation copyright 1997 Addison Wesley Longman,For use with Data Structures and Other Objects Using C++by Michael Main and Walter Savitch.

Some artwork in the presentation is used with permission from Presentation Task Force(copyright New Vision Technologies Inc) and Corel Gallery Clipart Catalog (copyrightCorel Corporation, 3G Graphics Inc, Archive Arts, Cartesia Software, Image ClubGraphics Inc, One Mile Up Inc, TechPool Studios, Totem Graphics Inc).

Students and instructors who use Data Structures and Other Objects Using C++ are welcometo use this presentation however they see fit, so long as this copyright notice remainsintact.

Copyright from slide 2 – slide 49: