Skip to content

Instantly share code, notes, and snippets.

@mchayapol
Created October 21, 2015 03:52
Show Gist options
  • Select an option

  • Save mchayapol/6f0d6e31995c2320a804 to your computer and use it in GitHub Desktop.

Select an option

Save mchayapol/6f0d6e31995c2320a804 to your computer and use it in GitHub Desktop.
Tree sample
package edu.au.scitech.sc2101;
public class Tree<T> {
Node<T> root;
public Tree() {
root = null;
}
public boolean hasRoom(Node<T> n) {
return (n.left == null || n.right == null);
}
public int depth() {
return searchDepth(root);
}
public int searchDepth(Node<T> node){
if(node == null)
return 0;
int left = searchDepth(node.left);
int right = searchDepth(node.right);
int x = left > right ? left+1 : right+1;
/*
if (left > right)
x = left+1;
else
x = right+1;
*/
return x;
}
public void add(T i) {
Node<T> n = new Node<T>(i);
if (root == null) {
root = n;
return;
}
// Add to left node only
// Search for the deepest node on left handside
addLeft(root,n);
}
public void addLeft(Node<T> parent, Node<T> n) {
if (parent.left == null) {
parent.left = n;
} else {
addLeft(parent.left, n);
}
}
public int size() {
return walkTree(root);
}
public int walkTree(Node<T> n) {
if (n == null) return 0;
return 1 + walkTree(n.left) + walkTree(n.right);
}
class Node<T> {
Node<T> left; // node of type T
Node<T> right;
T data;
Node(T newData) {
left = right = null;
data = newData;
}
boolean isLeaf() {
return ((left == null) && (right == null));
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment