5 package TreePackage interface BinaryNodeInterface lt T gt public T getData public void setData T newData public BinaryNodeInterface lt T gt ID: 690280
Download Presentation The PPT/PDF document "An Interface for a Node Adapted from Pea..." is the property of its rightful owner. Permission is granted to download and print the materials on this web site for personal, non-commercial use only, and to display it on your personal computer provided you do not modify the materials and that you retain all copyright notices contained in the materials. By downloading content from our website, you accept the terms of this agreement.
Slide1
Tree Implementations(plus briefing about Iterators)
Chapter 24
Adapted from Pearson Education, Inc.
1Slide2
ContentsAdapted from Pearson Education, Inc.
The Nodes in a Binary Tree
An Interface for a NodeAn Implementation of
BinaryNode
An Implementation of the ADT Binary Tree
Creating a Basic Binary Tree
The Method privateSetTreeComputing the Height and Counting NodesTraversals & IteratorsAn Implementation of an Expression TreeGeneral TreesA Node for a General TreeUsing a Binary Tree to Represent a General Tree
2Slide3
ObjectivesAdapted from Pearson Education, Inc.
Describe necessary operations on node within binary tree
Implement class of nodes for binary treeImplement class of binary trees
Implement an expression tree by extending class of binary trees
Describe necessary operations on a node within general tree
Use binary tree to represent general tree
3Slide4
Interfaces…(review from ch
23)
package
TreePackage
;
public interface
TreeInterface < T >{public T getRootData ();public int getHeight ();public
int
getNumberOfNodes
();
public
boolean
isEmpty ();public void clear ();} // end TreeInterface
package TreePackage;import java.util.Iterator;public interface TreeIteratorInterface < T >{public Iterator < T > getPreorderIterator ();public Iterator < T > getPostorderIterator ();public Iterator < T > getInorderIterator ();public Iterator < T > getLevelOrderIterator ();} // end TreeIteratorInterface
package TreePackage;public interface BinaryTreeInterface < T > extends TreeInterface < T > ,TreeIteratorInterface < T >{public void setTree (T rootData);public void setTree (T rootData, BinaryTreeInterface < T > leftTree,BinaryTreeInterface < T > rightTree);} // end BinaryTreeInterface
Adapted from Pearson Education, Inc.
4Slide5
An Interface for a Node
Adapted from Pearson Education, Inc.
5
package
TreePackage
;
interface
BinaryNodeInterface
< T >
{
public
T
getData
();
public void setData (T newData
); public BinaryNodeInterface < T > getLeftChild (); public BinaryNodeInterface < T > getRightChild ();
public
void
setLeftChild
(
BinaryNodeInterface
< T > leftChild); public void setRightChild (BinaryNodeInterface < T > rightChild); public boolean hasLeftChild (); public boolean hasRightChild (); public boolean isLeaf (); /** Counts the nodes in the subtree rooted at this node. @return the number of nodes in the subtree rooted at this node */ public int getNumberOfNodes (); /** Computes the height of the subtree rooted at this node. @return the height of the subtree rooted at this node */ public int getHeight (); public BinaryNodeInterface < T > copy (); } // end BinaryNodeInterface
An implementation of
BinaryNode
,
Listing 24-2Slide6
First draft of the class,
Listing 24-3
Notice private method
privateSetTree
:
private
void
privateSetTree
(T
rootData
,
BinaryTree
< T >
leftTree, BinaryTree < T > rightTree)
{ /* FIRST DRAFT - See Segments 24.5 - 24.8 for improvements. */ root = new BinaryNode < T > (rootData) ;
if
(
leftTree
!=
null) root.setLeftChild (leftTree.root); if (rightTree != null) root.setRightChild (rightTree.root); } // end privateSetTree Result of calling:treeA.setTree(a, treeB, treeC) Implementation of a basic Binary TreeAdapted from Pearson Education, Inc.6Slide7
Problems with
PrivateSetTree
Adapted from Pearson Education, Inc.
Result of calling:
treeA.setTree
(a,
treeB
,
treeB
)
Can be solved as follows:
private void
privateSetTree
(T
rootData
, BinaryTree < T > leftTree, BinaryTree
< T > rightTree) { root = new BinaryNode < T > (rootData); if ((leftTree != null) && !leftTree.isEmpty ())
root.setLeftChild
(
leftTree.root.copy
());
if ((rightTree != null) && !rightTree.isEmpty ()) root.setRightChild (rightTree.root.copy ()); } // end privateSetTree But copy is expensive:public BinaryNodeInterface < T > copy () { BinaryNode < T > newRoot = new BinaryNode < T > (data); if (left != null) newRoot.left = (BinaryNode<T>) left.copy (); if (right!= null) newRoot.right =(BinaryNode<T>) right.copy (); return newRoot; } // end copy 7So what is the solution?Slide8
Recursive In-Order traversal of a BT
Adapted from Pearson Education, Inc.
Inorder traversalPublic method for user, calls private method
8Slide9
Iterators vs regular traversal
Adapted from Pearson Education, Inc.
What if I want to do something at each node?I need to be able to stop at each nodeI need to be able to control when to go to the next node
Iterators have these methods: (see
ch.
15)
public boolean hasNext ();
public T
next
();
public void
remove
(); // Optional method
Iterators allow you to step through a structure, one step at a time.
See complete interface Iterator, Listing 15-19Slide10
Using iterators (ch
15)
ListWithIteratorInterface
<String>
nameList
= new
ArrayListWithIterator <String>();
nameList.add
(“
Maha
”);
nameList.add
(“Nadia”);
nameList.add
(“Rehab”);
Iterator < String > nameIterator = nameList.getIterator();
nameIterator.remove();if(nameIterator.hasNext()) nameIterator.next();System.out.println (nameIterator.next
());
nameIterator.remove
();
nameList
nameIterator
MahaNadiaRehab
IllegalStateException
Adapted from Pearson Education, Inc.
10
Print “Nadia”Slide11
Using iterators (ch 15)
ListWithIteratorInterface
<String>
nameList
= new
LinkedListWithIterator
<String>();
nameList.add
(“
Maha
”);
nameList.add
(“Nadia”);
nameList.add
(“Rehab”);
Iterator < String > nameIterator = nameList.getIterator();
if(nameIterator.hasNext()) nameIterator.next();System.out.println (nameIterator.next());
nameIterator.remove();
nameList
nameIterator
Maha
Rehab
Adapted from Pearson Education, Inc.11Slide12
Iterative In-Order Traversal of a BT
Adapted from Pearson Education, Inc.
public void
inorderTraverse
()
{
StackInterface
<
BinaryNodeInterface
<T>>
nodeStack
=
new
LinkedStack
<BinaryNodeInterface<T>> ();
BinaryNodeInterface < T > currentNode = root; while
(!
nodeStack.isEmpty
() || (
currentNode
!= null))
{ // find leftmost node with no left child while (currentNode != null) { nodeStack.push (currentNode); currentNode = currentNode.getLeftChild (); } // end while // visit leftmost node, then traverse its right subtree if (!nodeStack.isEmpty ()) { BinaryNodeInterface < T > nextNode = nodeStack.pop (); System.out.println (nextNode.getData ());
currentNode = nextNode.getRightChild ();
}
// end if
}
// end while
}
// end
inorderTraverse
12Slide13
In-Order Traversal with an Iterator
Adapted from Pearson Education, Inc.
private class
InorderIterator
implements Iterator < T >
{
private
StackInterface
<
BinaryNodeInterface
< T >>
nodeStack
; private
BinaryNodeInterface
< T >
currentNode; public InorderIterator
() { nodeStack = new LinkedStack < BinaryNodeInterface < T >> ();
currentNode
= root;
} // end default constructor public boolean hasNext () { return ( !nodeStack.isEmpty () || (currentNode != null) ); } // end hasNext13Slide14
In-Order Traversal with an Iterator
Adapted from Pearson Education, Inc.
public T
next
()
{
BinaryNodeInterface
< T >
nextNode
= null;
//
find leftmost node with no left child
while (currentNode != null)
{ nodeStack.push (currentNode); currentNode = currentNode.getLeftChild ();
}
// end while
//
get leftmost node, then move to its right subtree if (!nodeStack.isEmpty ()) { nextNode = nodeStack.pop (); currentNode = nextNode.getRightChild (); } else throw new NoSuchElementException (); return nextNode.getData (); } // end next public void remove () { throw new UnsupportedOperationException (); } // end remove } // end InorderIterator14Slide15
Tracing of inorderTraverse
()
Adapted from Pearson Education, Inc.
15Slide16
Using a stack to do a pre-order traversal
Adapted from Pearson Education, Inc.
16Slide17
Using a stack to do a post-order traversal
Adapted from Pearson Education, Inc.
17Slide18
Using a queue to do a level-order
traversal
Adapted from Pearson Education, Inc.
c d
X
c
d e
X
c
18Slide19
Implementation of an Expression Tree
Adapted from Pearson Education, Inc.
Note interface, Listing 24-5
Derive from
BinaryTree
,
Listing 24-6
19Slide20
Node for a General TreeAdapted from Pearson Education, Inc.
Figure 24-8 A node for a general tree
20Slide21
Node for a General TreeAdapted from Pearson Education, Inc.
Interface,
Listing 24-7
21Slide22
Using a Binary Tree to Represent a General Tree
Adapted from Pearson Education, Inc.
Figure 24-9 (a) A general tree; (b) an equivalent binary tree;
22Slide23
EndChapter 24
Adapted from Pearson Education, Inc.
23