You are on page 1of 7

EXERCISES 3 Tree

1. For each tree shown below

4 0 3 0 3 2 5 5 3 2 3 0 3 5

4 0 5 5 5 0

(A)

(B)

a. Answer these questions: What is its height? Is it a full binary tree? Is it a complete binary tree? Is it a binary search tree? b. If visiting a node displays the integer value stored, show the inorder, preorder and postorder traversal of each tree 2. Show the tree that would be built by the following input string, then create this tree using Create method 30 15 4 null null 20 18 null 19 null null null 35 32 null null 38 null

null 3. Show the BSTree that would be formed by adding following values to the tree: a. happy, depressed, manic, sad, ecstatic b. 45, 30, 15, 50, 60, 20, 25, 90 4. For each tree shown below, show or explain the effect of removing the nodes 15, 30 from the tree
4 8 2 8 1 5 1 1 2 1 7 2 3 2 6 3 0 3 7 4 2 4 9 5 1 5 2 6 0 6 3 7 0 5 5 7 1 8 3 9 0

5. Build a class for Binary Search Tree of objects having parameter type a. Implement a recursive and non-recursive method that adds a value to the BSTree, and returns true if the add is sucessful, otherwise returns false b. Implement a recursive and non-recursive method that searches a BSTree looking for the node having specified value and returns a reference variable to that node if found, otherwise returns null c. Implement a method to remove a specified value from the BSTree, and return the removed value if the remove is sucessful, otherwise return null d. Implement recursive and non-recursive methods to perform an inorder, preorder and postorder traversal of a BSTree and display the nodes value e. Implement a method that returns the number of nodes in the BST f. Implement a method that returns the number of nodes having odd value in the BST g. Implement a method that returns the average of nodes having even value in the BST h. Implement a method that returns the height of the BST i. Implement a method that returns the depth or level of the node having specified value j. Implement a method that returns the number of leaf nodes in the BST k. Implement a method that returns the number of nodes at the first level in the BST

l. BST m. BST n. o. tree

Implement a method that returns the number of nodes at the second level in the Implement a method that returns the number of nodes at specified level in the Implement a method that returns the number of internal nodes in the BST Write a recursive and non-recursive method to check if the tree is binary search Recursive: left subtree: BST and right subtree: BST and max(datas of left childs) < root.data < min(datas of right childs) Non-Recursive: inorder traversal, if node datas are increasing: BST Implement a method that returns the height of the node having specified value Implement a method that displays the values of nodes at specified level in the

p. q. BST 6. Build a class for Balanced Search Tree of objects having parameter type a. Implement a method that adds a value to the Tree, and returns true if the add is sucessful, otherwise returns false b. Write a method to check if the tree is balanced binary tree 7. For BSTree shown below:
root
3 8 3 1

a.

Draw the BSTree that would be built by adding following values to the tree: 32, 69, 18, 11, 28, 70, 23, 9, 37, 47 b. 45, 30, 15, 50, 60, 20, 25, 90

c. Show the inorder, preorder and postorder traversal for displaying the nodes value in the tree after adding above values 8. Create following tree using Create method 8
T
1 1 1 1 2 1 0 3 0

9. Draw process of stack uesed to build binary expression trees for the following infix expressions. Your trees should enforce the Java rules for operator evaluation (higherprecedence operators before lower-precedence operators and left associativity) a. x/y + a b*c b. x*a y/b * (c+d) 10. For expression: (A+B) * (H - (E F ^ D) / C) a. Build expression tree from the above expression b. Build postfix expression from the expression c. Present algorism of evaluating an expression tree d. Draw process of stack used to evaluate the expression
import java.util.*; public class BSTree<E> { private Node<E> root; public static private private private class Node<E> { Node<E>left; Node<E>right; E data;

//Tree includes 1 node having the value d public Node(E d){ left = null; right = null; data = d; } //Tree includes the root node d, //and left subtree l, right subtree r public Node(E d, Node<E>l, Node<E>r) { left = l; right = r; data = d; } } //Initiate tree having root public Node<E> Create(E d){

root = new Node<E>(d); return root; } //Initiate tree having root, and left subtree l, right subtree r public Node<E> Create(E d, Node<E> l, Node<E> r){ root = new Node<E>(d, l, r); return root; } //inorder traversal private void inorderTraversal(Node<E> r) { if (r == null) return; inorderTraversal(r.left); System.out.print(r.data+" "); inorderTraversal(r.right); } public void inorderTraversal(){ inorderTraversal(root); } //inorder traversal return String private String inorderTraversal1(Node<E> r) { if (r == null) return ""; return inorderTraversal1(r.left) + r.data + inorderTraversal1(r.right); } public String innorderTraversal1(){ return inorderTraversal1(root); } //preorder traversal return String private void preorderTraversal1(Node<E> r) { if (r == null) return; System.out.print( r.data+" "); preorderTraversal1(r.left); preorderTraversal1(r.right); } public void prenorderTraversal1(){ preorderTraversal1(root); } //postorder traversal return String private void postorderTraversal1(Node<E> r) { if (r == null) return; postorderTraversal1(r.left); postorderTraversal1(r.right); System.out.print(r.data+" "); } public void postorderTraversal1(){ postorderTraversal1(root); } /********** * This support routine searches a specified subtree looking * for the item specified as the target. * @param r The root of a subtree to be searched

* @param target The target of the search * @return The node having the target value of the search if found, otherwise null */ private Node<E> searchBST(Node<E> r, Comparable<E> target) { if (r == null) return null; int result = target.compareTo(r.data); if (result == 0) return r; if (result < 0) return searchBST(r.left, target); return searchBST(r.right, target); } /********** * This routine searches the BSTree looking for the specified item. * @param target The target of the search * @return The target is returned if found or a null is */ public Node<E> searchBST(E target) { return searchBST((Node<E>)root,(Comparable<E>)target); } private boolean returnValue; // Used to signal if an add was successful /********** * This method is the entry point routines outside of this class use to add * a value to the BST. * @param newValue The target of the search * @return The target is returned if found or a null is */ public boolean add(Comparable<E> newValue) { root = add(root, newValue); return returnValue; } /********** * This is private method is used to add a value to the BST via recursion. * @param newValue The target of the search * @return A reference to the updated subtree after the add */ private Node<E> add(Node<E> r, Comparable<E> newValue) { if (r == null) return new Node(newValue); int compareResult = newValue.compareTo(r.data); if (compareResult == 0) return r; if (compareResult < 0) { r.left = add(r.left, newValue); return r; } r.right = add(r.right, newValue); return r; } public static void main(String[] args){ BSTree<Integer> tree = new BSTree<Integer>(); tree.add(24); tree.add(15); tree.add(30);

} }

tree.add(5); tree.add(7); tree.add(25); tree.add(56); tree.add(17); tree.add(22); tree.add(20); tree.inorderTraversal(); System.out.println(); tree.preorderTraversal1(); System.out.println(); //Creat Binary tree /*BSTree<Integer> tree = new BSTree<Integer>(); tree.Create(7, tree.Create(5, null, tree.Create(9, tree.Create(11), null)), tree.Create(8, tree.Create(6), tree.Create(3, tree.Create(2),tree.Create(4)) ) ); tree.inorderTraversal(); */