package com.trees;
import com.node.TreeNode;
public class BinaryTree {
TreeNode root;
public BinaryTree() {
}
public void addRoot(TreeNode node) {
this.root = node;
}
public static BinaryTree buildBTtoString(String str) {
BinaryTree bt = new BinaryTree();
bt.addRoot(buildBTtoString(str.split(",")));
return bt;
}
static int index = -1;
private static TreeNode buildBTtoString(String str[]) {
++index;
if (index < str.length && !str[index].equals("@")) {
TreeNode rt = new TreeNode(str[index]);
rt.left = buildBTtoString(str);
rt.right = buildBTtoString(str);
return rt;
}
return null;
}
public void print() {
print(this.root);
}
public void print(TreeNode node) {
if (node != null) {
print(node.left);
System.out.print(node.data + " ");
print(node.right);
}
}
public void printPreOrderWithoutRecursion() {
if (this.root == null)
return;
TreeNode curr = this.root;
System.out.print(curr.data + " ");
while ((curr = preOrderSuccessor(curr)) != null) {
System.out.print(curr.data + " ");
}
}
public TreeNode preOrderSuccessor(TreeNode node) {
if (node.left != null)
return node.left;
if (node.right != null)
return node.right;
TreeNode parent = node.parent;
while(parent != null &&parent.left == node) {
if(parent.right != null)
return parent.right;
else {
node = parent;
parent = node.parent;
}
}
while(parent != null && parent.right == node) {
node = parent;
parent = node.parent;
}
if(parent !=null && parent.right != null)
return parent.right;
return null;
}
public void printInOrderWithoutRecursion() {
if (this.root == null)
return;
TreeNode curr = this.root;
while (curr.left != null)
curr = curr.left;
// System.out.println(curr.data);
while (curr != null) {
System.out.print(curr.data + " ");
curr = inordersuccessor(curr);
}
}
public TreeNode inordersuccessor(TreeNode node) {
if (node.right != null) {
node = node.right;
while (node.left != null)
node = node.left;
return node;
}
TreeNode parent = node.parent;
while (parent != null && parent.right == node) {
node = parent;
parent = node.parent;
}
return parent;
}
public static BinaryTree buildBSTFromString(String str) {
BinaryTree bst = new BinaryTree();
bst.addRoot(buildBSTFromString(str.split(",")));
return bst;
}
private static TreeNode buildBSTFromString(String[] split) {
int index = 0;
TreeNode rt = null, curr = null;
int cmp = 0;
while (index < split.length) {
if (index == 0) {
rt = new TreeNode();
rt.data = split[index];
index++;
continue;
} else {
curr = rt;
while ((cmp = ((String) curr.data).compareTo(split[index])) != 0) {
if (cmp > 0) {
if (curr.left != null)
curr = curr.left;
else {
curr.left = new TreeNode(split[index]);
curr.left.parent = curr;
break;
}
} else {
if (curr.right != null)
curr = curr.right;
else {
curr.right = new TreeNode(split[index]);
curr.right.parent = curr;
break;
}
}
}
index++;
}
}
return rt;
}
public void printPostOrderWithoutRecursion() {
if(this.root == null)
return;
TreeNode node = this.root;
while(node.left != null)
node = node.left;
System.out.print(node.data+" ");
while((node = postOrderSuccessor(node)) != null ) {
System.out.print(node.data+" ");
}
}
private TreeNode postOrderSuccessor(TreeNode node) {
if(node != null) {
TreeNode parent = node.parent;
if(parent != null && parent.right != null && parent.right != node) {
TreeNode lNode= parent.right;
while(lNode != null) {
while(lNode.left != null)
lNode = lNode.left;
if(lNode.right != null)
lNode = lNode.right;
else
break;
}
return lNode;
}
return parent;
}
return null;
}
}
Comments
Post a Comment