From 2a9547060ff8cfb77cf75a429e9bc5f084f2a8f8 Mon Sep 17 00:00:00 2001 From: RustyRaptor Date: Tue, 19 Nov 2019 12:12:46 -0700 Subject: [PATCH] Started Lab 12: added BST file --- src/BST.java | 300 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 300 insertions(+) create mode 100644 src/BST.java diff --git a/src/BST.java b/src/BST.java new file mode 100644 index 0000000..2231f8e --- /dev/null +++ b/src/BST.java @@ -0,0 +1,300 @@ + + + +/** + * Node for Binary Tree + * + * @author Huiping Cao + */ +class BSTNode{ + private int data; //the element value for this node + private BST left; //the left child of this node + private BST right; //the right child of this node + + /** + * No-argument constructor + */ + public BSTNode(){} + + /** + * Constructor with the initial element + * @param initData: the initial element + */ + public BSTNode(int initData){ + data = initData; + left = new BST(); + right = new BST(); + } + + /** + * Constructor with the initial element, initial left and right children + * @param initData: the initial element + * @param initParent: the initial parent tree + * @param initLeft: left child + * @param initRight: right child + */ + public BSTNode(int initData, BST initLeft, BST initRight){ + data = initData; + left = initLeft; + right = initRight; + } + + /** + * Evaluate whether this node is a leaf node or not + * @return true if it is a leaf node; otherwise, return false. + */ + public boolean isLeaf() + { + return (((left==null)||(left!=null && left.root==null)) + &&((right==null)||(right!=null && right.root==null))); + } + + + /** + * @return the data + */ + public int getData() { + return data; + } + + /** + * @param set the element in this node + */ + public void setData(int data) { + this.data = data; + } + + /** + * @return the left child + */ + public BST getLeft() { + return left; + } + + /** + * @param the left child to be set + */ + public void setLeft(BST left) { + this.left = left; + } + + /** + * @return the right child + */ + public BST getRight() { + return right; + } + + /** + * @param the right child to be set + */ + public void setRight(BST right) { + this.right = right; + } + + + /** + * Convert this BTNode to a string + */ + public String toString() + { + String str=""; + + if((left==null)||(left!=null && left.root==null)) str +="(null)"; + else str +="("+left.root.getData()+")"; + + str += data; + + if((right==null)||(right!=null&&right.root==null)) str +="(null)"; + else str +="("+right.root.getData()+")"; + + return str; + } +} + + +/** + * The class for Binary Search Tree + * @author Huiping Cao + * + */ +public class BST { + protected BSTNode root; //the tree root + + + /** + * Get the left subtree of this tree + * @return the left subtree of this tree + */ + private BST getLeftSubtree(){ + return root.getLeft(); + } + + /** + * Get the right subtree of this tree + * @return the right subtree of this tree + */ + private BST getRightSubtree(){ + return root.getRight(); + } + + /** + * Print the tree using in-order traversal strategy + */ + public void inOrderPrt(){ + inOrderPrt(0); + } + + /** + * private, recursive function + * I slightly changed the method to print right subtree first + * It is to make the display more easier to read + * + * @param node + * @param indentation + * @param branch + */ + private void inOrderPrt(int indentation){ + if(root!=null){ + if(getRightSubtree()!=null) getRightSubtree().inOrderPrt(indentation+1); + + for(int i=0;i