240 likes | 255 Views
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
E N D
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 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
Nodes in a Binary Tree Fig. 25-1 A node in a binary tree.
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
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
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.
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
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
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
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
Accessor, Mutator Methods • Methods implemented • getRootData() • isEmpty() • clear() • setRootData (Object rootData) • setRootNode (BinaryNode rootNode) • getRootNode()
Computing Height, Counting Nodes • Within BinaryTree • getHeight() • getNumberOfNodes() • Within BinaryNode • getHeight() • getHeight(BinaryNode node) • getNumberOfNodes()
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
Traversals Fig. 25-4 A binary tree.
Traversals Fig. 25-5 Using a stack to perform an inorder traversal of the binary tree in Fig. 25-4.
Traversals Fig. 25-6 Using a stack to traverse the binary tree in Fig. 25-4 in (a) preorder
Traversals Fig. 25-6 Using a stack to traverse the binary tree in Fig. 25-4 in (b) postorder.
Traversals Fig. 25-7 Using a queue to traverse the binary tree in Fig. 25-4 in level order.
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
General Trees Fig. 25-8 A node for a general tree.
Using a Binary Tree to Represent a General Tree Fig. 25-9 (a) A general tree;
Using a Binary Tree to Represent a General Tree Fig. 25-9 (b) an equivalent binary tree;
Using a Binary Tree to Represent a General Tree Fig. 25-9 (c) a more conventional view of the same binary tree.
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