import java.util.LinkedList;
import java.util.Queue;
class Main {
public static void main(String[] args) {
System.out.println("Hello world! - java.jsrun.net ");
BinaryTree tree = new BinaryTree();
TreeNode root = tree.newTree();
tree.prevOrder(root);
System.out.println(root.value);
}
}
class TreeNode<T>
{
public T value;
public TreeNode left;
public TreeNode right;
public TreeNode(T value)
{
this.value=value;
}
}
class BinaryTree<T>
{
public TreeNode newTree()
{
TreeNode A = new TreeNode('A');
TreeNode B = new TreeNode('B');
TreeNode C = new TreeNode('C');
TreeNode D = new TreeNode('D');
TreeNode E = new TreeNode('E');
TreeNode F = new TreeNode('F');
TreeNode G = new TreeNode('G');
TreeNode H = new TreeNode('H');
TreeNode I = new TreeNode("I");
TreeNode J = new TreeNode("J");
A.left = B;
A.right = C;
B.left = D;
B.right = E;
C.left = F;
C.right = G;
D.left = I;
D.right = J;
E.right = H;
return A;
}
public void prevOrder(TreeNode root)
{
if(root==null)
{
return;
}
System.out.print(root.value+" ");
prevOrder(root.left);
prevOrder(root.right);
}
public void inOrder(TreeNode root)
{
if(root==null)
{
return;
}
inOrder(root.left);
System.out.print(root.value+" ");
inOrder(root.right);
}
public void postOrder(TreeNode root)
{
if(root==null)
{
return;
}
postOrder(root.left);
postOrder(root.right);
System.out.print(root.value+" ");
}
public int size(TreeNode root)
{
if(root==null)
{
return 0;
}
return 1+size(root.left)+size(root.right);
}
public int levelSize(TreeNode root)
{
if(root==null)
{
return 0;
}
if(root.left==null&&root.right==null)
{
return 1;
}
return levelSize(root.left)+levelSize(root.right);
}
public int getKLevelNodeCount(TreeNode root,int k)
{
if(root==null)
{
return 0;
}
if(k==1)
{
return 1;
}
return getKLevelNodeCount(root.left,k-1)+getKLevelNodeCount(root.right,k-1);
}
public int getHeight(TreeNode root)
{
if(root==null)
{
return 0;
}
int leftH=getHeight(root.left);
int rightH=getHeight(root.right);
return (leftH>=rightH?leftH:rightH)+1;
}
TreeNode find(TreeNode root, T val)
{
if(root==null)
{
return null;
}
if(root.value==val)
{
return root;
}
TreeNode leftNode=find(root.left,val);
if(leftNode!=null)
{
return leftNode;
}
TreeNode rightNode=find(root.right,val);
if(rightNode!=null)
{
return rightNode;
}
return null;
}
public void levelOrder(TreeNode root)
{
Queue<TreeNode> queue=new LinkedList<>();
if(root!=null)
{
queue.offer(root);
}
while(!queue.isEmpty())
{
TreeNode cur=queue.poll();
System.out.print(cur.value+" ");
if(cur.left!=null)
{
queue.offer(cur.left);
}
if(cur.right!=null)
{
queue.offer(cur.right);
}
}
}
}