Download presentation
Presentation is loading. Please wait.
Published byYanti Sudjarwadi Modified over 6 years ago
1
Tree Implementations (plus briefing about Iterators)
Chapter 24 Adapted from Pearson Education, Inc.
2
Contents The Nodes in a Binary Tree
An Interface for a Node An Implementation of BinaryNode An Implementation of the ADT Binary Tree Creating a Basic Binary Tree The Method privateSetTree Computing the Height and Counting Nodes Traversals & Iterators An Implementation of an Expression Tree General Trees A Node for a General Tree Using a Binary Tree to Represent a General Tree Adapted from Pearson Education, Inc.
3
Objectives Describe necessary operations on node within binary tree
Implement class of nodes for binary tree Implement 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 Adapted from Pearson Education, Inc.
4
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.
5
An Interface for a Node An implementation of BinaryNode, Listing 24-2
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 the number of nodes in the subtree rooted at this node */ public int getNumberOfNodes (); /** Computes the height of the subtree rooted at this the height of the subtree rooted at this node */ public int getHeight (); public BinaryNodeInterface < T > copy (); } // end BinaryNodeInterface An implementation of BinaryNode, Listing 24-2 Adapted from Pearson Education, Inc.
6
Implementation of a basic Binary Tree
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 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) Adapted from Pearson Education, Inc.
7
Problems with PrivateSetTree
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 So what is the solution? Adapted from Pearson Education, Inc.
8
Recursive In-Order traversal of a BT
Public method for user, calls private method Adapted from Pearson Education, Inc.
9
Iterators vs regular traversal
What if I want to do something at each node? I need to be able to stop at each node I 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-1 Adapted from Pearson Education, Inc.
10
Adapted from Pearson Education, Inc.
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()); nameList nameIterator Maha Nadia Rehab IllegalStateException Print “Nadia” As we go through this example, ask the students to look at the interface for iterators. Q - At the first remove, ask students they can predict what will happen. A - exception Q - Then ask them why it happened A - we did not call next() yet. At the second remove statement, point out that a call to next should give us “Rehab”, we have to make sure in our implementation that that will be the case. Ask the students where they think the arrow will be after removal? Only then click for next slide!! Adapted from Pearson Education, Inc.
11
Adapted from Pearson Education, Inc.
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.
12
Iterative In-Order Traversal of a BT
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 inorderTraverse Adapted from Pearson Education, Inc.
13
In-Order Traversal with an Iterator
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 hasNext Adapted from Pearson Education, Inc.
14
In-Order Traversal with an Iterator
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 InorderIterator Adapted from Pearson Education, Inc.
15
Tracing of inorderTraverse()
Adapted from Pearson Education, Inc.
16
Using a stack to do a pre-order traversal
Adapted from Pearson Education, Inc.
17
Using a stack to do a post-order traversal
Adapted from Pearson Education, Inc.
18
Using a queue to do a level-order traversal
c d X c d e c Adapted from Pearson Education, Inc.
19
Implementation of an Expression Tree
Note interface, Listing 24-5 Derive from BinaryTree, Listing 24-6 Adapted from Pearson Education, Inc.
20
Figure 24-8 A node for a general tree
Adapted from Pearson Education, Inc.
21
Node for a General Tree Interface, Listing 24-7
Adapted from Pearson Education, Inc.
22
Using a Binary Tree to Represent a General Tree
Figure 24-9 (a) A general tree; (b) an equivalent binary tree; Adapted from Pearson Education, Inc.
23
End Chapter 24 Adapted from Pearson Education, Inc.
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.