Presentation is loading. Please wait.

Presentation is loading. Please wait.

Tree Implementations Chapter 25. 2 Chapter Contents The Nodes in a Binary Tree An Interface for a Node An implementation of BinaryNode An Implementation.

Similar presentations


Presentation on theme: "Tree Implementations Chapter 25. 2 Chapter Contents The Nodes in a Binary Tree An Interface for a Node An implementation of BinaryNode An Implementation."— Presentation transcript:

1 Tree Implementations Chapter 25

2 2 Chapter 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 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

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

4 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 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 6 An Implementation of the ADT Binary Tree Recall interface for class of binary trees from previous chapterinterface 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 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 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 9 The Method privateSetTree Another possible solution for treeA.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 fig 25-3 here This still has some problems

10 10 The Method privateSetTree Real solution should do the following: 1. Create root node r for containing the data 2. If left subtree exists, not empty Attach root node to r as left child 3. 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 4. If left (right) subtree exists, different from the invoking tree object Set its data field root to null

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

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

13 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 14 Traversals Fig. 25-4 A binary tree.

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

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

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

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

19 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 20 General Trees Fig. 25-8 A node for a general tree.

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

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

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

24 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


Download ppt "Tree Implementations Chapter 25. 2 Chapter Contents The Nodes in a Binary Tree An Interface for a Node An implementation of BinaryNode An Implementation."

Similar presentations


Ads by Google