Skip to content

Instantly share code, notes, and snippets.

@sadikovi
Created March 5, 2017 05:01
Show Gist options
  • Save sadikovi/820b31cd80daf971eb9ad3b573160176 to your computer and use it in GitHub Desktop.
Save sadikovi/820b31cd80daf971eb9ad3b573160176 to your computer and use it in GitHub Desktop.
BST with AVL balancing, only supports inserts and check if element is in the tree
public class Tree<T extends Comparable<T>> {
static class TreeNode<T> {
T value;
int height;
TreeNode<T> left;
TreeNode<T> right;
public TreeNode(T value) {
this.value = value;
this.height = 0;
this.left = null;
this.right = null;
}
@Override
public String toString() {
return "[value=" + this.value + ", height=" + this.height + "]";
}
}
private TreeNode<T> root;
private int height(TreeNode<T> node) {
return (node == null) ? -1 : node.height;
}
private int balanceFactor(TreeNode<T> node) {
return height(node.left) - height(node.right);
}
private TreeNode<T> balance(TreeNode<T> node) {
if (balanceFactor(node) < -1) {
if (balanceFactor(node.right) > 0) {
node.right = rotateRight(node.right);
}
node = rotateLeft(node);
} else if (balanceFactor(node) > 1) {
if (balanceFactor(node.left) < 0) {
node.left = rotateLeft(node.left);
}
node = rotateRight(node);
}
return node;
}
private TreeNode<T> rotateRight(TreeNode<T> node) {
TreeNode<T> tmp = node.left;
node.left = tmp.right;
tmp.right = node;
node.height = 1 + Math.max(height(node.left), height(node.right));
tmp.height = 1 + Math.max(height(tmp.left), height(tmp.right));
return tmp;
}
private TreeNode<T> rotateLeft(TreeNode<T> node) {
TreeNode<T> tmp = node.right;
node.right = tmp.left;
tmp.left = node;
node.height = 1 + Math.max(height(node.left), height(node.right));
tmp.height = 1 + Math.max(height(tmp.left), height(tmp.right));
return tmp;
}
private TreeNode<T> insert(T value, TreeNode<T> node) {
if (node == null) return new TreeNode<T>(value);
if (node.value.compareTo(value) == 0) return node;
if (node.value.compareTo(value) > 0) {
node.left = insert(value, node.left);
} else {
node.right = insert(value, node.right);
}
node.height = 1 + Math.max(height(node.left), height(node.right));
return balance(node);
}
public void insert(T value) {
this.root = insert(value, this.root);
}
private boolean contains(T value, TreeNode<T> node) {
if (node == null) return false;
if (node.value.compareTo(value) == 0) return true;
if (node.value.compareTo(value) > 0) {
return contains(value, node.left);
} else {
return contains(value, node.right);
}
}
public boolean contains(T value) {
return contains(value, this.root);
}
private boolean isBalanced(TreeNode<T> node) {
if (node == null) return true;
int factor = balanceFactor(node);
if (factor > 1 || factor < -1) return false;
return isBalanced(node.left) && isBalanced(node.right);
}
public boolean isBalanced() {
return isBalanced(this.root);
}
private boolean isBST(TreeNode<T> node, T min, T max) {
if (node == null) return true;
if (min != null && node.value.compareTo(min) < 0) return false;
if (max != null && node.value.compareTo(max) > 0) return false;
return isBST(node.left, min, node.value) && isBST(node.right, node.value, max);
}
public boolean isBST() {
return isBST(this.root, null, null);
}
private String debug(String margin, TreeNode node) {
if (node == null) {
return margin + "null";
} else {
return margin + node + ": " + "\n" +
debug(margin + " ", node.left) + "\n" +
debug(margin + " ", node.right);
}
}
public String debug() {
return debug("", this.root);
}
@Override
public String toString() {
return "[init=" + (this.root != null) + ", balanced=" + isBalanced() + ", bst=" + isBST() + "]";
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment