Skip to content

Instantly share code, notes, and snippets.

@thmain
Last active December 7, 2021 18:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save thmain/3b89e795917e83c783e3 to your computer and use it in GitHub Desktop.
Save thmain/3b89e795917e83c783e3 to your computer and use it in GitHub Desktop.
public class TreeHeight {
public int treeHeight(Node root){
if(root==null)return 0;
return (1+ Math.max(treeHeight(root.left),treeHeight(root.right)));
}
public static void main (String[] args) throws java.lang.Exception
{
Node root = new Node(5);
root.left = new Node(10);
root.right = new Node(15);
root.left.left = new Node(20);
root.left.right = new Node(25);
root.left.left.left =new Node(30);
root.left.right.left = new Node(35);
root.left.right.left.left = new Node(40);
root.left.right.left.left.right = new Node(45);
root.left.right.left.left.right.left = new Node(50);
TreeHeight i = new TreeHeight();
System.out.println("Tree Height: "+i.treeHeight(root));
}
}
class Node{
int data;
Node left;
Node right;
public Node(int data){
this.data = data;
this.left = null;
this.right = null;
}
}
@XinerLove
Copy link

can you help me check my code, I need add public int treeHeight to the implementation.
import interfaces.*;
import nodes.BSTNode;

public class BinarySearchTree<T extends Comparable>
implements BSTInterface
{
protected BSTNode root; // reference to the root of this BST

boolean found;   // used by remove
int rightHeight = -1;
int leftHeight = -1;

// for traversals
protected ArrayQueue<T> inOrderQ;
protected ArrayQueue<T> preOrderQ;
protected ArrayQueue<T> postOrderQ;

public BinarySearchTree() {
	root = null;
}


public void add (T element) {
	root = recAdd(element, root);
}

private BSTNode<T> recAdd(T element, BSTNode<T> tree) {
	if (tree == null) {
		tree = new BSTNode<T>(element);
	}
	else {
		if (element.compareTo(tree.getData()) <= 0) {
			tree.setLeft(recAdd(element, tree.getLeft()));  // add to left subtree
			
		}
		else {
			tree.setRight(recAdd(element, tree.getRight()));  // add to right subtree
	    }
	}
	return tree;
}


  public boolean remove (T element) {
	  root = recRemove(element, root);
	  return found;
  }
  
  private BSTNode<T> recRemove(T element, BSTNode<T> tree) {
	  if (tree == null) {
		  found = false;
	  }
	  else {
		  if (element.compareTo(tree.getData()) < 0) {
			  tree.setLeft(recRemove(element, tree.getLeft()));
		  }
	      else {
	    	  if (element.compareTo(tree.getData()) > 0) {
	    		  tree.setRight(recRemove(element, tree.getRight()));
	    	  }
	          else {
	              tree = removeNode(tree);
	              found = true;
	          }
	      }
	  }
      return tree;
  }

  private BSTNode<T> removeNode(BSTNode<T> tree) {

	  T payload;
	  
	  if (tree.getLeft() == null) {
		  return tree.getRight();
	  }
	  else {
		  if (tree.getRight() == null) {
			  return tree.getLeft();
		  }
	      else {
	    	  payload = getPredecessor(tree.getLeft());
	          tree.setData(payload);
	          tree.setLeft(recRemove(payload, tree.getLeft()));  
	          return tree;
	      }
	  }
  }

  private T getPredecessor(BSTNode<T> tree) {
	  while (tree.getRight() != null) {
	      tree = tree.getRight();
	  }
      return tree.getData();
  }

  
  public int size() {
	  return recSize(root);
  }

  private int recSize(BSTNode<T> tree) {
	  if (tree == null) {
	      return 0;
	  }
	  else {
	      return recSize(tree.getLeft()) + recSize(tree.getRight()) + 1;
	  }
  }


  // this implementation of a size operation demonstrates that
  // it is possible to visit all the nodes of the tree without recursion	  
  public int size2() {
	  int count = 0;
	  if (root != null) {
		  LLStack<BSTNode<T>> hold = new LLStack<BSTNode<T>>();
	      BSTNode<T> currNode;
	      hold.push(root);
	      while (!hold.isEmpty()) {
	         currNode = hold.peek();
	         hold.pop();
	         count++;
	         if (currNode.getLeft() != null) {
	            hold.push(currNode.getLeft());
	         }
	         if (currNode.getRight() != null) {
	            hold.push(currNode.getRight());
	         }  
	      }
	  }
	  return count;
  }

	  
  public boolean isEmpty() {
	  return (root == null);
  }

public boolean contains (T element) {
return recContains(element, root);
}

private boolean recContains(T element, BSTNode tree) {
if (tree == null) return false;
else
if (element.compareTo(tree.getData()) < 0)
return recContains(element, tree.getLeft()); // search left subtree
else
if (element.compareTo(tree.getData()) > 0)
return recContains(element, tree.getRight()); // search right subtree
else
return true; // element is found!
}

public T get(T element) {
return recGet(element, root);
}

private T recGet(T element, BSTNode tree) {
if (tree == null)
return null;
else
if (element.compareTo(tree.getData()) < 0)
return recGet(element, tree.getLeft()); // get from left subtree
else
if (element.compareTo(tree.getData()) > 0)
return recGet(element, tree.getRight()); // get from right subtree
else
return tree.getData(); // element is found!
}

// populate inOrderQ with tree elements based on in-order traversal
private void inOrder(BSTNode tree) {
if (tree != null) {
inOrder(tree.getLeft());
inOrderQ.enqueue(tree.getData());
inOrder(tree.getRight());
}
}

// populate preOrderQ with tree elements based on pre-order traversal
private void preOrder(BSTNode tree) {
if (tree != null) {
preOrderQ.enqueue(tree.getData());
preOrder(tree.getLeft());
preOrder(tree.getRight());
}
}

// populate postOrderQ with tree elements based on post-order traversal
private void postOrder(BSTNode tree) {
if (tree != null) {
postOrder(tree.getLeft());
postOrder(tree.getRight());
postOrderQ.enqueue(tree.getData());
}
}

public int reset(TraversalType orderType) {
// returns current number of nodes in the tree

int numNodes = size();

switch (orderType) {
   case INORDER :
	   inOrderQ = new ArrayQueue<T>(numNodes);
	   inOrder(root);
	   break;

   case PREORDER :
	   preOrderQ = new ArrayQueue<T>(numNodes);
	   preOrder(root);
	   break;

   case POSTORDER :
	   postOrderQ = new ArrayQueue<T>(numNodes);
	   postOrder(root);
	   break;
}

return numNodes;

}

public T getNext (TraversalType orderType) {
/*

  • preconditions
    • the BST is not empty
    • the BST has been reset for orderType
    • the BST has not been modified since the most recent reset
    • application code is responsible for not iterating beyond the end of the tree
  • Returns the element at the current position on the BST for the specified traversal type
  • and advances the value of the current position.

*/
switch (orderType) {
case INORDER : return inOrderQ.dequeue();
case PREORDER : return preOrderQ.dequeue();
case POSTORDER: return postOrderQ.dequeue();
default: return null;
}
}

public int treeHeight(BSTNode root) {
return findHeight( root);
}

private int left(BSTNode root) {
int leftHeight = -1;

  if(root.getLeft() != null)
	  leftHeight = findHeight(root.getLeft());
  return leftHeight;

}
private int findHeight(BSTNode left) {
// TODO Auto-generated method stub
return 0;
}

private int right(BSTNode root) {
int rightHeight = -1;
if(root.getRight()!= null)
rightHeight = findHeight(root.getRight());

	  return rightHeight;

}
private int findHeight(BSTNode root, BSTNodetree) {

  if(tree == null) {
	  return -1;}
  if(root != null) {
	  return 0;}
  else {
	  int max =(leftHeight > rightHeight) ? leftHeight : rightHeight;
return max + 1;
  }

}
}
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment