Trees - Ed. 2. and 3.: Chapter 6 - Ed. 4.: Chapter 7.

33
Trees - Ed. 2. and 3.: Chapter 6 - Ed. 4.: Chapter 7
  • date post

    21-Dec-2015
  • Category

    Documents

  • view

    216
  • download

    0

Transcript of Trees - Ed. 2. and 3.: Chapter 6 - Ed. 4.: Chapter 7.

Trees

- Ed. 2. and 3.: Chapter 6- Ed. 4.: Chapter 7

Trees

• What is a tree?- Examples of trees- Tree interface and interface hierarchy

• Algorithms on binary trees- Binary tree interface- Traversal on a binary tree- Binary tree implementation- Traversal on a tree

• Sample case study application

Trees

Class Mammalia

Order Carnivora(carnivores)

Order Chiroptera(bats)

...

Order Proboscidea(elephants)

Family Felidae(cats)

Family Phocidae(seals)

...

Family Ursidae(bears)

SubfamilyAcinonychinae(cheetahs)

Subfamily Felinae(small cats)

SubfamilyPantherinae(leopards, lions,and tigers)

Is it a sequence?

tree: A data structure that organizes data hierarchically. hierarchical: With the exception of the top element, each element in a tree has a parent element and zero or more children elements. root of the tree: The top element

Electronics R’ Us

R&D Sales

Domestic

M anufacturingPurchasing

US

TV CD TunerInternational

S. Am erica Overseas

Example:

A formal definition of trees:

A tree T is a set of nodes storing elements in a parent-child relationship with the following properties:

T has a special node r, called the root of T. Each node v of T different from r has a parent node u.

r

v

u

T

Note: By definition, a tree has at least one element: the root. child: If node u is the parent of node v, then we say that v is a child of u.

v

u

siblings: Two nodes that are children of the same parent

v

u

w

Nodes w and v are siblings.

external node (leaf): The node has no children.

r

v

u

T

Node v is an external node. internal node: The node has one or more children. Node u is an internal node.

ancestor: Either the node itself or an ancestor of the parent of the node.

r

v

u

T

Node u and r are ancestors of node v.

descendent: A note v is a descendent of a node u if u is an ancestor of v.

r

v

u

T

Node u and v are descendent of node r.

subtree rooted at a node v: The tree consisting of all the descendents of v (including v itself).

r

v

u

T

v

u

A subtree rooted at node u

Example 6.1 The inheritance relation between classes in a Java program.

o class java.lang.Object

o class java.lang.Boolean o class java.lang.Character o class java.lang.Math o class java.lang.Number

o class java.lang.Byte o class java.lang.Double o class java.lang.Float o class java.lang.Integer o class java.lang.Long o class java.lang.Short

o class java.lang.Process o class java.lang.Runtime o class java.lang.SecurityManager o class java.lang.String o class java.lang.System o class java.lang.Thread

Example 6.2

In operating systems, files are organized hierarchically into directories (folders).

o r d e r e d t r e e : T h e r e i s a l i n e a r o r d e r i n g d e f i n e d f o r t h e c h i l d r e n o f e a c h n o d e ; t h a t i s , w e c a n i d e n t i f y c h i l d r e n o f a n o d e a s t h e f i r s t , s e c o n d , t h i r d , a n d s o o n . E x a m p l e 6 . 3 : T h e s t r u c t u r e o f a b o o k i s a n o r d e r e d t r e e

B o o k

C h . 1 C h . 2 C h . 9 R e f e r e n c e s

1 . 1 1 . 4 9 . 1 9 . 6. . . . . .

Comparing Example 6.3 with Example 6.2, we can see animportant difference between the ordered tree and the unorderedtree.

In an ordered tree, the order of the children of a node is significantwhile in an unordered tree, the order of the children of a node isnot important.

binary tree: A tree in which every node has at most two children. Example:

B

c

1

T

b

2 3

p r o p e r b i n a r y t r e e : E a c h n o d e h a s e i t h e r z e r o o r t w o c h i l d r e n . E v e r y i n t e r n a l n o d e h a s e x a c t l y t w o c h i l d r e n . T h e s e c h i l d r e n a r e l a b e l l e d a s a l e f t c h i l d a n d a r i g h t c h i l d , r e s p e c t i v e l y .

B

c

1

T

b

2 3 4

1

b

2 R i g h t c h i l dL e f t c h i l d

A p r o p e r b i n a r y t r e e . A l s o n o d e b ’ s l e f t c h i l d a n d r i g h t c h i l d .

A p r o p e r b i n a r y t r e e a n d t h e l e f t s u b t r e e a n d r i g h t s u b t r e e o f n o d e B .

B

c

1

T

b

2 3 4

c

1

b

2 3 4L e f t s u b t r e e R i g h t s u b t r e e

F r o m n o w o n , a l l t h e b in a r y t r e e s in th is c o u r s e (a n d in t h e t e x t b o o k ) a r e p r o p e r b in a r y t r e e s b y d e f a u lt . E x a m p le 6 .4 : a d e c is io n t r e e

A re yo u n e rv o u s?

W il l y o u n e e d t o a c c e ss m o s t o f t h em o n ey w i th i n th e n e x t 5 y e a r s?

A re yo u w il li n g t o a c c e p t r is k s i ne x c h a n g e fo r h i g h er e x p e c te d r e tu rn s

D i v e r s if ie d p o r tfo li o w it h s t o c k s ,b o n d s , a n d s h o r t -t e rm in v e s tm e n t s

S a v in g a c c o u n t

M o n e y m ark e t fu n d

S to c k p o r t fo li o

Y e sN o

Y e s

Y e s

N o

N o

Example 6.5: M ath with binary operators

+

-

*

/

+

* 6+

2

-

33 -

3 1 9 5 7 4

(((( 3 + 1 ) * 3 ) / (( 9 - 5 ) + 2 )) - (( 3 * ( 7 - 4 )) + 6 )) Each node has a value associated with it.

If a node is external, then its value is that of its variable or constant. If a node is internal, then its value is defined by applying its operation to the

values of its children.

Recall that in a list, a position is located by its two neighbours: the previous and next positions. Similarly, a position or node in a tree is located by its neighbours: Its parent and its children, if any. The position interface for a tree supports the method: element(): Return the object at this position.

Input: None; Output: Object

The Tree Abstract Data Type

The tree abstract data type supports the following fundamental accessor methods:

root(): Return the root of the tree. Input: None; Output: Position

parent(v): Return the parent of node v; an error if v is root. Input: Position; Output: Position

children(v): Return an iterator of the children of node v. Input: Position; Output: Iterator of positions.

The tree ADT also supports the following query methods: isInternal(v):

Test whether node v is internal. Input: Position; Output: Boolean

isExternal(v): Test whether node v is external. Input: Position; Output: Boolean

isRoot(v): Test whether node v is the root. Input: Position; Output: Boolean

Other methods are:

size():

Return the number of nodes in the tree. Input: None; Output: Integer

elements(): Return an iterator of all the elements stored at nodes of the tree. Input: None; Output: Iterator of elementss

positions(): Return an iterator of all the nodes of the tree. Input: Position; Output: Iterator of positions

swapElements(v, w): Swap the elements stored at the nodes v and w. Input: Two positions; Output: None

replaceElement(v,e): Replace with e and return the element stored at node v. Input: A position and an object; Output: Object

public interface Tree { public int size(); public Boolean isEmpty(); public ElementIterator elements(); public PositionIterator positions(); public void swapElements( Position v, Position w ); public Object replaceElement( Position v, Object e ); public Position root(); public Position parent( Position v ); public PositionIterator children( Position v ); public boolean isInternal( Position v ); public boolean isExternal( Position v ); public boolean isRoot( Position v );}

A Tree Interface in Java

Construction of Interface Hierarchy

public interface InspectableContainer { public int size(); public Boolean isEmpty(); public ElementIterator elements(); } public interface InspectablePositionalContainer extends InspectableContainer { public PositionIterator positions(); }

public interface PositionalContainer extends InspectablePositionalContainer { public void swapElements( Position v, Position w ); public Object replaceElement( Position v, Object e ); }

Since this interface inherites InspectablePositionalContainer, the other available methods are: size isEmpty elements positions

public interface InspectableTree extends InspectablePositionalContainer { public Position root(); public Position parent( Position v ); public PositionIterator children( Position v ); public boolean isInternal( Position v ); public boolean isExternal( Position v ); public boolean isRoot( Position v ); }

Since this interface extends InspectablePositionalContainer, the other available methods are size isEmpty elements positions

public interface Tree extends InspectableTree, PositionalContainer {

}

Since this interface inherites both InspectableTree and PositionalContainer, the available methods are: size root isEmpty parent elements children positions isInternal swapElements isExternal replaceElement isRoot

IspectableContainersizeisEmptyElements

IspectablePositionContainerpositions

PositionContainerswapElementreplaceElement

InspectableTreerootparentchildrenisRootisInternalisExternal

Tree

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

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 { }

InspectablePositionalContainerpositions

InspectableVectorelemAtRank

InspectableListfirstlastbeforeafterpositions

VectorreplaceAtRankinsertAtRankremoveAtRank

InspectableSequenceatRankrankOf

ListinsertFirstinsertLastinsertBeforeinsertAfterremove

Sequence

InspectableContainersizeisEmptyelements

PositionalContainerswapElementsreplaceElement

InspectableTreerootparentchildrenisRootisInternalisExternal

Tree InspectableBinaryTreeleftChildrightChildsibling

BinaryTree