1 / 24

Tree Implementations

Tree Implementations. Chapter 25. 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 Accessor and Mutator Methods. Computing the Height and Counting Nodes

Download Presentation

Tree Implementations

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Tree Implementations Chapter 25

  2. 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 Accessor and Mutator Methods Computing the Height and Counting Nodes Traversals An Implementation of an Expression Tree General Trees A Node for a General Tree Using a Binary Tree for Represent a General Tree Chapter Contents

  3. Nodes in a Binary Tree Fig. 25-1 A node in a binary tree.

  4. Interface for a Node • Interface for class of nodes suitable for a binary tree public interface BinaryNodeInterface{ public Object getData();public void setData(Object newData);public BinaryNodeInterface getLeftChild();public BinaryNodeInterface getRightChild();public void setLeftChild(BinaryNodeInterface leftChild);public void setRightChild(BinaryNodeInterface rightChild);public boolean hasLeftChild();public boolean hasRightChild();public boolean isLeaf();} // end BinaryNodeInterface

  5. Implementation of BinaryNode • Usually the class that represents a node in a tree is a detail hidden from the client • It is placed within a package • Makes it available • Available to all classes involved in implementation of a tree

  6. An Implementation of the ADT Binary Tree • Recall interface for class of binary trees from previous chapter • In that interface • TreeInterface specifies basic operations common to all trees • TreeInteratorInterface specifies operations for tree traversal • Note full implementation of BinaryTree, section 25.4 in text.

  7. The Method privateSetTree • Problem: previous implementation of privateSetTree not sufficient • Given: treeA.setTree (a, treeB, treeC); • Now treeA shares nodes with treeB, treeC • Changes to treeB also affect treeA (Fig. 25-2) fig 25-2 here

  8. The Method privateSetTree • Possible solution • Have privatSetTree copy the nodes in TreeB and TreeC • Now treeA is separate and distinct from treeB and treeC • Changes to either treeB or treeC do NOT affect treeA • Note that copying nodes is expensive • Other solutions are considered

  9. The Method privateSetTree • Another possible solution fortreeA.setTree( a, treeB, treeC); • Have privateSetTree first link the root node of treeA to root nodes of treeB, treeC (Fig. 25-3) • Then set treeB.root, treeC.root to null This still has some problems fig 25-3 here

  10. The Method privateSetTree • Real solution should do the following: • Create root node r for containing the data • If left subtree exists, not empty • Attach root node to r as left child • If right subtree exists, not empty, distinct from left subtree • Attach root node r as right child • If right, left subtrees are the same, attach copy of right subtree to r instead • If left (right) subtree exists, different from the invoking tree object • Set its data field root to null

  11. Accessor, Mutator Methods • Methods implemented • getRootData() • isEmpty() • clear() • setRootData (Object rootData) • setRootNode (BinaryNode rootNode) • getRootNode()

  12. Computing Height, Counting Nodes • Within BinaryTree • getHeight() • getNumberOfNodes() • Within BinaryNode • getHeight() • getHeight(BinaryNode node) • getNumberOfNodes()

  13. Traversals • Make recursive method private • Call from public method with no parameters public void inorderTraverse(){ inorderTraverse(root); } private void inorderTraverse(BinaryNode node){ if (node != null) { inorderTraverse((BinaryNode)node.getLeftChild()); System.out.println(node.getData()); inorderTraverse((BinaryNode)node.getRightChild()); } // end if} // end inorderTraverse

  14. Traversals Fig. 25-4 A binary tree.

  15. Traversals Fig. 25-5 Using a stack to perform an inorder traversal of the binary tree in Fig. 25-4.

  16. Traversals Fig. 25-6 Using a stack to traverse the binary tree in Fig. 25-4 in (a) preorder

  17. Traversals Fig. 25-6 Using a stack to traverse the binary tree in Fig. 25-4 in (b) postorder.

  18. Traversals Fig. 25-7 Using a queue to traverse the binary tree in Fig. 25-4 in level order.

  19. Implementation of an Expression Tree • Defining an interface for an expression tree • Extend the interface for a binary tree • Add a declaration for the method evaluate public interface ExpressionTreeInterface extends BinaryTreeInterface{ /** Task: Computes the value of the expression in the tree. * @return the value of the expression */public double evaluate();} // end ExpressionTreeInterface

  20. General Trees Fig. 25-8 A node for a general tree.

  21. Using a Binary Tree to Represent a General Tree Fig. 25-9 (a) A general tree;

  22. Using a Binary Tree to Represent a General Tree Fig. 25-9 (b) an equivalent binary tree;

  23. Using a Binary Tree to Represent a General Tree Fig. 25-9 (c) a more conventional view of the same binary tree.

  24. Traversals • Traversals of general tree in 25-9a • Preorder: A B E F C G H I D J • Postorder: E F B G H I C J D A • Level order: A B C D E F G H I J • Traversals of binary tree in 25-9c • Preorder: A B E F C G H I D J • Postorder: F E I H G J D C B A • Level order: A B E C F G D H J I • Inorder: E F B G H I C J D A

More Related