Chapter 5 Trees: Outline Introduction Representation Of Trees Binary Trees Binary Tree...

91
Chapter 5 Trees: Chapter 5 Trees: Outline Outline Introduction Introduction Representation Of Trees Representation Of Trees Binary Trees Binary Trees Binary Tree Traversals Binary Tree Traversals Additional Binary Tree Operations Additional Binary Tree Operations Threaded Binary Trees Threaded Binary Trees Heaps Heaps Binary Search Trees Binary Search Trees Selection Trees Selection Trees Forests Forests

Transcript of Chapter 5 Trees: Outline Introduction Representation Of Trees Binary Trees Binary Tree...

Page 1: Chapter 5 Trees: Outline  Introduction  Representation Of Trees  Binary Trees  Binary Tree Traversals  Additional Binary Tree Operations  Threaded.

Chapter 5 Trees: OutlineChapter 5 Trees: Outline

IntroductionIntroduction Representation Of TreesRepresentation Of Trees

Binary TreesBinary Trees Binary Tree TraversalsBinary Tree Traversals Additional Binary Tree OperationsAdditional Binary Tree Operations Threaded Binary TreesThreaded Binary Trees HeapsHeaps Binary Search TreesBinary Search Trees Selection TreesSelection Trees ForestsForests

Page 2: Chapter 5 Trees: Outline  Introduction  Representation Of Trees  Binary Trees  Binary Tree Traversals  Additional Binary Tree Operations  Threaded.

Introduction (1/8)Introduction (1/8) A A tree structure means that the data are organized structure means that the data are organized

so that items of information are related by branchesso that items of information are related by branches Examples:Examples:

Page 3: Chapter 5 Trees: Outline  Introduction  Representation Of Trees  Binary Trees  Binary Tree Traversals  Additional Binary Tree Operations  Threaded.

Introduction (2/8)Introduction (2/8)

Definition Definition (recursively): A (recursively): A treetree is a finite set of is a finite set of oonene or or moremore nodes such that nodes such that There is a specially designated node called There is a specially designated node called rootroot.. The remaining nodes are partitioned into The remaining nodes are partitioned into nn>=0 disjoint >=0 disjoint

set set TT11,…,,…,TTnn, where each of these sets is a tree. , where each of these sets is a tree. TT11,…,,…,

TTnn are called the are called the subtreessubtrees of the root. of the root.

Every node in the tree is the root of some subtreEvery node in the tree is the root of some subtreee

Page 4: Chapter 5 Trees: Outline  Introduction  Representation Of Trees  Binary Trees  Binary Tree Traversals  Additional Binary Tree Operations  Threaded.

Introduction (3/8)Introduction (3/8) Some Some TerminologyTerminology

nodenode: the item of information plus the branches to each n: the item of information plus the branches to each node.ode.

degreedegree: the number of subtrees of a node: the number of subtrees of a node degreedegree of a tree of a tree: the maximum of the degree of the node: the maximum of the degree of the node

s in the tree.s in the tree. terminalterminal nodes (or nodes (or leafleaf)): nodes that have degree zero: nodes that have degree zero nonterminalnonterminal nodes nodes: nodes that don’t belong to terminal n: nodes that don’t belong to terminal n

odes.odes. childrenchildren: the roots of the subtrees of a node X are the : the roots of the subtrees of a node X are the chichi

ldrenldren of X of X parentparent: X is the : X is the parentparent of its children. of its children.

Page 5: Chapter 5 Trees: Outline  Introduction  Representation Of Trees  Binary Trees  Binary Tree Traversals  Additional Binary Tree Operations  Threaded.

Introduction (4/8)Introduction (4/8)

Some Some Terminology Terminology (cont’d)(cont’d) siblingssiblings:: children of the same parent are said to be children of the same parent are said to be

siblings.siblings. AncestorsAncestors of a node of a node: all the nodes along the path : all the nodes along the path

from the root to that node.from the root to that node. The The levellevel of a node of a node: defined by letting the root be at : defined by letting the root be at

level one. If a node is at level level one. If a node is at level ll, then it children are at , then it children are at level level l+1l+1..

Height Height (or (or depthdepth)): the maximum level of any node in : the maximum level of any node in the treethe tree

Page 6: Chapter 5 Trees: Outline  Introduction  Representation Of Trees  Binary Trees  Binary Tree Traversals  Additional Binary Tree Operations  Threaded.

Introduction (5/8)Introduction (5/8) ExampleExample

A is the root nodeB is the parent of D and EC is the sibling of BD and E are the children of BD, E, F, G, I are external nodes, or leavesA, B, C, H are internal nodesThe level of E is 3The height (depth) of the tree is 4The degree of node B is 2The degree of the tree is 3The ancestors of node I is A, C, HThe descendants of node C is F, G, H, I

A

B C

H

ID E F G

Level

1

2

3

4

Property: (# edges) = (#nodes) - 1

Page 7: Chapter 5 Trees: Outline  Introduction  Representation Of Trees  Binary Trees  Binary Tree Traversals  Additional Binary Tree Operations  Threaded.

Introduction (6/8)Introduction (6/8) Representation Of TreesRepresentation Of Trees

List RepresentationList Representation we can write of Figure 5.2 as a list in which each of the subtrwe can write of Figure 5.2 as a list in which each of the subtr

ees is also a listees is also a list

( A ( B ( E ( K, L ), F ), C ( G ), D ( H ( M ), I, J ) ) )( A ( B ( E ( K, L ), F ), C ( G ), D ( H ( M ), I, J ) ) ) The root comes first, The root comes first,

followed by a list of sub-treesfollowed by a list of sub-trees

Page 8: Chapter 5 Trees: Outline  Introduction  Representation Of Trees  Binary Trees  Binary Tree Traversals  Additional Binary Tree Operations  Threaded.

Introduction (7/8)Introduction (7/8)

Representation Of Representation Of Trees (cont’d)Trees (cont’d) Left Child-Left Child-

Right Sibling Right Sibling RepresentationRepresentation

Page 9: Chapter 5 Trees: Outline  Introduction  Representation Of Trees  Binary Trees  Binary Tree Traversals  Additional Binary Tree Operations  Threaded.

Introduction (8/8)Introduction (8/8) Representation Of Trees (cont’d)Representation Of Trees (cont’d)

Representation Representation As A Degree As A Degree Two TreeTwo Tree

Page 10: Chapter 5 Trees: Outline  Introduction  Representation Of Trees  Binary Trees  Binary Tree Traversals  Additional Binary Tree Operations  Threaded.

Binary Trees (1/9)Binary Trees (1/9) Binary trees are characterized by the fact that anBinary trees are characterized by the fact that an

y node can have at most two branchesy node can have at most two branches Definition Definition (recursive):(recursive):

A A binary tree binary tree is a finite set of nodes that is either emptis a finite set of nodes that is either empty or consists of a y or consists of a rootroot and and two disjoint binary treestwo disjoint binary trees call called the ed the left subtreeleft subtree and the and the right subtree right subtree

Thus the left subtree and the right subtree are Thus the left subtree and the right subtree are didistinguishedstinguished

Any tree can be transformed into binary treeAny tree can be transformed into binary tree by left child-right sibling representationby left child-right sibling representation

A

B

A

B

Page 11: Chapter 5 Trees: Outline  Introduction  Representation Of Trees  Binary Trees  Binary Tree Traversals  Additional Binary Tree Operations  Threaded.

Binary Trees (2/9)Binary Trees (2/9) The abstract data type of binary treeThe abstract data type of binary tree

Page 12: Chapter 5 Trees: Outline  Introduction  Representation Of Trees  Binary Trees  Binary Tree Traversals  Additional Binary Tree Operations  Threaded.

Binary Trees (3/9)Binary Trees (3/9) Two special kinds of binary trees: Two special kinds of binary trees:

(a) (a) skewed tree,skewed tree, (b) (b) complete binary treecomplete binary tree The all leaf nodes of these trees are on two adjacent levelsThe all leaf nodes of these trees are on two adjacent levels

Page 13: Chapter 5 Trees: Outline  Introduction  Representation Of Trees  Binary Trees  Binary Tree Traversals  Additional Binary Tree Operations  Threaded.

Binary Trees (4/9)Binary Trees (4/9) Properties of binary treesProperties of binary trees

Lemma 5.1 [Lemma 5.1 [Maximum number of nodesMaximum number of nodes]:]:

1.1. The maximum number of nodes on The maximum number of nodes on level level ii of a binary of a binary tree is tree is 22ii-1-1, , ii 1.1.

2.2. The maximum number of nodes in a The maximum number of nodes in a binary tree of binary tree of depth depth kk is is 22kk-1-1, , kk1.1.

Lemma 5.2 [Lemma 5.2 [Relation between number of leaf Relation between number of leaf nodes and degree-2 nodesnodes and degree-2 nodes]:]:

For any nonempty binary tree, T, if For any nonempty binary tree, T, if nn00 is the number is the number

of of leaf nodesleaf nodes and and nn22 is the number of nodes of is the number of nodes of

degree 2degree 2, then , then nn00 = n = n22 + 1 + 1..

These lemmas allow us to define full and These lemmas allow us to define full and complete binary treescomplete binary trees

Page 14: Chapter 5 Trees: Outline  Introduction  Representation Of Trees  Binary Trees  Binary Tree Traversals  Additional Binary Tree Operations  Threaded.

Binary Trees (5/9)Binary Trees (5/9) Definition:Definition:

A A full binary treefull binary tree of depth of depth kk is a binary tree of death is a binary tree of death kk ha having ving 22kk-1 nodes, k -1 nodes, k 0 0..

A binary tree with n nodes and depth k is A binary tree with n nodes and depth k is complete complete iff iff its its nodes correspond to the nodes nodes correspond to the nodes numbered from 1 to n in numbered from 1 to n in the full binary treethe full binary tree of depth k. of depth k.

From Lemma 5.1, the From Lemma 5.1, the height of a complete height of a complete binary tree with binary tree with nn nodes nodes is is loglog22((nn+1)+1)

Page 15: Chapter 5 Trees: Outline  Introduction  Representation Of Trees  Binary Trees  Binary Tree Traversals  Additional Binary Tree Operations  Threaded.

Binary Trees (6/9)Binary Trees (6/9) Binary tree representations (using array)Binary tree representations (using array)

Lemma 5.3:Lemma 5.3: If a complete binary tree with If a complete binary tree with nn nodes i nodes is represented sequentially, then for any node with ins represented sequentially, then for any node with index dex ii, 1 , 1 i i nn, we have, we have

1.1. parent(i)parent(i) is at is at i i /2/2 if if ii 1. 1. If If ii = 1, = 1, ii is at the root and has no parent. is at the root and has no parent.

2.2. LeftChild(i)LeftChild(i) is at is at 22ii if 2if 2i i nn. . If 2 If 2i i nn, then , then ii has no left child. has no left child.

3.3. RightChild(i)RightChild(i) is at is at 22ii+1 +1 if 2if 2ii+1 +1 nn. . If 2 If 2i i +1+1 nn, then , then ii has no left child has no left child

[1] [2] [3] [4] [5] [6] [7]

A B C — D — E

Level 1Level 2 Level 3

A

B

D

C

E

1

2 3

4 5 6 7

Page 16: Chapter 5 Trees: Outline  Introduction  Representation Of Trees  Binary Trees  Binary Tree Traversals  Additional Binary Tree Operations  Threaded.

Binary Trees (7/9)Binary Trees (7/9) Binary tree representations (using array)Binary tree representations (using array)

Waste spaces: in the worst case, a skewed tree of depth Waste spaces: in the worst case, a skewed tree of depth kk requires 2 requires 2kk-1 spaces. Of these, -1 spaces. Of these, only only kk spaces will be spaces will be occupiedoccupied

Insertion or deletion Insertion or deletion of nodes from the of nodes from the middle of a tree middle of a tree requires the requires the movement of movement of potentially many nodespotentially many nodes to reflect the to reflect the change in change in the levelthe level of these nodes of these nodes

Page 17: Chapter 5 Trees: Outline  Introduction  Representation Of Trees  Binary Trees  Binary Tree Traversals  Additional Binary Tree Operations  Threaded.

Binary Trees (8/9)Binary Trees (8/9)

Binary tree representations (using link)Binary tree representations (using link)

Page 18: Chapter 5 Trees: Outline  Introduction  Representation Of Trees  Binary Trees  Binary Tree Traversals  Additional Binary Tree Operations  Threaded.

Binary Trees (9/9)Binary Trees (9/9) Binary tree representations (using link)Binary tree representations (using link)

Page 19: Chapter 5 Trees: Outline  Introduction  Representation Of Trees  Binary Trees  Binary Tree Traversals  Additional Binary Tree Operations  Threaded.

Binary Tree Traversals (1/9)Binary Tree Traversals (1/9) How to traverse a tree or visit each node in the trHow to traverse a tree or visit each node in the tr

ee exactly once?ee exactly once? There are six possible combinations of traversalThere are six possible combinations of traversal

LVR, LRV, VLR, VRL, RVL, RLVLVR, LRV, VLR, VRL, RVL, RLV Adopt convention that we traverse left before Adopt convention that we traverse left before

right, only 3 traversals remainright, only 3 traversals remain

LLVVR (R (ininorder), LRorder), LRVV ( (postpostorder), order), VVLR (LR (prepreorder)order)

data right_childleft_child

L: moving left R: moving rightV:

visitingnode

Page 20: Chapter 5 Trees: Outline  Introduction  Representation Of Trees  Binary Trees  Binary Tree Traversals  Additional Binary Tree Operations  Threaded.

Binary Tree Traversals (2/9)Binary Tree Traversals (2/9) Arithmetic Expression using binary treeArithmetic Expression using binary tree

inorder traversal (infix expression)

A / B * C * D + E preorder traversal (prefix expression)

+ * * / A B C D E postorder traversal

(postfix expression)

A B / C * D * E + level order traversal

+ * E * D / C A B

Page 21: Chapter 5 Trees: Outline  Introduction  Representation Of Trees  Binary Trees  Binary Tree Traversals  Additional Binary Tree Operations  Threaded.

Binary Tree Traversals (3/9)Binary Tree Traversals (3/9) Inorder traversal (Inorder traversal (LVRLVR) (recursive version)) (recursive version)

LVR

ptr

output:

A / B* C * D + E

Page 22: Chapter 5 Trees: Outline  Introduction  Representation Of Trees  Binary Trees  Binary Tree Traversals  Additional Binary Tree Operations  Threaded.

Binary Tree Traversals (4/9)Binary Tree Traversals (4/9) Preorder traversal (Preorder traversal (VLRVLR) (recursive ) (recursive

version)version)

VLR

output:

A/ B* C* D+ E

Page 23: Chapter 5 Trees: Outline  Introduction  Representation Of Trees  Binary Trees  Binary Tree Traversals  Additional Binary Tree Operations  Threaded.

Binary Tree Traversals (5/9)Binary Tree Traversals (5/9) Postorder traversal (Postorder traversal (LRVLRV) (recursive versio) (recursive versio

n)n)

LRV

output:

A /B *C * D +E

Page 24: Chapter 5 Trees: Outline  Introduction  Representation Of Trees  Binary Trees  Binary Tree Traversals  Additional Binary Tree Operations  Threaded.

Binary Tree Traversals (6/9)Binary Tree Traversals (6/9) Iterative inorder traversal Iterative inorder traversal

we use a stack to simulate recursionwe use a stack to simulate recursion

L

V

R

1

+

nodeoutput:

A /B*C *D +E

2

*

3

*

4

/

5

A

8

B

11

C

14

D

17

E

Page 25: Chapter 5 Trees: Outline  Introduction  Representation Of Trees  Binary Trees  Binary Tree Traversals  Additional Binary Tree Operations  Threaded.

Binary Tree Traversals (7/9)Binary Tree Traversals (7/9)

Analysis of inorder2 (Non-recursive Inorder traveAnalysis of inorder2 (Non-recursive Inorder traversal)rsal) Let Let nn be the number of nodes in the tree be the number of nodes in the tree Time complexity: O(Time complexity: O(nn))

Every node of the tree is placed on and removed Every node of the tree is placed on and removed from the stack exactly oncefrom the stack exactly once

Space complexity: O(Space complexity: O(nn)) equal to the depth of the tree which equal to the depth of the tree which

(skewed tree is the worst case)(skewed tree is the worst case)

Page 26: Chapter 5 Trees: Outline  Introduction  Representation Of Trees  Binary Trees  Binary Tree Traversals  Additional Binary Tree Operations  Threaded.

Binary Tree Traversals (8/9)Binary Tree Traversals (8/9)

Level-order traversalLevel-order traversal method:method:

We visit the root first, then the root’s left child, followed by the We visit the root first, then the root’s left child, followed by the root’s right child. root’s right child.

We continue in this manner, visiting the nodes at each new We continue in this manner, visiting the nodes at each new level from the leftmost node to the rightmost nodeslevel from the leftmost node to the rightmost nodes

This traversal requires a queue to implementThis traversal requires a queue to implement

Page 27: Chapter 5 Trees: Outline  Introduction  Representation Of Trees  Binary Trees  Binary Tree Traversals  Additional Binary Tree Operations  Threaded.

Binary Tree Traversals (9/9)Binary Tree Traversals (9/9) Level-order traversal (using queue)Level-order traversal (using queue)

FIFO

1

+

ptr

output:

A/ B* C* D+ E

2

*

17

E

3

*

14

D

4

/

11

C

5

A

8

B

Page 28: Chapter 5 Trees: Outline  Introduction  Representation Of Trees  Binary Trees  Binary Tree Traversals  Additional Binary Tree Operations  Threaded.

Additional Binary Tree Operations (1/7)Additional Binary Tree Operations (1/7) Copying Binary TreesCopying Binary Trees

we can modify the we can modify the postorder traversalpostorder traversal algorithm only s algorithm only slightly to copy the binary treelightly to copy the binary tree

similar as Program 5.3

Page 29: Chapter 5 Trees: Outline  Introduction  Representation Of Trees  Binary Trees  Binary Tree Traversals  Additional Binary Tree Operations  Threaded.

Additional Binary Tree Operations (2/7)Additional Binary Tree Operations (2/7)

Testing EqualityTesting Equality Binary trees are equivalent if they have the same Binary trees are equivalent if they have the same

topology and the information in corresponding nodes topology and the information in corresponding nodes is identicalis identical

the same topology and data as Program 5.6

LV R

Page 30: Chapter 5 Trees: Outline  Introduction  Representation Of Trees  Binary Trees  Binary Tree Traversals  Additional Binary Tree Operations  Threaded.

Additional Binary Tree Operations (3/7)Additional Binary Tree Operations (3/7)

Variables: Variables: xx11, , xx22, …, , …, xxnn can hold only of two possi can hold only of two possi

ble values, ble values, truetrue or or falsefalse Operators: Operators: ((andand), ), ((oror), ¬(), ¬(notnot)) Propositional Calculus ExpressionPropositional Calculus Expression

A variable is an expressionA variable is an expression If If xx and and yy are expressions, then ¬ are expressions, then ¬xx, , xxyy, , xxyy are expre are expre

ssionsssions Parentheses can be used to alter the normal order of Parentheses can be used to alter the normal order of

evaluation (evaluation (¬ >> >> )) Example: Example: xx11 ( (xx22 ¬ ¬xx33))

Page 31: Chapter 5 Trees: Outline  Introduction  Representation Of Trees  Binary Trees  Binary Tree Traversals  Additional Binary Tree Operations  Threaded.

Additional Binary Tree Operations (4/7)Additional Binary Tree Operations (4/7)

Satisfiability problem:Satisfiability problem: Is there an assignment to make an expression true?Is there an assignment to make an expression true?

Solution for the Example Solution for the Example xx11 ( (xx22 ¬ ¬xx33) :) : If If xx11 and and xx33 are are falsefalse and and xx22 is is truetrue

falsefalse ( (truetrue ¬ ¬falsefalse) = ) = falsefalse truetrue = = truetrue

For For nn value of an expression, there are 2 value of an expression, there are 2nn possib possible combinations of le combinations of truetrue and and falsefalse

Page 32: Chapter 5 Trees: Outline  Introduction  Representation Of Trees  Binary Trees  Binary Tree Traversals  Additional Binary Tree Operations  Threaded.

(x1 ¬x2) (¬ x1 x3) ¬x3

postorder traversal

X3X1

X2 X1

X3

data

value

Additional Binary Tree Operations (5/7)Additional Binary Tree Operations (5/7)

Page 33: Chapter 5 Trees: Outline  Introduction  Representation Of Trees  Binary Trees  Binary Tree Traversals  Additional Binary Tree Operations  Threaded.

Additional Binary Tree Operations (6/7)Additional Binary Tree Operations (6/7) node structurenode structure

For the purpose of our evaluation algorithm, we For the purpose of our evaluation algorithm, we assume each node has four fields:assume each node has four fields:

We define this node structure in C as:We define this node structure in C as:

Page 34: Chapter 5 Trees: Outline  Introduction  Representation Of Trees  Binary Trees  Binary Tree Traversals  Additional Binary Tree Operations  Threaded.

Additional Binary Tree Operations (7/7)Additional Binary Tree Operations (7/7) Satisfiability function

To evaluate the tree is To evaluate the tree is easily obtained by easily obtained by modifying the original modifying the original recursive postorder recursive postorder traversaltraversal

LRV

F

T

T

T

FTRUE

TRUE

TRUE

FALSE

FALSE FALSE

FALSE

FALSE

TRUE

TRUE

TRUE

TRUEnode

Page 35: Chapter 5 Trees: Outline  Introduction  Representation Of Trees  Binary Trees  Binary Tree Traversals  Additional Binary Tree Operations  Threaded.

Threaded Binary Trees (1/10)Threaded Binary Trees (1/10)

ThreadsThreads Do you find any drawback of the above tree?Do you find any drawback of the above tree? Too many null pointers in current representation of Too many null pointers in current representation of

binary treesbinary trees

n: number of nodes

number of non-null links: n-1

total links: 2n

null links: 2n-(n-1) = n+1 Solution: replace these null pointers with some useful Solution: replace these null pointers with some useful

“threads”“threads”

Page 36: Chapter 5 Trees: Outline  Introduction  Representation Of Trees  Binary Trees  Binary Tree Traversals  Additional Binary Tree Operations  Threaded.

Threaded Binary Trees (2/10)Threaded Binary Trees (2/10)

Rules for constructing the threadsRules for constructing the threads If If ptr->left_childptr->left_child is null, is null,

replace it with a pointer to the node that would be visitreplace it with a pointer to the node that would be visited ed beforebefore ptrptr in anin an inorder traversalinorder traversal

If If ptr->right_childptr->right_child is null, is null, replace it with a pointer to the node that would be visitreplace it with a pointer to the node that would be visited ed afterafter ptrptr in anin an inorder traversalinorder traversal

Page 37: Chapter 5 Trees: Outline  Introduction  Representation Of Trees  Binary Trees  Binary Tree Traversals  Additional Binary Tree Operations  Threaded.

Threaded Binary Trees (3/10)Threaded Binary Trees (3/10) A Threaded Binary Tree

A

C

G

I

D

H

Fdangling

dangling

H D I B E A F C G

inorder traversal:

E tt

B ff

t: true threadf: false child

root

Page 38: Chapter 5 Trees: Outline  Introduction  Representation Of Trees  Binary Trees  Binary Tree Traversals  Additional Binary Tree Operations  Threaded.

Threaded Binary Threaded Binary Trees (4/10)Trees (4/10)

Two additional fields of the node structure, Two additional fields of the node structure, left-threadleft-thread and and right-threadright-thread If ptr->left-thread=If ptr->left-thread=TRUETRUE, ,

then ptr->left-child contains a thread; then ptr->left-child contains a thread; Otherwise it contains a pointer to the left child.Otherwise it contains a pointer to the left child. Similarly for the right-threadSimilarly for the right-thread

Page 39: Chapter 5 Trees: Outline  Introduction  Representation Of Trees  Binary Trees  Binary Tree Traversals  Additional Binary Tree Operations  Threaded.

Threaded Binary Trees (5/10)Threaded Binary Trees (5/10) If we don’t want the left pointer of If we don’t want the left pointer of HH and the right and the right

pointer of pointer of GG to be dangling pointers, we may to be dangling pointers, we may create root node and assign them create root node and assign them pointing to the pointing to the root noderoot node

Page 40: Chapter 5 Trees: Outline  Introduction  Representation Of Trees  Binary Trees  Binary Tree Traversals  Additional Binary Tree Operations  Threaded.

Threaded Binary Trees (6/10)Threaded Binary Trees (6/10)

Inorder traversal of a threaded binary treeInorder traversal of a threaded binary tree By using of threads we can perform an inorder traveBy using of threads we can perform an inorder trave

rsal without making use of a stack (simplifying the tarsal without making use of a stack (simplifying the task)sk)

Now, we can follow the thread of any node, Now, we can follow the thread of any node, ptrptr, to th, to the “next” node of inorder traversale “next” node of inorder traversal

1.1. If If ptrptr->->right_threadright_thread = = TRUETRUE, the inorder successor o, the inorder successor of f ptrptr is is ptr->right_childptr->right_child by definition of the threads by definition of the threads

2.2. Otherwise we obtain the inorder successor of Otherwise we obtain the inorder successor of ptrptr by by following a path of left-child linksfollowing a path of left-child links from the from the right-child right-child of of ptrptr until we reach a until we reach a nodenode with with left_threadleft_thread = = TRUETRUE

Page 41: Chapter 5 Trees: Outline  Introduction  Representation Of Trees  Binary Trees  Binary Tree Traversals  Additional Binary Tree Operations  Threaded.

Threaded Binary Trees (7/10)Threaded Binary Trees (7/10) Finding the inorder successor (next node) of a nodeFinding the inorder successor (next node) of a node

threaded_pointer insucc(threaded_pointer tree){threaded_pointer insucc(threaded_pointer tree){threaded_pointer temp;threaded_pointer temp;temp = tree->right_child;temp = tree->right_child;if (!tree->right_thread)if (!tree->right_thread)

while (!temp->left_thread) while (!temp->left_thread) temp = temp->left_child;temp = temp->left_child;

return temp;return temp;}}

Inorder

tree

temp

Page 42: Chapter 5 Trees: Outline  Introduction  Representation Of Trees  Binary Trees  Binary Tree Traversals  Additional Binary Tree Operations  Threaded.

Inorder traversal of a threaded binary treeInorder traversal of a threaded binary treevoid tinorder(threaded_pointer tree){void tinorder(threaded_pointer tree){

/* traverse the threaded binary tree inorder *//* traverse the threaded binary tree inorder */

threaded_pointer temp = tree;threaded_pointer temp = tree;

for (;;) {for (;;) {

temp = temp = insucc(temp);(temp);

if (temp==tree)if (temp==tree)

break;break;

printf(“%3c”,temp->data);printf(“%3c”,temp->data);

}}

}}

Threaded Binary Trees (8/10)Threaded Binary Trees (8/10)

Time Complexity: O(n)

tree

output:

FC GH D I B E A

Page 43: Chapter 5 Trees: Outline  Introduction  Representation Of Trees  Binary Trees  Binary Tree Traversals  Additional Binary Tree Operations  Threaded.

Threaded Binary Trees (9/10)Threaded Binary Trees (9/10)

Inserting A Node Into A Threaded Binary TreeInserting A Node Into A Threaded Binary Tree Insert Insert childchild as the as the right childright child of node of node parentparent

1.1. change change parent->right_threadparent->right_thread to to FALSEFALSE

2.2. set set child->left_threadchild->left_thread and and child->right_threadchild->right_thread to to TRTRUEUE

3.3. set set child->left_childchild->left_child to point to to point to parentparent

4.4. set set child->right_childchild->right_child to to parent->right_childparent->right_child

5.5. change change parent->right_childparent->right_child to point to to point to childchild

Page 44: Chapter 5 Trees: Outline  Introduction  Representation Of Trees  Binary Trees  Binary Tree Traversals  Additional Binary Tree Operations  Threaded.

Threaded Binary Trees (10/10)Threaded Binary Trees (10/10) Right insertion in a threaded binary treeRight insertion in a threaded binary tree

void insert_right(thread_pointer parent, threaded_pointer child){void insert_right(thread_pointer parent, threaded_pointer child){/* insert child as the right child of parent in a threaded binary tree *//* insert child as the right child of parent in a threaded binary tree */

threaded_pointer temp;threaded_pointer temp;child->right_child = parent->right_child;child->right_child = parent->right_child;child->right_thread = parent->right_thread;child->right_thread = parent->right_thread;child->left_child = parent;child->left_child = parent;child->left_thread = TRUE;child->left_thread = TRUE;parent->right_child = child;parent->right_child = child;parent->right_thread = FALSE;parent->right_thread = FALSE;If(!child->right_thread){If(!child->right_thread){

temp = insucc(child);temp = insucc(child);temp->left_child = child;temp->left_child = child;

}}}}

X

A

B

C

root

child

parent

A

B

C

childparent

D

E Fsuccessor

temp

First CaseSecond Case

X

Page 45: Chapter 5 Trees: Outline  Introduction  Representation Of Trees  Binary Trees  Binary Tree Traversals  Additional Binary Tree Operations  Threaded.

Heaps (1/6)Heaps (1/6)

The heap abstract data typeThe heap abstract data type Definition: Definition: A A maxmax((minmin) ) treetree is a tree in which the key is a tree in which the key

value in each node is value in each node is no smallerno smaller ( (largerlarger) than the key ) than the key values in its children. Avalues in its children. A maxmax ((minmin) ) heapheap is a is a complete binary tree that is also a that is also a maxmax ((minmin) ) treetree

Basic Operations:Basic Operations: creation of an empty heapcreation of an empty heap insertion of a new elemrnt into a heapinsertion of a new elemrnt into a heap deletion of the largest element from the heapdeletion of the largest element from the heap

Page 46: Chapter 5 Trees: Outline  Introduction  Representation Of Trees  Binary Trees  Binary Tree Traversals  Additional Binary Tree Operations  Threaded.

Heaps (2/6)Heaps (2/6) The examples of max heaps and min heapsThe examples of max heaps and min heaps

Property: The root of max heap (min heap) contains the largest (smallest) element

Page 47: Chapter 5 Trees: Outline  Introduction  Representation Of Trees  Binary Trees  Binary Tree Traversals  Additional Binary Tree Operations  Threaded.

Heaps (3/6)Heaps (3/6) Abstract data type of Max HeapAbstract data type of Max Heap

Page 48: Chapter 5 Trees: Outline  Introduction  Representation Of Trees  Binary Trees  Binary Tree Traversals  Additional Binary Tree Operations  Threaded.

Heaps (4/6)Heaps (4/6) Queue in Chapter 3: FIFOQueue in Chapter 3: FIFO Priority queuesPriority queues

Heaps are frequently used to implement Heaps are frequently used to implement priority queuespriority queues delete the element with highest (lowest) prioritydelete the element with highest (lowest) priority insert the element with arbitrary priorityinsert the element with arbitrary priority Heaps is the only way to implement priority queueHeaps is the only way to implement priority queue

machine service:amount of time(min heap)amount of payment(max heap)factory:time tag

Page 49: Chapter 5 Trees: Outline  Introduction  Representation Of Trees  Binary Trees  Binary Tree Traversals  Additional Binary Tree Operations  Threaded.

Heaps (5/6)Heaps (5/6) Insertion Into A Max HeapInsertion Into A Max Heap

Analysis of Analysis of insert_max_heapinsert_max_heap The complexity of the insertion function is O(logThe complexity of the insertion function is O(log22 nn))

parent sinkitem upheap

15 2

14 10

[1]

[2]

[3]

[4]

[5]

[6]

[7]

*n=

i=

5

6

2

3

5

insert 521

6

7

5

3

20

20

1

21

Page 50: Chapter 5 Trees: Outline  Introduction  Representation Of Trees  Binary Trees  Binary Tree Traversals  Additional Binary Tree Operations  Threaded.

<

Heaps (6/6)Heaps (6/6) Deletion from a max heapDeletion from a max heap After deletion, the After deletion, the

heap is still a heap is still a complete binary treecomplete binary tree

Analysis of Analysis of delete_max_heapdelete_max_heap The complexity of the The complexity of the

insertion function insertion function is O(logis O(log22 nn))

15 2

14 10

[1]

[2]

[3]

[5]

20

parent =child =

item.key =temp.key =

[4]

*n=

2010

12

15

542

4

10

14

48

Page 51: Chapter 5 Trees: Outline  Introduction  Representation Of Trees  Binary Trees  Binary Tree Traversals  Additional Binary Tree Operations  Threaded.

Binary Search Trees (1/8)Binary Search Trees (1/8)

Why do binary search trees need?Why do binary search trees need? Heap is not suited for applications in which arbitrary elHeap is not suited for applications in which arbitrary el

ements are to be deleted from the element listements are to be deleted from the element list a min (max) element is deleteda min (max) element is deleted O(log2n)

deletion of an arbitrary elementdeletion of an arbitrary element O(n) search for an arbitrary elementsearch for an arbitrary element O(n)

DefinitionDefinition of binary search tree: of binary search tree: Every element has a unique keyEvery element has a unique key The keys in a nonemptyThe keys in a nonempty left subtree left subtree ((right subtreeright subtree) are) are

smaller smaller ((largerlarger) than the key in the root of subtree) than the key in the root of subtree The left and right subtrees are also binary search treesThe left and right subtrees are also binary search trees

Page 52: Chapter 5 Trees: Outline  Introduction  Representation Of Trees  Binary Trees  Binary Tree Traversals  Additional Binary Tree Operations  Threaded.

Binary Search Trees (2/8)Binary Search Trees (2/8) Example: (b) and (c) are binary search treesExample: (b) and (c) are binary search trees

medium

largersmaller

Page 53: Chapter 5 Trees: Outline  Introduction  Representation Of Trees  Binary Trees  Binary Tree Traversals  Additional Binary Tree Operations  Threaded.

44

32 65

88

28

17

80

76

97

8254

29

Search(25) Search(76)

Binary Search Trees Binary Search Trees (3/8)(3/8)

Search:Search:

Page 54: Chapter 5 Trees: Outline  Introduction  Representation Of Trees  Binary Trees  Binary Tree Traversals  Additional Binary Tree Operations  Threaded.

Binary Search Trees (4/8)Binary Search Trees (4/8) Searching a Searching a

binary search binary search treetree

O(h)

Page 55: Chapter 5 Trees: Outline  Introduction  Representation Of Trees  Binary Trees  Binary Tree Traversals  Additional Binary Tree Operations  Threaded.

Binary Search Trees (5/8)Binary Search Trees (5/8) Inserting into a binary search treeInserting into a binary search tree

An empty tree

Page 56: Chapter 5 Trees: Outline  Introduction  Representation Of Trees  Binary Trees  Binary Tree Traversals  Additional Binary Tree Operations  Threaded.

Binary Search Trees (6/8)Binary Search Trees (6/8) Deletion from a binary search treeDeletion from a binary search tree

Three cases should be considered Three cases should be considered case 1. leaf case 1. leaf delete delete case 2. case 2.

one child one child delete and change the pointer to this child delete and change the pointer to this child case 3. two child case 3. two child either the smallest element in the right either the smallest element in the right

subtree or the largest element in the left subtreesubtree or the largest element in the left subtree

Page 57: Chapter 5 Trees: Outline  Introduction  Representation Of Trees  Binary Trees  Binary Tree Traversals  Additional Binary Tree Operations  Threaded.

Binary Search Trees (7/8)Binary Search Trees (7/8)

Height of a binary search treeHeight of a binary search tree The height of a binary search tree with The height of a binary search tree with nn elements elements

can become as large as can become as large as nn.. It can be shown that when insertions and deletions It can be shown that when insertions and deletions

are made at random, the height of the binary search are made at random, the height of the binary search tree is O(logtree is O(log22nn) on the average.) on the average.

Search trees with a worst-case height of O(logSearch trees with a worst-case height of O(log22nn) are ) are

called called balance search treesbalance search trees

Page 58: Chapter 5 Trees: Outline  Introduction  Representation Of Trees  Binary Trees  Binary Tree Traversals  Additional Binary Tree Operations  Threaded.

Binary Search Trees (8/8)Binary Search Trees (8/8)

Time ComplexityTime Complexity Searching, insertion, removalSearching, insertion, removal

O(O(hh), where h is the height of the tree), where h is the height of the tree

Worst case - skewed binary treeWorst case - skewed binary tree O(O(nn), where n is the # of internal nodes), where n is the # of internal nodes

Prevent worst casePrevent worst case rebalancing schemerebalancing scheme AVL, 2-3, and Red-black treeAVL, 2-3, and Red-black tree

Page 59: Chapter 5 Trees: Outline  Introduction  Representation Of Trees  Binary Trees  Binary Tree Traversals  Additional Binary Tree Operations  Threaded.

Selection Trees (1/6)Selection Trees (1/6)

Problem:Problem: suppose we have suppose we have kk order sequences, called runs, that order sequences, called runs, that

are to be merged into a single ordered sequenceare to be merged into a single ordered sequence

Solution:Solution: straightforward : straightforward : kk-1 comparison-1 comparison selection tree :selection tree : loglog22kk+1+1

There are two kinds of selection trees: There are two kinds of selection trees: winner treeswinner trees and and loser treesloser trees

Page 60: Chapter 5 Trees: Outline  Introduction  Representation Of Trees  Binary Trees  Binary Tree Traversals  Additional Binary Tree Operations  Threaded.

Selection Trees (2/6)Selection Trees (2/6)

Definition: (Winner tree)Definition: (Winner tree) aa selection tree is the binary tree where each node selection tree is the binary tree where each node

represents the smaller of its two childrenrepresents the smaller of its two children root node is the smallest node in the treeroot node is the smallest node in the tree a winner is the record with smaller keya winner is the record with smaller key

Rules:Rules: tournament : between sibling nodestournament : between sibling nodes putput X X in the parent node in the parent node X X tree tree

wherewhere X X = = winner winner oror loser loser

Page 61: Chapter 5 Trees: Outline  Introduction  Representation Of Trees  Binary Trees  Binary Tree Traversals  Additional Binary Tree Operations  Threaded.

Selection Trees (3/6)Selection Trees (3/6) Winner TreeWinner Tree

sequential allocationscheme(completebinary tree)

Each node representsthe smaller of its two

children

ordered sequence

Page 62: Chapter 5 Trees: Outline  Introduction  Representation Of Trees  Binary Trees  Binary Tree Traversals  Additional Binary Tree Operations  Threaded.

Selection Trees (4/6)Selection Trees (4/6)

Analysis of merging runs using winner treesAnalysis of merging runs using winner trees # of levels: # of levels: loglog22KK +1 +1 restructure time: O(log restructure time: O(log22KK))

merge time: O(merge time: O(nnloglog22KK))

setup time: O(K)setup time: O(K) merge time: O(nlogmerge time: O(nlog22K)K)

Slight modification:Slight modification: tree of loser tree of loser consider the parent node only (vs. sibling nodes)consider the parent node only (vs. sibling nodes)

Page 63: Chapter 5 Trees: Outline  Introduction  Representation Of Trees  Binary Trees  Binary Tree Traversals  Additional Binary Tree Operations  Threaded.

Selection Trees (5/6)Selection Trees (5/6) After one record has been outputAfter one record has been output

6

6

6

15

6

Page 64: Chapter 5 Trees: Outline  Introduction  Representation Of Trees  Binary Trees  Binary Tree Traversals  Additional Binary Tree Operations  Threaded.

Selection Trees (6/6)Selection Trees (6/6) Tree of losers can be conducted by Winner treeTree of losers can be conducted by Winner tree

6

0

8

9 17

10 20 9 90

Page 65: Chapter 5 Trees: Outline  Introduction  Representation Of Trees  Binary Trees  Binary Tree Traversals  Additional Binary Tree Operations  Threaded.

Forests (1/4)Forests (1/4) Definition:Definition:

A A forestforest is a set of is a set of nn 0 disjoint trees 0 disjoint trees

Transforming a forest into a binary treeTransforming a forest into a binary tree Definition: If Definition: If TT11,…,,…,TTnn is a forest of trees, then the binary is a forest of trees, then the binary

tree corresponding to this forest, denoted by tree corresponding to this forest, denoted by BB((TT11,…,,…,TTnn

): ): is empty, if is empty, if nn = 0 = 0

has root equal to root(has root equal to root(TT11); has); has left subtree equal to B(left subtree equal to B(TT11

11,,TT1212,…,,…,TT11mm); and has right subtree equal to B(); and has right subtree equal to B(TT22,,TT33,…,,…,TT

nn)) where where TT1111,,TT1212,…,,…,TT11mm are the subtrees of root ( are the subtrees of root (TT11))

Page 66: Chapter 5 Trees: Outline  Introduction  Representation Of Trees  Binary Trees  Binary Tree Traversals  Additional Binary Tree Operations  Threaded.

Forests (2/4)Forests (2/4)

Rotate the tree clockwise by 45 degrees

A E G

B C D F H I G

H

I

A

B

C

D

F

E

Leftmost child

Right sibling

Page 67: Chapter 5 Trees: Outline  Introduction  Representation Of Trees  Binary Trees  Binary Tree Traversals  Additional Binary Tree Operations  Threaded.

Forests (3/4)Forests (3/4) Forest traversalsForest traversals Forest preorder traversalForest preorder traversal

(1)(1)If If FF is empty, then return. is empty, then return.

(2)(2)Visit the root of the first tree of Visit the root of the first tree of FF..

(3)(3)Traverse the subtrees of the first tree in tree preorder.Traverse the subtrees of the first tree in tree preorder.

(4)(4)Traverse the remaining tree of Traverse the remaining tree of FF in preorder. in preorder.

Forest inorder traversalForest inorder traversal(1)(1)If If FF is empty, then return is empty, then return

(2)(2)Traverse the subtrees of the first tree in tree inorderTraverse the subtrees of the first tree in tree inorder

(3)(3)Visit the root of the first tree of Visit the root of the first tree of FF

(4)(4)Traverse the remaining tree of Traverse the remaining tree of FF in inorder in inorder

Forest postorder traversalForest postorder traversal(1)(1)If If FF is empty, then return is empty, then return

(2)(2)Traverse the subtrees of the first tree in tree postorderTraverse the subtrees of the first tree in tree postorder

(3)(3)Traverse the remaining tree of Traverse the remaining tree of FF in postorder in postorder

(4)(4)Visit the root of the first tree of Visit the root of the first tree of FF

Page 68: Chapter 5 Trees: Outline  Introduction  Representation Of Trees  Binary Trees  Binary Tree Traversals  Additional Binary Tree Operations  Threaded.

preorder: A B C D E F G H Iinorder: B C A E D G H F I

A

B, C E, D, G, H, F, I

A

F, G, H, I

B

C

preorder: A B C (D E F G H I)inorder: B C A (E D G H F I)

D

E

Forests (4/4)Forests (4/4)

Page 69: Chapter 5 Trees: Outline  Introduction  Representation Of Trees  Binary Trees  Binary Tree Traversals  Additional Binary Tree Operations  Threaded.

Set Representation(1/13)Set Representation(1/13)

SS11={0, 6, 7, 8}, S={0, 6, 7, 8}, S22={1, 4, 9}, S={1, 4, 9}, S33={2, 3, 5}={2, 3, 5}

Two operations considered hereTwo operations considered here Disjoint set union Disjoint set union SS11 S S22={0,6,7,8,1,4,9}={0,6,7,8,1,4,9}

FindFind((ii): Find the set containing the element ): Find the set containing the element ii.. 3 3 S S33, 8 , 8 S S11

0

6 7 8

4

1 9

2

3 5

Si Sj =

Page 70: Chapter 5 Trees: Outline  Introduction  Representation Of Trees  Binary Trees  Binary Tree Traversals  Additional Binary Tree Operations  Threaded.

4

1 9

0

6 7 8

4

1 90

6 7 8

Possible representation for S1 union S2

Make one of trees a subtree of the other

Set Representation(2/13)Set Representation(2/13)

Union and Find OperationsUnion and Find Operations

Page 71: Chapter 5 Trees: Outline  Introduction  Representation Of Trees  Binary Trees  Binary Tree Traversals  Additional Binary Tree Operations  Threaded.

0

6 7 8

4

1 9

2

3 5

set name

pointer

S1

S2

S3

*Figure 5.41:Data Representation of S1S2and S3 (p.240)

Set Representation(3/13)Set Representation(3/13)

Page 72: Chapter 5 Trees: Outline  Introduction  Representation Of Trees  Binary Trees  Binary Tree Traversals  Additional Binary Tree Operations  Threaded.

int find1(int i) {int find1(int i) { for(; parent[i] >= 0; i = parent[i]);for(; parent[i] >= 0; i = parent[i]); return i;return i;}}void union1(int i, int j) {void union1(int i, int j) { parent[i] = j;parent[i] = j;}} Program 5.18: Program 5.18: Initial attempt at union-find function (p.241)Initial attempt at union-find function (p.241)

Set Representation(4/13)Set Representation(4/13)

Array Representation for SetArray Representation for Set

i [0] [1] [2] [3] [4] [5] [6] [7] [8] [9]

parent -4 4 -3 2 -3 2 0 0 0 4

Page 73: Chapter 5 Trees: Outline  Introduction  Representation Of Trees  Binary Trees  Binary Tree Traversals  Additional Binary Tree Operations  Threaded.

n-1

n-2

0

*Figure 5.43:Degenerate tree (p.242)

union operationO(n) n-1

find operationO(n2)

ii

n

2

union(0,1), find(0)union(1,2), find(0)...union(n-2,n-1),find(0)

degenerate tree

Set Representation(5/13)Set Representation(5/13)

Page 74: Chapter 5 Trees: Outline  Introduction  Representation Of Trees  Binary Trees  Binary Tree Traversals  Additional Binary Tree Operations  Threaded.

weighting rule for union(i,j): if # of nodes in i < # in j then j the parent of i

Set Representation(6/13)Set Representation(6/13)

Page 75: Chapter 5 Trees: Outline  Introduction  Representation Of Trees  Binary Trees  Binary Tree Traversals  Additional Binary Tree Operations  Threaded.

void union2(int i, int j)void union2(int i, int j){{ int temp = parent[i] + parent[j];int temp = parent[i] + parent[j]; if (parent[i] > parent[j]) {if (parent[i] > parent[j]) { parent[i] = j; parent[i] = j; /*make j the new root*//*make j the new root*/ parent[j] = temp;parent[j] = temp; }} else {else { parent[j] = i;parent[j] = i;/* make i the new root*//* make i the new root*/ parent[i] = temp;parent[i] = temp; }}}}

If the number of nodes in tree i is less than the number in tree j, thenmake j the parent of i; otherwisemake i the parent of j.

Keep a count in the root of tree

Set Representation(7/13)Set Representation(7/13)

Modified Union OperationModified Union Operation

Page 76: Chapter 5 Trees: Outline  Introduction  Representation Of Trees  Binary Trees  Binary Tree Traversals  Additional Binary Tree Operations  Threaded.

Figure 5.45:Trees achieving worst case bound (p.245)

log28+1

Set Representation(8/13)Set Representation(8/13)

Page 77: Chapter 5 Trees: Outline  Introduction  Representation Of Trees  Binary Trees  Binary Tree Traversals  Additional Binary Tree Operations  Threaded.

Set Representation(9/13)Set Representation(9/13)

The definition of Ackermann’s function used The definition of Ackermann’s function used here is :here is :

))1,(,1(

0

0

2

qpApA

q P=0

q=0 and p >= 1

P>=1 and p = 1

p>=1 and q >= 2

A ( p, q) =

Page 78: Chapter 5 Trees: Outline  Introduction  Representation Of Trees  Binary Trees  Binary Tree Traversals  Additional Binary Tree Operations  Threaded.

Int find2(int i) { Int find2(int i) { int root, trail, lead;int root, trail, lead; for (root=i;parent[root]>=0;oot=parent[root])for (root=i;parent[root]>=0;oot=parent[root]) ;; for (trail=i; trail!=root; trail=lead) {for (trail=i; trail!=root; trail=lead) { lead = parent[trail];lead = parent[trail]; parent[trail]= root;parent[trail]= root; }} return root;return root;}}

If j is a node on the path fromi to its root then make j a child of the root

Set Representation(10/13)Set Representation(10/13)

Modified Modified FindFind((ii) Operation) Operation

Page 79: Chapter 5 Trees: Outline  Introduction  Representation Of Trees  Binary Trees  Binary Tree Traversals  Additional Binary Tree Operations  Threaded.

0

1 2 4

3 5 6

7

0

1 2 4

3 5

6 7

find(7) find(7) find(7) find(7) find(7) find(7) find(7) find(7)go up 3 1 1 1 1 1 1 1reset 2 12 moves (vs. 24 moves)

Set Representation(11/13)Set Representation(11/13)

Page 80: Chapter 5 Trees: Outline  Introduction  Representation Of Trees  Binary Trees  Binary Tree Traversals  Additional Binary Tree Operations  Threaded.

Set Representation(12/13)Set Representation(12/13)

ApplicationsApplications Find equivalence class Find equivalence class i i j j Find SFind Sii and S and Sjj such that i such that i S Sii and j and j S Sjj

(two finds)(two finds) SSii = S = Sjj do nothingdo nothing

SSii S Sjj union(Sunion(Sii , S , Sjj))

exampleexample0 0 4, 3 4, 3 1, 6 1, 6 10, 8 10, 8 9, 7 9, 7 4, 6 4, 6 8, 8,3 3 5, 2 5, 2 11, 11 11, 11 0 0{0, 2, 4, 7, 11}, {1, 3, 5}, {6, 8, 9, 10}{0, 2, 4, 7, 11}, {1, 3, 5}, {6, 8, 9, 10}

Page 81: Chapter 5 Trees: Outline  Introduction  Representation Of Trees  Binary Trees  Binary Tree Traversals  Additional Binary Tree Operations  Threaded.

Set Representation(13/13)Set Representation(13/13)

Page 82: Chapter 5 Trees: Outline  Introduction  Representation Of Trees  Binary Trees  Binary Tree Traversals  Additional Binary Tree Operations  Threaded.

Counting Binary trees(1/10)Counting Binary trees(1/10)

Distinct Binary Trees :Distinct Binary Trees : If n=0 or n=1, there is only one binary tree.If n=0 or n=1, there is only one binary tree. If n=2 and n=3,If n=2 and n=3,

Page 83: Chapter 5 Trees: Outline  Introduction  Representation Of Trees  Binary Trees  Binary Tree Traversals  Additional Binary Tree Operations  Threaded.

Counting Binary trees(2/10)Counting Binary trees(2/10) Stack PermutationsStack Permutations

preorder: A B C D E F G H Iinorder: B C A E D G H F IA

B, C D, E, F, G, H, I

A

D, E, F, G, H, IB

C

A

B

C

D

E F

G I

H

Page 84: Chapter 5 Trees: Outline  Introduction  Representation Of Trees  Binary Trees  Binary Tree Traversals  Additional Binary Tree Operations  Threaded.

Counting Binary trees(3/10)Counting Binary trees(3/10) Figure5.49(c) with the node numberingFigure5.49(c) with the node numbering

of Figure 5.50.Its preorder permutationof Figure 5.50.Its preorder permutation

is 1,2…,9, and its inorderis 1,2…,9, and its inorder

permutation is 2,3,1,5,4,permutation is 2,3,1,5,4,

7,8,6,9.7,8,6,9.

1

2

3

4

5 6

7 9

8

Page 85: Chapter 5 Trees: Outline  Introduction  Representation Of Trees  Binary Trees  Binary Tree Traversals  Additional Binary Tree Operations  Threaded.

Counting Binary trees(4/10)Counting Binary trees(4/10) If we start with the numbers1,2,3, then theIf we start with the numbers1,2,3, then the

possible permutations obtainable by a stack are:possible permutations obtainable by a stack are:

(1,2,3) (1,3,2) (2,1,3) (2,3,1) (3,2,1)(1,2,3) (1,3,2) (2,1,3) (2,3,1) (3,2,1) Obtaining(3,1,2) is impossible. Obtaining(3,1,2) is impossible.

1

2

3

1

2

3

1

2 3

1

2

3

1

2

3

Page 86: Chapter 5 Trees: Outline  Introduction  Representation Of Trees  Binary Trees  Binary Tree Traversals  Additional Binary Tree Operations  Threaded.

Counting Binary trees(5/10)Counting Binary trees(5/10)

Matrix MultiplicationMatrix Multiplication Suppose that we wish to compute the product of n matrices:Suppose that we wish to compute the product of n matrices:

MM1 * 1 * MM2 . . .* 2 . . .* MMnn Since matrix multiplication is associative, we can perform theSince matrix multiplication is associative, we can perform the

se multiplications in any order. We would like to know how mse multiplications in any order. We would like to know how many different ways we can perform these multiplications . For any different ways we can perform these multiplications . For example, If n =3, there are two possibilities:example, If n =3, there are two possibilities:

((MM1*1*MM2)*2)*MM33

MM1*(1*(MM2*2*MM3) 3)

Page 87: Chapter 5 Trees: Outline  Introduction  Representation Of Trees  Binary Trees  Binary Tree Traversals  Additional Binary Tree Operations  Threaded.

Counting Binary trees(6/10)Counting Binary trees(6/10) Let be the number of different ways to compute the Let be the number of different ways to compute the

product of n matrices. Then , and . product of n matrices. Then , and . Let be the product .Let be the product .

The product we wish to compute is by computing The product we wish to compute is by computing any one of the products any one of the products

The number of distinct ways to obtain The number of distinct ways to obtain are and ,respectively. Therefore, letting =1, are and ,respectively. Therefore, letting =1, we have:we have:

1

0

1,n

iinin nbbb

nb

,12 b 23 b 54 b

jiM ij , .*...** 1 jii MMM

lnM

.1,* ,11 niMM nii

niiandMM ,11

ib inb ib

Page 88: Chapter 5 Trees: Outline  Introduction  Representation Of Trees  Binary Trees  Binary Tree Traversals  Additional Binary Tree Operations  Threaded.

Counting Binary trees(7/10)Counting Binary trees(7/10)

Now instead let be the number of distinct binary treNow instead let be the number of distinct binary trees with n nodes. Again an expression for in terms es with n nodes. Again an expression for in terms of n is what we want. Than we see that is the sum of n is what we want. Than we see that is the sum of all the possible binary trees formed in the following of all the possible binary trees formed in the following way: a root and two subtrees with and nodes, for way: a root and two subtrees with and nodes, for . This explanation says that . This explanation says that

and and

nb

nb

nb

ib 1 inb

ni 0

1,1

01

nbbb

n

iinin 10 b

bn

bi bn-i-1

Page 89: Chapter 5 Trees: Outline  Introduction  Representation Of Trees  Binary Trees  Binary Tree Traversals  Additional Binary Tree Operations  Threaded.

Counting Binary trees(8/10)Counting Binary trees(8/10) Number of Distinct Binary Trees:Number of Distinct Binary Trees:

To obtain number of distinct binary trees with n To obtain number of distinct binary trees with n nodes, we must solve the recurrence of Eq.(5.5).To nodes, we must solve the recurrence of Eq.(5.5).To begin we let: (5.6)begin we let: (5.6)

Which is the generating function for the number of Which is the generating function for the number of binary trees. Next observe that by the recurrence binary trees. Next observe that by the recurrence relation we get the identity: relation we get the identity:

Using the formula to solve quadratics and the fact Using the formula to solve quadratics and the fact (Eq. (5.5)) that B(0) = = 1 ,we get(Eq. (5.5)) that B(0) = = 1 ,we get

0)(

i

ii xbxB

1)()(2 xbxxB

0b

x

xxB

2

411)(

Page 90: Chapter 5 Trees: Outline  Introduction  Representation Of Trees  Binary Trees  Binary Tree Traversals  Additional Binary Tree Operations  Threaded.

Counting Binary trees(9/10)Counting Binary trees(9/10)

Number of Distinct Binary Trees:Number of Distinct Binary Trees: We can use the binomial theorem to expandWe can use the binomial theorem to expand to obtain:to obtain:

(5.7) (5.7)

2/141 x

n

n

xn

xB0

)4(2/1

12

1)(

0

122)1(1

2/1

m

mmm xm

Page 91: Chapter 5 Trees: Outline  Introduction  Representation Of Trees  Binary Trees  Binary Tree Traversals  Additional Binary Tree Operations  Threaded.

Counting Binary trees(10/10)Counting Binary trees(10/10)

Number of Distinct Binary Trees:Number of Distinct Binary Trees: Comparing Eqs.(5.6) and (5.7) we see that , whiComparing Eqs.(5.6) and (5.7) we see that , whi

ch is the coeffcient of in B(x), is :ch is the coeffcient of in B(x), is :

Some simplification yields the more compact formSome simplification yields the more compact form

which is approximately which is approximately

nbnx

n

n

nbn

2

1

1

12211

2/1

nn

n

)4( 2/3nOb

n

n