Presentation is loading. Please wait.

Presentation is loading. Please wait.

Tree Implementations Chapter 16. 2 Nodes in a Binary Tree The most common way uses a linked structure with each node represent each element in a binary.

Similar presentations


Presentation on theme: "Tree Implementations Chapter 16. 2 Nodes in a Binary Tree The most common way uses a linked structure with each node represent each element in a binary."— Presentation transcript:

1 Tree Implementations Chapter 16

2 2 Nodes in a Binary Tree The most common way uses a linked structure with each node represent each element in a binary tree. We could use an array to implement a tree. However, it is attractive only when the tree is complete. In such cases, the link between a parent and child is not stored explicitly.

3 3 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(); public int getHeight(); public int getNumberNodes(); } // end BinaryNodeInterface

4 4 Implementation of BinaryNode Usually the class that represents a node in a tree is a detail hidden from the client, e.g, node is private to link list. Since any class that extends our binary tree class might need to manipulate nodes, we define tree nodes outside of our binary tree class. However, the tree node is not public. It is placed within a package TreePackage Available to all classes in the same package involved in the implementation of a tree

5 5 Implementation of BinaryNode package TreePackage Class BinaryNode implements BinaryNodeInterface { private T data; private BinaryNode left; private BinaryNode right; public BinaryNode() { …. } …. }

6 6 Computing Height, Counting Nodes Within BinaryTree getHeight() { return root.getHeight();// public getHeight method } getNumberOfNodes() { return root.getNumberOfNodes(); } Implementation within BinaryNode is easier private getHeight(BinaryNode node) //tree rooted at the invoking node

7 7 Recursive getHeight private int getHeight(BinaryNode node) { int height = 0; if(node != null ) height = 1 + Math.max(getHeight(node.left), getHeight(node.right)); return height; }

8 8 Question How to write a recursive method to compute the total number of nodes in a binary tree?

9 9 Recursive getNumberOfNodes public int getNumberOfNodes() { …. }

10 10 Class BinaryTree package TreePackage; public class BinaryTree implements BinaryTreeInterface { private BinaryNodeInterface root; public BinaryTree(T rootData) { root = new BinaryNode ( rootData) } public BinaryTree( T rootData, BinaryTree leftTree, BinaryTree rightTree) { root = new BinaryNode (rootData); if(leftTree !=null) root.setLeftChild(leftTree.root); if(rightTree != null) root.setRightChild(rightTree.root); …. } …. }

11 11 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

12 12 Traversals A binary tree.

13 13 Traversals Using a stack to perform an inorder traversal of the binary tree.. Start from pushing the root, then traverse to the left as far as possible, pushing each node, then pop, if has right children, push again.

14 14 Traversals Using a queue to traverse the binary tree in level order.

15 15 General Trees A node for a general tree. Accommodate any number of children by referencing an object such as a list that contains the children. Two references, one reference is to the data object, and the other is to a list of child nodes.


Download ppt "Tree Implementations Chapter 16. 2 Nodes in a Binary Tree The most common way uses a linked structure with each node represent each element in a binary."

Similar presentations


Ads by Google