הפעולות בעצים
public static void preOrder(BinNode<Integer> t){
if (t != null){
System.out.print(t.getValue()+",");
preOrder(t.getLeft());
preOrder(t.getRight());
}
}
public static void inOrder(BinNode<Integer> t){
if (t != null){
inOrder(t.getLeft());
System.out.print(t.getValue()+",");
inOrder(t.getRight());
}
}
public static void postOrder(BinNode<Integer> t){
if (t != null){
postOrder(t.getLeft());
postOrder(t.getRight());
System.out.print(t.getValue()+",");
}
}
public static <T> int countNodesInTree(BinNode <T> t){
if (t == null)
return 0;
else
return 1 + countNodesInTree(t.getLeft()) + countNodesInTree(t.getRight());
}
public static <T> int binTreeH(BinNode <T> t){
if (t == null){
return -1;
}
else{
int x = binTreeH(t.getLeft());
int y = binTreeH(t.getRight());
return Math.max(x,y)+1;
}
}
public static void insertToSrtedTree(BinNode<Integer>t,int num){
if (num < t.getValue()){
if (t.getLeft() == null){
BinNode<Integer>child = new BinNode<Integer>(num);
t.setLeft(child);
}
else{
insertToSrtedTree(t.getLeft(), num);
}
}
else{
if (t.getRight() == null){
BinNode<Integer>child = new BinNode<Integer>(num);
t.setRight(child);
}
else{
insertToSrtedTree(t.getRight(), num);
}
}
}
public static <T> boolean symetry(BinNode <T> t){
if (t == null)
return true;
else{
int x = binTreeH(t.getLeft());
int y = binTreeH(t.getRight());
return ((Math.abs(x–y) <2) && symetry(t.getLeft()) && symetry(t.getRight()));
}
}