29
Basic Algorithms on Trees r v u T Recall that nodes in a tree are organized in a hierarchical structure. If the tree is a family tree, each node belongs to a “generation.” Howcan we find out the number of ancestors for nodes u and v ? Howcan we find out the number of generations belowthe node u ? Howcan we list all tne nodes systematically?

Basic Algorithms on Trees. If the node v is the root, then the depth of the node v is 0. Otherwise, the depth of the node v is one plus the depth of

  • View
    217

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Basic Algorithms on Trees. If the node v is the root, then the depth of the node v is 0. Otherwise, the depth of the node v is one plus the depth of

Basic Algorithms on Trees

r

v

u

T

Recall that nodes in a tree are organized in a hierarchical structure. If the tree is a family tree, each node belongs to a “generation.” How can we find out the number of ancestors for nodes u and v? How can we find out the number of generations below the node u? How can we list all tne nodes systematically?

Page 2: Basic Algorithms on Trees. If the node v is the root, then the depth of the node v is 0. Otherwise, the depth of the node v is one plus the depth of

Depth of node v: The number of ancestors of node v, excluding v itself. Example:

r

v

u

T

The depth of node u is 1. The depth of node v is 2. The depth of the root is 0.

Page 3: Basic Algorithms on Trees. If the node v is the root, then the depth of the node v is 0. Otherwise, the depth of the node v is one plus the depth of

A recursive definition of depth: This definition does not give the depth directly. Rather, it uses the depth of the parent of a given node to express the depth.

• If the node v is the root, then the depth of the node v is 0.

• Otherwise, the depth of the node v is one plus the depth ofthe parent of the node v.

Page 4: Basic Algorithms on Trees. If the node v is the root, then the depth of the node v is 0. Otherwise, the depth of the node v is one plus the depth of

Example:

r

v

u

T

The depth of the root is 0. The depth of the node u is the depth of the root plus 1. The depth of the node v is the depth of the node u plus 1.

Page 5: Basic Algorithms on Trees. If the node v is the root, then the depth of the node v is 0. Otherwise, the depth of the node v is one plus the depth of

Based on the recursive definition, a recursive algorithm can be developed to find out the depth of a node. depth(T, v) if node v is the root then return 0 else save the parent of the node v to variable u return 1 + depth(T, u)

Page 6: Basic Algorithms on Trees. If the node v is the root, then the depth of the node v is 0. Otherwise, the depth of the node v is one plus the depth of

The following is a simple Java implementation of the algorithm: public static int depth( InspectableTree T, Position v ) { if( T.isRoot( v )) return 0; else return 1 + depth( T, T.parent( v )); }

Page 7: Basic Algorithms on Trees. If the node v is the root, then the depth of the node v is 0. Otherwise, the depth of the node v is one plus the depth of

The recursive definition of the height of a node v: If the node v is an external node, then the height of the

node v is 0. Otherwise, the height of the node v is one plus the

maximum height of the children of the node v. The height of the node v is 1. The height of the node u is 2.

r

v

u

T

Page 8: Basic Algorithms on Trees. If the node v is the root, then the depth of the node v is 0. Otherwise, the depth of the node v is one plus the depth of

The height of a tree T is the height of the root of T.

Example: The height of the tree T is 3, the height of the node r (root).

r

v

u

T

Page 9: Basic Algorithms on Trees. If the node v is the root, then the depth of the node v is 0. Otherwise, the depth of the node v is one plus the depth of

The Algorithm to find the height of a position v: Algorithm height2(T,v) if position v is external return 0 else

initialize the variable h to zero for each child w of v call method height2( T, w ) to calculate

the height of w if the height of w is greater than the value in h replace the value in h by the height of w

return h + 1

Page 10: Basic Algorithms on Trees. If the node v is the root, then the depth of the node v is 0. Otherwise, the depth of the node v is one plus the depth of

A Java implementation of the algorithm: public static int height2( InspectableTree T, Position v ) { if( T.isExternal( v )) return 0; else { int h = 0; PositionIterator children = T.childrens(v); while( children.hasNext())

h = Math.max( h, height2( T, children.nextPosition() ));

return 1 + h; } }

Page 11: Basic Algorithms on Trees. If the node v is the root, then the depth of the node v is 0. Otherwise, the depth of the node v is one plus the depth of

Data Structure Exercises 11.1

Page 12: Basic Algorithms on Trees. If the node v is the root, then the depth of the node v is 0. Otherwise, the depth of the node v is one plus the depth of

Binary Trees

Recall that a binary tree is a tree in which every node has at most two children. Each internal node in a proper binary tree has exactly 2 children. Binary tree has many applications.

Therefore, we dedicate this class to discuss further about binary trees.

B

c

1

T

b

2 3 4

Page 13: Basic Algorithms on Trees. If the node v is the root, then the depth of the node v is 0. Otherwise, the depth of the node v is one plus the depth of

By convention, all binary trees are assumed to be proper. This convention does not cause the loss of generality because we can always add “null nodes” to an improper binary tree to make it proper.

r

v

u

T

r

v

u

T

Null node

Page 14: Basic Algorithms on Trees. If the node v is the root, then the depth of the node v is 0. Otherwise, the depth of the node v is one plus the depth of

The Binary Tree Abstract Data Type

Recall that the two children of an internal node in a binary tree are labelled as a left child and a right child, respectively. Therefore, the binary tree ADT supports three more methods. leftChild(v): Return the left child of v; an error condition occurs if v

is an external node. Input: Position; Output: Position

rightChild(v): Return the right child of v; an error condition occurs if v is an external node. Input: Position; Output: Position

sibling(v): Return the sibling of v; an error condition occurs if v is the root. Input: Position; Output: Position

Page 15: Basic Algorithms on Trees. If the node v is the root, then the depth of the node v is 0. Otherwise, the depth of the node v is one plus the depth of

A Binary Tree Interface in Java

Similar to the interfaces InspectableTree and Tree, we have interfaces InspectableBinaryTree and BinaryTree. public interface InspectableBinaryTree extends InspectableTree { public Position leftChild( Position v ); public Position rightChild( Position v ); public Position sibling( Position v ); } public interface BinaryTree extends InspectableBinaryTree, PositionalContainer { }

Page 16: Basic Algorithms on Trees. If the node v is the root, then the depth of the node v is 0. Otherwise, the depth of the node v is one plus the depth of

InspectablePositionalContainerpositions

InspectableVectorelemAtRank

InspectableListfirstlastbeforeafterpositions

VectorreplaceAtRankinsertAtRankremoveAtRank

InspectableSequenceatRankrankOf

ListinsertFirstinsertLastinsertBeforeinsertAfterremove

Sequence

InspectableContainersizeisEmptyelements

PositionalContainerswapElementsreplaceElement

InspectableTreerootparentchildrenisRootisInternalisExternal

Tree InspectableBinaryTreeleftChildrightChildsibling

BinaryTree

Page 17: Basic Algorithms on Trees. If the node v is the root, then the depth of the node v is 0. Otherwise, the depth of the node v is one plus the depth of

Levels of a Binary Tree

level: All nodes of a tree T at the same depth d. We call it level d of T. How many nodes can be at each level?

Page 18: Basic Algorithms on Trees. If the node v is the root, then the depth of the node v is 0. Otherwise, the depth of the node v is one plus the depth of

0

1

2

3

2

1

4

8

W h a t is th e r e la t io n s h ip b e tw e e n th e le v e l a n d th e m a x im u m n u m b e r o f n o d e s a t th a t le v e l?

Level: number of nodes at a level:

Page 19: Basic Algorithms on Trees. If the node v is the root, then the depth of the node v is 0. Otherwise, the depth of the node v is one plus the depth of

Level Number of nodes Pattern 0 1 1 2 2 4 3 8 … … k … …

02122232

…k2

Page 20: Basic Algorithms on Trees. If the node v is the root, then the depth of the node v is 0. Otherwise, the depth of the node v is one plus the depth of

Traversals of a Binary TreeT r a v e r s a l o f a t r e e T : A s y s t e m a t i c a l w a y o f a c c e s s i n g a l l t h e n o d e s o f T . I n o r d e r t r a v e r s a l : F o r e a c h n o d e v , r e c u r s i v e l y p e r f o r m t h e t r a v e r s a l o f i t s l e f t s u b t r e e , t h e n v i s i t t h e n o d e i t s e l f , a n d f i n a l y r e c u r s i v e l y p e r f o r m t h e t r a v e r s a l o f i t s r i g h t s u b t r e e .

V i s i t a l l t h e n o d e s i n l e f t s u b t r e e f i r s t

V i s i t a l l t h e n o d e s i n r i g h t s u b t r e e l a s t

V i s i t i t s e l f a f t e rv i s i t i n g t h e l e f ts u b t r e e

Page 21: Basic Algorithms on Trees. If the node v is the root, then the depth of the node v is 0. Otherwise, the depth of the node v is one plus the depth of

The action of visiting a node: When we visit each node of a binary tree, what do we do? We can do anything that makes a sense such as incrementing a counter or some complex computation based on the element at each node.

Page 22: Basic Algorithms on Trees. If the node v is the root, then the depth of the node v is 0. Otherwise, the depth of the node v is one plus the depth of

Algorithm inorder(T,v): if v is an internal node assign the left child of the node v to u call method inorder(T, u) perform the “visit” action for node v if v is an internal node assign the right child of the node v to u call method inorder(T, u)

r

v

u

T

Page 23: Basic Algorithms on Trees. If the node v is the root, then the depth of the node v is 0. Otherwise, the depth of the node v is one plus the depth of

Data Structure Exercises 11.2

Page 24: Basic Algorithms on Trees. If the node v is the root, then the depth of the node v is 0. Otherwise, the depth of the node v is one plus the depth of

Algorithm inorder(T,v): if v is an internal node assign the left child of the node v to u call method inorder(T, u) perform the “visit” action for node v if v is an internal node assign the right child of the node v to u call method inorder(T, u)

r

v

u

T

W

X Y

A

B C

Inorder tree traversal• Inorder traversal based on recursion:

Page 25: Basic Algorithms on Trees. If the node v is the root, then the depth of the node v is 0. Otherwise, the depth of the node v is one plus the depth of

inorder(T, r)

if …inorder(T, u)

“visit” r

If …inorder (T, a)

inorder(T, u)

if …inorder(T, w)

“visit” u

If …inorder (T, v)

inorder(T, w)

if …

“visit” w

if …

126

Page 26: Basic Algorithms on Trees. If the node v is the root, then the depth of the node v is 0. Otherwise, the depth of the node v is one plus the depth of

inorder(T, x)

if …

“visit” x

if …

3inorder(T, v)

inorder(T, v)

if …inorder(T, x)

“visit” v

If …inorder (T, y)

4

Page 27: Basic Algorithms on Trees. If the node v is the root, then the depth of the node v is 0. Otherwise, the depth of the node v is one plus the depth of

inorder(T, y)

inorder(T, y)

if …

“visit” y

if …

5

inorder(T, a)

inorder(T, a)

if …inorder(T, b)

“visit” a

If …inorder (T, c)

8

7

9

Page 28: Basic Algorithms on Trees. If the node v is the root, then the depth of the node v is 0. Otherwise, the depth of the node v is one plus the depth of

• Inorder traversal based on Stack data structure

Algorithm Stack-control-inorder(T, v)establish stack S;S.push(v);while (S is not empty) do{u := S.pop(); if u is leaf or u is marked, visit u; else {let v1 and v2 be the left and right child node of

v, respectively; S.push(v2); mark u; S.push(u*); S.push(v1); }

}

Page 29: Basic Algorithms on Trees. If the node v is the root, then the depth of the node v is 0. Otherwise, the depth of the node v is one plus the depth of

ur*ar

wu*vr*a

u*vr*a

vr*a

print(u)

xv*yr*a

v*yr*a

print(v)yr*a

print(y)r*a

print(y)

a

ba*c a*

c

print(b)

c

print(a)

2

4 5 6

7

print(x)3

print(w)1

print(c)8 9