31
Trees CS 105

Trees CS 105. 10/02/05 L7: Trees Slide 2 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved Definition

Embed Size (px)

Citation preview

Trees

CS 105

L7: TreesSlide 2

Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved 10/02/05

Definition

The Tree Data Structure stores objects (nodes) hierarchically nodes have parent-child relationships operations include accessing the parent

or children of a given node A tree T is a set of nodes such that

there is a distinguished node r (called the root) of T that has no parent

each node v of T except r has a parent node

L7: TreesSlide 3

Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved 10/02/05

Visualizing a Tree

N

AP

T

MI

O

R

G

RootChild

L7: TreesSlide 4

Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved 10/02/05

Sample Uses

A company’s organizational structure Family tree The Java class hierarchy O/S directory structure Book (parts, chapters, sections) Arithmetic expressions Web page links (?)

L7: TreesSlide 5

Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved 10/02/05

Tree Terminology

Root the only node that has no parent

Leaf (External node) node that has no children

Internal node node that has at least one child

Siblings nodes that have a common parent

L7: TreesSlide 6

Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved 10/02/05

More Tree Terminology

Ancestor recursive definition: ancestors of a node v

are v itself and the ancestors of its parent proper ancestors: ancestors excluding itself

Descendant v is a descendant of u if u is an ancestor of v

Subtree of T rooted at v set of all descendants of v

L7: TreesSlide 7

Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved 10/02/05

Even More Terminology

Ordered Tree children of a node have a strict linear order

Binary Tree an ordered tree where nodes have at most

two children (left and right) Depth and Height of a node

depth: distance from node to root height: from node to its farthest

descendant

L7: TreesSlide 8

Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved 10/02/05

Tree Implementations

Array-based implementation elements (or references to them) are

stored in an array parent-child relationships derived from

indices Linked implementation

elements stored in nodes nodes contain pointers to parent and

children

L7: TreesSlide 9

Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved 10/02/05

Tree Operations

Get the root node of the tree Go to parent or children from a given node Add a root to an empty tree Add a child to a node Remove a node (can impose that the node

be a leaf, for simplicity) Get the element associated to a node Replace the element associated to a node Others …

L7: TreesSlide 10

Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved 10/02/05

Binary Tree operations

For a binary tree, a node has at most two children and are ordered

Could distinguish between the left and right child of a node

Implementations are simpler for binary trees but many concepts apply to general trees

L7: TreesSlide 11

Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved 10/02/05

Binary Tree methods

size(): returns number of nodes in tree isEmpty(): check if tree is empty isInternal(v): check if node v is an internal

node isExternal(v): check if node v is a leaf isRoot(v): check if node v is the root hasLeft(v): check if node v has a left child hasRight(v): check if node v has a right child

L7: TreesSlide 12

Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved 10/02/05

Binary Tree methods

root(): returns the root node parent(v): return the parent of node v left(v): return the left child of node v right(v): return the right child of node

v sibling(v): return the only sibling of v getElement(v): return the element

associated to node v

L7: TreesSlide 13

Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved 10/02/05

Binary Tree methods

replace(v,e): set element value of node v to e

addRoot(e): add a root node to an empty tree containing element e

insertLeft(v,e): add left child containing element e to node v

insertRight(v,e): add right child containing element e to node v

remove(v): remove node v, return current element in that node

L7: TreesSlide 14

Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved 10/02/05

Implementation considerations

Distinguish nodes (or positions) from the elements they contain For array implementation, nodes are referred to

by integers For linked implementation, there are explicit

node objects Exceptions

Non-existent node for check, traversal, update, and removal operations

Previously existing node for insertion operation

L7: TreesSlide 15

Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved 10/02/05

Array limitations

Capacity restrictions Disproportional space required when tree is

skewed Consider a tree where most of its nodes have right

children and no left children Not really easier to implement Choose array implementation only when tree

is complete or nearly complete (all levels are filled up or almost filled up with nodes)

Technique not as practical for non-binary trees

L7: TreesSlide 16

Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved 10/02/05

Traversals

Traversal systematic way of accessing or visiting the

nodes of a tree Preorder

visit root first, then traverse its subtrees (recursive)

Postorder traverse subtrees first (recursive), then root

Inorder (for binary trees) visit left subtree, then root, then right subtree

L7: TreesSlide 17

Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved 10/02/05

Preorder Traversal

Algorithm preorder( T, v )Input: Tree T, Node vOutput: Depends on application

perform the “visit” action for node v// example: print T.getElement(v)for each child w of v do preorder( T, w )

L7: TreesSlide 18

Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved 10/02/05

Postorder Traversal

Algorithm postorder( T, v )Input: Tree T, Node vOutput: Depends on application

for each child w of v do postorder( T, w)perform the “visit” action for node v// example: print T.getElement(v)

L7: TreesSlide 19

Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved 10/02/05

Traversals Example

Paper

Chap 2.3

Chap 1

Chap 2.2Chap 1.2 Chap 2.1Chap 1.1

Chap 2Title Abstract References

Chap 3.2Chap 3.1

Chap 3

L7: TreesSlide 20

Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved 10/02/05

Preorder Traversal

Paper

Chap 2.3

Chap 1

Chap 2.2Chap 1.2 Chap 2.1Chap 1.1

Chap 2Title Abstract References

Chap 3.2Chap 3.1

Chap 3

Paper, Title, Abstract, Chap 1, Chap 1.1, Chap 1.2, Chap 2, …, Chap 3.2, References

preorder( t, t.root() )

L7: TreesSlide 21

Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved 10/02/05

Paper

Chap 2.3

Chap 1

Chap 2.2Chap 1.2 Chap 2.1Chap 1.1

Chap 2Title Abstract References

Chap 3.2Chap 3.1

Chap 3

Postorder TraversalTitle, Abstract, Chap 1.1, Chap 1.2, Chap 1, Chap 2.1, …, Chap 3, References, Paper

postorder( t, t.root() )

L7: TreesSlide 22

Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved 10/02/05

Binary Tree Preorder Traversal

Algorithm preorder( T, v )Input: Binary Tree T, Node vOutput: Depends on application

perform the “visit” action for node v// example: print T.getElement( v )if T.hasLeft( v ) then

preorder ( T, T.left( v ) )if T.hasRight( v ) then

preorder ( T, T.right( v ) )

L7: TreesSlide 23

Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved 10/02/05

Binary Tree Postorder Traversal

Algorithm postorder( T, v )Input: Binary Tree T, Node vOutput: Depends on application

if T.hasLeft( v ) thenpostorder ( T, T.left( v ) )

if T.hasRight( v ) thenpostorder ( T, T.right( v ) )

perform the “visit” action for node v// example: print T.getElement(v)

L7: TreesSlide 24

Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved 10/02/05

Binary Tree Inorder Traversal

Algorithm inorder( T, v )Input: Binary Tree T, Node vOutput: Depends on application

if T.hasLeft( v ) theninorder ( T, T.left( v ) )

perform the “visit” action for node v// example: print T.getElement(v)if T.hasRight( v ) then

inorder ( T, T.right( v ) )

L7: TreesSlide 25

Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved 10/02/05

Inorder Traversal Example

--

33xx

// 88

1133( (3 X 1) / 3 ) - 8

inorder( t, t.root() )

L7: TreesSlide 26

Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved 10/02/05

Programming considerations

When programming stacks and queues in Java, we started with an interface as a way to standardize the different implementations

Array and linked implementations of trees pose an interesting challenge For trees, the concept of a node or position

needs to be exposed Arrays use ints to denote positions, the linked

implementation uses BTNode objects We need a way to standardize positions so that

ints and BTNodes are both applicable

L7: TreesSlide 27

Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved 10/02/05

Tree positions

Consider the signature of the insertLeft method for a binary tree

For ArrayBinaryTreepublic int insertLeft( int v, Object o )

For LinkedBinaryTreepublic BTNode insertLeft( BTNode v, Object o )

If we had a BinaryTree interface, how do we declare this method?

public ??? insertLeft( ??? v, Object o );

L7: TreesSlide 28

Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved 10/02/05

The Position interface

Position

Index BTNode

public class BTNode implements Position{ // define as before}

public interface Position{ // empty interface // or have a getElement() method}

public class Index implements Position{ private int indexValue; public Index( int val ) { indexValue = val; } public int getIndex() { return indexValue; }}

L7: TreesSlide 29

Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved 10/02/05

The BinaryTree interfaceBinaryTree

ArrayBinaryTree LinkedBinaryTree

public interface BinaryTree{

public int size();public boolean isEmpty();public boolean isInternal( Position p );…public Position root();public Position left( Position p );…public Object getElement( Position p );…public Position insertLeft( Position p, Object e );…

}

L7: TreesSlide 30

Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved 10/02/05

Impact on implementing classes

BTNode and LinkedBinaryTree require little revision

For ArrayBinaryTree, there is a need to revise the methods so that positions are Index objects instead of ints

Example:

public Index left( Index v ){ int num = v.getIndex(); checkOccupiedPosition( num ); checkPosition( 2*num +

1 ); return new Index( 2*num + 1 );}

actually, these types shouldbe Position, not Index

L7: TreesSlide 31

Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved 10/02/05

About trees in general

Concepts discussed so far also apply to general (non-binary) trees

Array implementation possible but less practical Need to impose a maximum on number of

children so that arithmetic on ints still computes positions of parent and children

Linked implementation In a TNode class, there is an array or list

of TNodes to represent children