1Objective Binary Search Tree traversal 2 points Use travers
(1)Objective: Binary Search Tree traversal (2 points)
Use traversal.pptx as guidance to write a program to build a binary search tree Dictionary. I of BST <Key,E> have the same data from each input record.
Download traversal-lab.pptx, inventory.txt, BinNode.java, BSTNode.java, BST.java, Dictionary.java .
Perform specifications as follow:
(a)Provide Add, Delete and Retrieve functions for user to access the database. Reject duplicate record when add a new record.
(b)Modify BST.java to add printpostOrder, printpreOrder methods.
(c)At the end, display inorder, postorder and preorder of the tree.
Codes:
/** Source code example for \"A Practical Introduction to Data
 Structures and Algorithm Analysis, 3rd Edition (Java)\"
 by Clifford A. Shaffer
 Copyright 2008-2011 by Clifford A. Shaffer
 */
/** ADT for binary tree nodes */
 public interface BinNode<E> {
 /** Get and set the element value */
 public E element();
 public void setElement(E v);
/** @return The left child */
 public BinNode<E> left();
/** @return The right child */
 public BinNode<E> right();
/** @return True if a leaf node, false otherwise */
 public boolean isLeaf();
 }
 //********************************************************************
  // StringTree.java   
 //
 //********************************************************************
import java.util.*;
public class StringTree
 {
 private Node root;
 //----------------------------------------------------------------
  // Creates an initially empty tree.
 //----------------------------------------------------------------
  public StringTree()
 {
 root = null;
 }
 //----------------------------------------------------------------
  // Adds a string to the tree.
 //----------------------------------------------------------------
  public void addString (String str)
 {
 root = addStringToSubTree(str, root);
 }
 //----------------------------------------------------------------
  // Adds a string to the subtree with the given root node
 //----------------------------------------------------------------
  private Node addStringToSubTree (String str, Node node)
 {
 Node result = node;
 if (node == null)
 result = new Node(str);
       
 // If the new string comes before the string in the node, add
 // the new string to the left child. Otherwise, add it to the
 // right child.
 else
 if (str.compareTo(node.value) < 0)
 node.left = addStringToSubTree(str, node.left);
 else
 node.right = addStringToSubTree(str, node.right);
return result;
 }
 //----------------------------------------------------------------
  // Prints the result of a depth-first traversal of the tree using
 // recursion.
 //----------------------------------------------------------------
  public void traverseWithRecursion()
 {
 traverseWithRecursion(root);
 }
 //----------------------------------------------------------------
  // Prints the elements in the specified tree using recursion.
 //----------------------------------------------------------------
  private void traverseWithRecursion (Node node)
 {
 if (node != null)
 {
 traverseWithRecursion (node.left);
System.out.print (node.value+\" \");
traverseWithRecursion (node.right);
 }
 }
 }
 //********************************************************************
  // Node for a binary tree of Strings.
 //********************************************************************
  class Node
 {
 public String value;
 public Node left;
 public Node right;
public Node (String value)
 {
 this.value = value;
 this.left = left;
 this.right = right;
 }
 }
/** Source code example for \"A Practical Introduction to Data
 Structures and Algorithm Analysis, 3rd Edition (Java)\"
 by Clifford A. Shaffer
 Copyright 2008-2011 by Clifford A. Shaffer
 */
/** Binary tree node implementation: Pointers to children
 @param E The data element
 @param Key The associated key for the record */
 import java.util.*;
 class BSTNode<Key, E> implements BinNode<E> {
 private Key key; // Key for this node
 private E element; // Element for this node
 private BSTNode<Key,E> left; // Pointer to left child
 private BSTNode<Key,E> right; // Pointer to right child
/** Constructors */
 public BSTNode() {left = right = null; }
 public BSTNode(Key k, E val)
 { left = right = null; key = k; element = val; }
 public BSTNode(Key k, E val,
 BSTNode<Key,E> l, BSTNode<Key,E> r)
 { left = l; right = r; key = k; element = val; }
/** Get and set the key value */
 public Key key() { return key; }
 public void setKey(Key k) { key = k; }
/** Get and set the element value */
 public E element() { return element; }
 public void setElement(E v) { element = v; }
/** Get and set the left child */
 public BSTNode<Key,E> left() { return left; }
 public void setLeft(BSTNode<Key,E> p) { left = p; }
/** Get and set the right child */
 public BSTNode<Key,E> right() { return right; }
 public void setRight(BSTNode<Key,E> p) { right = p; }
/** @return True if a leaf node, false otherwise */
 public boolean isLeaf()
 { return (left == null) && (right == null); }
 }
   
/** Source code example for \"A Practical Introduction to Data
 Structures and Algorithm Analysis, 3rd Edition (Java)\"
 by Clifford A. Shaffer
 Copyright 2008-2011 by Clifford A. Shaffer
 */
import java.lang.Comparable;
/** Binary Search Tree implementation for Dictionary ADT */
 class BST<Key extends Comparable<? super Key>, E>
 implements Dictionary<Key, E> {
 private BSTNode<Key,E> root; // Root of the BST
 int nodecount; // Number of nodes in the BST
/** Constructor */
 BST() { root = null; nodecount = 0; }
/** Reinitialize tree */
 public void clear() { root = null; nodecount = 0; }
/** Insert a record into the tree.
 @param k Key value of the record.
 @param e The record to insert. */
 public void insert(Key k, E e) {
 root = inserthelp(root, k, e);
 nodecount++;
 }
// Return root
public BSTNode getRoot()
 {
 return root;
 }
/** Remove a record from the tree.
 @param k Key value of record to remove.
 @return The record removed, null if there is none. */
 
 public E remove(Key k) {
 E temp = findhelp(root, k); // First find it
 if (temp != null) {
 root = removehelp(root, k); // Now remove it
 nodecount--;
 }
 return temp;
 }
/** Remove and return the root node from the dictionary.
 @return The record removed, null if tree is empty. */
 public E removeAny() {
 if (root == null) return null;
 E temp = root.element();
 root = removehelp(root, root.key());
 nodecount--;
 return temp;
 }
/** @return Record with key value k, null if none exist.
 @param k The key value to find. */
 public E find(Key k) { return findhelp(root, k); }
/** @return The number of records in the dictionary. */
 public int size() { return nodecount; }
   
 private E findhelp(BSTNode<Key,E> rt, Key k) {
 if (rt == null) return null;
 if (rt.key().compareTo(k) > 0)
 return findhelp(rt.left(), k);
 else if (rt.key().compareTo(k) == 0) return rt.element();
 else return findhelp(rt.right(), k);
 }
 /** @return The current subtree, modified to contain
 the new item */
 private BSTNode<Key,E> inserthelp(BSTNode<Key,E> rt,
 Key k, E e) {
 if (rt == null) return new BSTNode<Key,E>(k, e);
 if (rt.key().compareTo(k) > 0)
 rt.setLeft(inserthelp(rt.left(), k, e));
 else
 rt.setRight(inserthelp(rt.right(), k, e));
 return rt;
 }
 /** Remove a node with key value k
 @return The tree with the node removed */
 
 private BSTNode<Key,E> removehelp(BSTNode<Key,E> rt,Key k) {
 if (rt == null) return null;
 if (rt.key().compareTo(k) > 0)
 rt.setLeft(removehelp(rt.left(), k));
 else if (rt.key().compareTo(k) < 0)
 rt.setRight(removehelp(rt.right(), k));
 else { // Found it
 if (rt.left() == null) return rt.right();
 else if (rt.right() == null) return rt.left();
 else { // Two children
 BSTNode<Key,E> temp = getmin(rt.right());
 rt.setElement(temp.element());
 rt.setKey(temp.key());
 rt.setRight(deletemin(rt.right()));
 }
 }
 return rt;
 }
private BSTNode<Key,E> getmin(BSTNode<Key,E> rt) {
 if (rt.left() == null) return rt;
 return getmin(rt.left());
 }
private BSTNode<Key,E> deletemin(BSTNode<Key,E> rt) {
 if (rt.left() == null) return rt.right();
 rt.setLeft(deletemin(rt.left()));
 return rt;
 }
 private void printhelp(BSTNode<Key,E> rt) {
 if (rt == null) return;
 printhelp(rt.left());
 printVisit(rt.element());
 printhelp(rt.right());
 }
private StringBuffer out;
public String toString() {
 out = new StringBuffer(400);
 printhelp(root);
 return out.toString();
 }
 private void printVisit(E it) {
 out.append(it + \"\ \");
 }
}
/** Source code example for \"A Practical Introduction to Data
 Structures and Algorithm Analysis, 3rd Edition (Java)\"
 by Clifford A. Shaffer
 Copyright 2008-2011 by Clifford A. Shaffer
 */
/** The Dictionary abstract class. */
 public interface Dictionary<Key, E> {
/** Reinitialize dictionary */
 public void clear();
/** Insert a record
 @param k The key for the record being inserted.
 @param e The record being inserted. */
 public void insert(Key k, E e);
/** Remove and return a record.
 @param k The key of the record to be removed.
 @return A maching record. If multiple records match
 \"k\", remove an arbitrary one. Return null if no record
 with key \"k\" exists. */
 public E remove(Key k);
/** Remove and return an arbitrary record from dictionary.
 @return the record removed, or null if none exists. */
 public E removeAny();
/** @return A record matching \"k\" (null if none exists).
 If multiple records match, return an arbitrary one.
 @param k The key of the record to find */
 public E find(Key k);
/** @return The number of records in the dictionary. */
 public int size();
 };
Text file :
CT16C1288B
 DT14B1225F
 MI15B1250A
 MI15B1251A
 HO03N1095A
 HY07D1095BQ
 KI04D2593C
 DG12A1240AQ
 HY03G2593BQ
 TO30A1310A
 HO03N1095AQ
 HO01H1351C
 HO01H1350C
 FT18A1288B
 LR15A1000A
 BM12E1000A
 VW02B3113A
 NI23H1230AQ
 LX03D2503A
 LX03D2502A
 LX03D2502A
 VW22A3113B
 VW22B3113A
Solution
import java.util.Scanner;
 
 /* Class BSTNode */
 class BSTNode
 {
 BSTNode left, right;
 int data;
 
 /* Constructor */
 public BSTNode()
 {
 left = null;
 right = null;
 data = 0;
 }
 /* Constructor */
 public BSTNode(int n)
 {
 left = null;
 right = null;
 data = n;
 }
 /* Function to set left node */
 public void setLeft(BSTNode n)
 {
 left = n;
 }
 /* Function to set right node */
 public void setRight(BSTNode n)
 {
 right = n;
 }
 /* Function to get left node */
 public BSTNode getLeft()
 {
 return left;
 }
 /* Function to get right node */
 public BSTNode getRight()
 {
 return right;
 }
 /* Function to set data to node */
 public void setData(int d)
 {
 data = d;
 }
 /* Function to get data from node */
 public int getData()
 {
 return data;
 }   
 }
 
 /* Class BST */
 class BST
 {
 private BSTNode root;
 
 /* Constructor */
 public BST()
 {
 root = null;
 }
 /* Function to check if tree is empty */
 public boolean isEmpty()
 {
 return root == null;
 }
 /* Functions to insert data */
 public void insert(int data)
 {
 root = insert(root, data);
 }
 /* Function to insert data recursively */
 private BSTNode insert(BSTNode node, int data)
 {
 if (node == null)
 node = new BSTNode(data);
 else
 {
 if (data <= node.getData())
 node.left = insert(node.left, data);
 else
 node.right = insert(node.right, data);
 }
 return node;
 }
 /* Functions to delete data */
 public void delete(int k)
 {
 if (isEmpty())
 System.out.println(\"Tree Empty\");
 else if (search(k) == false)
 System.out.println(\"Sorry \"+ k +\" is not present\");
 else
 {
 root = delete(root, k);
 System.out.println(k+ \" deleted from the tree\");
 }
 }
 private BSTNode delete(BSTNode root, int k)
 {
 BSTNode p, p2, n;
 if (root.getData() == k)
 {
 BSTNode lt, rt;
 lt = root.getLeft();
 rt = root.getRight();
 if (lt == null && rt == null)
 return null;
 else if (lt == null)
 {
 p = rt;
 return p;
 }
 else if (rt == null)
 {
 p = lt;
 return p;
 }
 else
 {
 p2 = rt;
 p = rt;
 while (p.getLeft() != null)
 p = p.getLeft();
 p.setLeft(lt);
 return p2;
 }
 }
 if (k < root.getData())
 {
 n = delete(root.getLeft(), k);
 root.setLeft(n);
 }
 else
 {
 n = delete(root.getRight(), k);
 root.setRight(n);   
 }
 return root;
 }
 /* Functions to count number of nodes */
 public int countNodes()
 {
 return countNodes(root);
 }
 /* Function to count number of nodes recursively */
 private int countNodes(BSTNode r)
 {
 if (r == null)
 return 0;
 else
 {
 int l = 1;
 l += countNodes(r.getLeft());
 l += countNodes(r.getRight());
 return l;
 }
 }
 /* Functions to search for an element */
 public boolean search(int val)
 {
 return search(root, val);
 }
 /* Function to search for an element recursively */
 private boolean search(BSTNode r, int val)
 {
 boolean found = false;
 while ((r != null) && !found)
 {
 int rval = r.getData();
 if (val < rval)
 r = r.getLeft();
 else if (val > rval)
 r = r.getRight();
 else
 {
 found = true;
 break;
 }
 found = search(r, val);
 }
 return found;
 }
 /* Function for inorder traversal */
 public void inorder()
 {
 inorder(root);
 }
 private void inorder(BSTNode r)
 {
 if (r != null)
 {
 inorder(r.getLeft());
 System.out.print(r.getData() +\" \");
 inorder(r.getRight());
 }
 }
 /* Function for preorder traversal */
 public void preorder()
 {
 preorder(root);
 }
 private void preorder(BSTNode r)
 {
 if (r != null)
 {
 System.out.print(r.getData() +\" \");
 preorder(r.getLeft());   
 preorder(r.getRight());
 }
 }
 /* Function for postorder traversal */
 public void postorder()
 {
 postorder(root);
 }
 private void postorder(BSTNode r)
 {
 if (r != null)
 {
 postorder(r.getLeft());   
 postorder(r.getRight());
 System.out.print(r.getData() +\" \");
 }
 }   
 }
 
 /* Class BinarySearchTree */
 public class BinarySearchTree
 {
 public static void main(String[] args)
 {   
 Scanner scan = new Scanner(System.in);
 /* Creating object of BST */
 BST bst = new BST();
 System.out.println(\"Binary Search Tree Test\ \");
 char ch;
 /* Perform tree operations */
 do
 {
 System.out.println(\"\ Binary Search Tree Operations\ \");
 System.out.println(\"1. insert \");
 System.out.println(\"2. delete\");
 System.out.println(\"3. search\");
 System.out.println(\"4. count nodes\");
 System.out.println(\"5. check empty\");
 
 int choice = scan.nextInt();
 switch (choice)
 {
 case 1 :
 System.out.println(\"Enter integer element to insert\");
 bst.insert( scan.nextInt() );   
 break;
 case 2 :
 System.out.println(\"Enter integer element to delete\");
 bst.delete( scan.nextInt() );   
 break;   
 case 3 :
 System.out.println(\"Enter integer element to search\");
 System.out.println(\"Search result : \"+ bst.search( scan.nextInt() ));
 break;
 case 4 :
 System.out.println(\"Nodes = \"+ bst.countNodes());
 break;   
 case 5 :
 System.out.println(\"Empty status = \"+ bst.isEmpty());
 break;
 default :
 System.out.println(\"Wrong Entry \  \");
 break;   
 }
 /* Display tree */
 System.out.print(\"\ Post order : \");
 bst.postorder();
 System.out.print(\"\ Pre order : \");
 bst.preorder();
 System.out.print(\"\ In order : \");
 bst.inorder();
 
 System.out.println(\"\ Do you want to continue (Type y or n) \ \");
 ch = scan.next().charAt(0);
 } while (ch == \'Y\'|| ch == \'y\');   
 }
 }











