Presentation is loading. Please wait.

Presentation is loading. Please wait.

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.

Similar presentations


Presentation on theme: "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."— Presentation transcript:

1 Trees CS 105

2 10/02/05 L7: Trees Slide 2 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved 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

3 10/02/05 L7: Trees Slide 3 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved Visualizing a Tree N AP T M I O R G Root Child

4 10/02/05 L7: Trees Slide 4 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved 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 (?)

5 10/02/05 L7: Trees Slide 5 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved 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

6 10/02/05 L7: Trees Slide 6 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved 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

7 10/02/05 L7: Trees Slide 7 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved 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

8 10/02/05 L7: Trees Slide 8 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved 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

9 10/02/05 L7: Trees Slide 9 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved 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 …

10 10/02/05 L7: Trees Slide 10 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved 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

11 10/02/05 L7: Trees Slide 11 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved 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

12 10/02/05 L7: Trees Slide 12 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved 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

13 10/02/05 L7: Trees Slide 13 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved 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

14 10/02/05 L7: Trees Slide 14 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved 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

15 10/02/05 L7: Trees Slide 15 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved 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

16 10/02/05 L7: Trees Slide 16 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved 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

17 10/02/05 L7: Trees Slide 17 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved Preorder Traversal Algorithm preorder( T, v ) Input: Tree T, Node v Output: 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 )

18 10/02/05 L7: Trees Slide 18 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved Postorder Traversal Algorithm postorder( T, v ) Input: Tree T, Node v Output: 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)

19 10/02/05 L7: Trees Slide 19 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved Traversals Example Paper Chap 2.3 Chap 1 Chap 2.2 Chap 1.2Chap 2.1Chap 1.1 Chap 2 TitleAbstract References Chap 3.2Chap 3.1 Chap 3

20 10/02/05 L7: Trees Slide 20 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved Preorder Traversal Paper Chap 2.3 Chap 1 Chap 2.2 Chap 1.2Chap 2.1Chap 1.1 Chap 2 TitleAbstract 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() )

21 10/02/05 L7: Trees Slide 21 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved Paper Chap 2.3 Chap 1 Chap 2.2 Chap 1.2Chap 2.1Chap 1.1 Chap 2 TitleAbstract References Chap 3.2Chap 3.1 Chap 3 Postorder Traversal Title, Abstract, Chap 1.1, Chap 1.2, Chap 1, Chap 2.1, …, Chap 3, References, Paper postorder( t, t.root() )

22 10/02/05 L7: Trees Slide 22 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved Binary Tree Preorder Traversal Algorithm preorder( T, v ) Input: Binary Tree T, Node v Output: 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 ) )

23 10/02/05 L7: Trees Slide 23 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved Binary Tree Postorder Traversal Algorithm postorder( T, v ) Input: Binary Tree T, Node v Output: Depends on application if T.hasLeft( v ) then postorder ( T, T.left( v ) ) if T.hasRight( v ) then postorder ( T, T.right( v ) ) perform the “visit” action for node v // example: print T.getElement(v)

24 10/02/05 L7: Trees Slide 24 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved Binary Tree Inorder Traversal Algorithm inorder( T, v ) Input: Binary Tree T, Node v Output: Depends on application if T.hasLeft( v ) then inorder ( 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 ) )

25 10/02/05 L7: Trees Slide 25 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved Inorder Traversal Example - 3x / 8 13 ( (3 X 1) / 3 ) - 8 inorder( t, t.root() )

26 10/02/05 L7: Trees Slide 26 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved 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

27 10/02/05 L7: Trees Slide 27 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved Tree positions Consider the signature of the insertLeft method for a binary tree For ArrayBinaryTree public int insertLeft( int v, Object o ) For LinkedBinaryTree public BTNode insertLeft( BTNode v, Object o ) If we had a BinaryTree interface, how do we declare this method? public ??? insertLeft( ??? v, Object o );

28 10/02/05 L7: Trees Slide 28 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved The Position interface Position IndexBTNode 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; } }

29 10/02/05 L7: Trees Slide 29 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved The BinaryTree interface BinaryTree ArrayBinaryTreeLinkedBinaryTree 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 ); … }

30 10/02/05 L7: Trees Slide 30 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved 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 should be Position, not Index

31 10/02/05 L7: Trees Slide 31 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved 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


Download ppt "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."

Similar presentations


Ads by Google