package BinaryTree;
public class main {
public static void main(String[] args) {
Node<String> nodes = initTree();
visit(nodes);
System.out.println("=======================");
invert(nodes);
visit(nodes);
}
private static void invert(Node<String> node) {
if (node.leftTree != null || node.rightTree != null) {
Node<String> temp = node.leftTree;
node.leftTree = node.rightTree;
node.rightTree = temp;
invert(node.leftTree);
invert(node.rightTree);
}
}
private static void visit(Node<String> node) {
if (node != null) {
visit(node.leftTree);
System.out.println(node.data);
visit(node.rightTree);
}
}
private static Node<String> initTree() {
Node<String> nodes = new Node<String>("shun");
Node<String> node1 = new Node<String>("shun1");
Node<String> node2 = new Node<String>("shun2");
nodes.leftTree = node1;
nodes.rightTree = node2;
node1.leftTree = new Node<String>("shun11");
node1.rightTree = new Node<String>("shun12");
node2.leftTree = new Node<String>("shun21");
node2.rightTree = new Node<String>("shun22");
return nodes;
}
}
package BinaryTree;
public class Node<T> {
public T data;
public Node<T> leftTree;
public Node<T> rightTree;
Node(T data){
this.data = data;
this.leftTree = null;
this.rightTree = null;
}
}