Top Banner
Binary Trees 2 Prof. Sin-Min Lee Department of Computer Science
45

Binary Trees 2 Prof. Sin-Min Lee Department of Computer Science.

Dec 20, 2015

Download

Documents

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 2 Prof. Sin-Min Lee Department of Computer Science.

Binary Trees 2

Prof. Sin-Min Lee

Department of Computer Science

Page 2: Binary Trees 2 Prof. Sin-Min Lee Department of Computer Science.
Page 3: Binary Trees 2 Prof. Sin-Min Lee Department of Computer Science.
Page 4: Binary Trees 2 Prof. Sin-Min Lee Department of Computer Science.
Page 5: Binary Trees 2 Prof. Sin-Min Lee Department of Computer Science.
Page 6: Binary Trees 2 Prof. Sin-Min Lee Department of Computer Science.

For each number of nodes, n, there is a certain number of possible binary tree configurations. These numbers form a sequence of integers with respect to n.

A useful way to describe an integer sequence is to construct a generating

function:

whose coefficients bi are the sequence.

B(x) is power series over a purely formal variable x.

Page 7: Binary Trees 2 Prof. Sin-Min Lee Department of Computer Science.

Apparently, b1 is 1, and b2 is 2. The b0 coefficient

is somewhat artificial, its the no-nodes tree which is, I guess, the only one. Further analysis gives the following idea: if a binary tree has n nodes, then there must be one node as the root and two subtrees. The total number of nodes in the subtrees must be n-1, and either of them may be empty. Assuming k nodes in the left subtree, the right subtree then has n-k-1 nodes, k going from 0 to n-1. The root node is always there, and therefore the tree configurations differ only by the configuration of subtrees.

Page 8: Binary Trees 2 Prof. Sin-Min Lee Department of Computer Science.

The total number of possible trees on n nodes can then be expressed as:

 

Page 9: Binary Trees 2 Prof. Sin-Min Lee Department of Computer Science.
Page 10: Binary Trees 2 Prof. Sin-Min Lee Department of Computer Science.
Page 11: Binary Trees 2 Prof. Sin-Min Lee Department of Computer Science.
Page 12: Binary Trees 2 Prof. Sin-Min Lee Department of Computer Science.

Postorder

Template <class T>void Postorder ( BinaryTreeNode<T> *t){ // Postorder traversal of *t. If (t){ Postorder ( t->LeftChild);//do left subtree Postorder(t->RightChild);//do right subtree visit(t); // visit tree root }}Each root is visited after its left and right subtrees have been traversed.

Page 13: Binary Trees 2 Prof. Sin-Min Lee Department of Computer Science.
Page 14: Binary Trees 2 Prof. Sin-Min Lee Department of Computer Science.
Page 15: Binary Trees 2 Prof. Sin-Min Lee Department of Computer Science.

Example

Preorder +*ab/cdInorder a*b+c/dPostorder ab*cd/+

Elements of a binary tree listed in pre-,in-,and postorder.

+

b

* /

dca

Page 16: Binary Trees 2 Prof. Sin-Min Lee Department of Computer Science.

About level order

While loop only if the tree is not emptyFollowing the addition of the children of t to the queue,we attempt to delete an element from the queue. If the queue is empty,Delete throws an OutOfBounds exception. If the queue is not empty,then Delete returns the deleted element in t.

Page 17: Binary Trees 2 Prof. Sin-Min Lee Department of Computer Science.
Page 18: Binary Trees 2 Prof. Sin-Min Lee Department of Computer Science.
Page 19: Binary Trees 2 Prof. Sin-Min Lee Department of Computer Science.
Page 20: Binary Trees 2 Prof. Sin-Min Lee Department of Computer Science.
Page 21: Binary Trees 2 Prof. Sin-Min Lee Department of Computer Science.

Sample tree to illustrate tree traversal

1

2 3

5

10

4

8 9

6

11 12

7

Page 22: Binary Trees 2 Prof. Sin-Min Lee Department of Computer Science.

Tree after four nodes visited in preorder traversal

1

2 3

5

10

4

8 9

6

11 12

7

1

2

3

4

Page 23: Binary Trees 2 Prof. Sin-Min Lee Department of Computer Science.

Tree after left subtree of root visited using preorder traversal

1

2 3

5

10

4

8 9

6

11 12

7

1

2

3

4

6

5 7

Page 24: Binary Trees 2 Prof. Sin-Min Lee Department of Computer Science.

Tree after completed preorder traversal

1

2 3

5

10

4

8 9

6

11 12

7

1

2

3

4

6

5 7

8

9

10 11

12

Page 25: Binary Trees 2 Prof. Sin-Min Lee Department of Computer Science.

Tree visited using inorder traversal

1

2 3

5

10

4

8 9

6

11 12

7

7

4

2

1 3

5

6

11

9

8 10

12

Page 26: Binary Trees 2 Prof. Sin-Min Lee Department of Computer Science.

Tree visited using postorder traversal

1

2 3

5

10

4

8 9

6

11 12

7

12

6

3

1 2

5

4

11

9

7 8

10

Page 27: Binary Trees 2 Prof. Sin-Min Lee Department of Computer Science.
Page 28: Binary Trees 2 Prof. Sin-Min Lee Department of Computer Science.
Page 29: Binary Trees 2 Prof. Sin-Min Lee Department of Computer Science.

Here we will work through an example showing how a preorder traversal can be obtained by explicit use of a stack. The logic

follows (see the text for the code):

1.Stack the root node. 2.Exit with completion if the stack is

empty. 3.Pop the stack and visit the node

obtained. 4.Push the right child, if it exists, on

the stack. 5.Push the left child, if it exists, on

the stack. 6.Go to 2.

Page 30: Binary Trees 2 Prof. Sin-Min Lee Department of Computer Science.
Page 31: Binary Trees 2 Prof. Sin-Min Lee Department of Computer Science.
Page 32: Binary Trees 2 Prof. Sin-Min Lee Department of Computer Science.

Using the runtime stack via recursion, we can write simple definitions of the three depth-first traversals without use of an

explicitstack (in these definitions I leave out the templates for

brevity):

void Preorder(TreeNode *p) { if (p) {

Visit(p); Preorder(p->left);

Preorder(p->right); }}

Page 33: Binary Trees 2 Prof. Sin-Min Lee Department of Computer Science.
Page 34: Binary Trees 2 Prof. Sin-Min Lee Department of Computer Science.
Page 35: Binary Trees 2 Prof. Sin-Min Lee Department of Computer Science.
Page 36: Binary Trees 2 Prof. Sin-Min Lee Department of Computer Science.
Page 37: Binary Trees 2 Prof. Sin-Min Lee Department of Computer Science.
Page 38: Binary Trees 2 Prof. Sin-Min Lee Department of Computer Science.
Page 39: Binary Trees 2 Prof. Sin-Min Lee Department of Computer Science.

void Inorder(TreeNode *p) { if (p) {

Inorder(p->left); Visit(p);

Inorder(p->right); }}

void Postorder(TreeNode *p) { if (p) {

Postorder(p->left); Postorder(p->right);

Visit(p); }}

Page 40: Binary Trees 2 Prof. Sin-Min Lee Department of Computer Science.
Page 41: Binary Trees 2 Prof. Sin-Min Lee Department of Computer Science.
Page 42: Binary Trees 2 Prof. Sin-Min Lee Department of Computer Science.
Page 43: Binary Trees 2 Prof. Sin-Min Lee Department of Computer Science.
Page 44: Binary Trees 2 Prof. Sin-Min Lee Department of Computer Science.

TREE SORTCombines tree initialization, insertion, and inorder traversal to

generate and print each node in a "sorted" binary tree.1. Initialize: read first item, create root node.

2. For each item after the first: read item and insert in tree.3. Traverse in order, printing each node.

More formally, in pseudocode:def treesort ()

read item root = makebt(null, item, null)

while not end of data read item

insert(root, item) end while

inorder(root)end

Page 45: Binary Trees 2 Prof. Sin-Min Lee Department of Computer Science.

Using tree sort, sort the letters M, A, R, J, K, S, V.

Insertion produces M / \

A R \ \

J S \ \

K V

Traverse in order => A J K M R S V