Ordinary binary trees can be created in two ways, one is based on array storage, and the other is based on pre-order traversal.
1. Array-based.
Default: If the element of the array appears '0' [string], it means that the node does not exist.
Suppose the contents of the array are: 1 2 3 4 0 5 6 7 8 0 0 9 10
The shape of the tree is:
1
/ \
2 3
/ / \
4 5 6
/ \ / \
7 8 9 10
Node.java:
public class Node {
Node left;
Node right;
char data;
public Node(char data) {
left = null;
right = null;
this.data = data;
}
public char getData() {
return data;
}
public Node getLeft() {
return left;
}
public Node getRight() {
return right;
}
public void setLeft(Node left) {
this.left = left;
}
public void setRight(Node right) {
this.right = right;
}
public void setData(char data) {
this.data = data;
}
}
It's very simple, that is, the get and set methods are defined
Tree.java:
public class Tree {
Node root; // 根节点
int size; // 树长度
char[] data; // 数的数据
public Tree(char[] data) {
this.data = data;
size = data.length;
root = createTree(0);
}
public Node createTree(int index) { // 采用递归生成二叉树
if (index >= size)
return null;
if (data[index] == '0')
return null;
Node node = new Node(data[index]);
node.setLeft(createTree(2 * index + 1));
node.setRight(createTree(2 * index + 2));
return node;
}
public void preShow(Node node) { // 先序遍历
if (node != null) {
System.out.print(node.getData() + " ");
preShow(node.getLeft());
preShow(node.getRight());
}
}
public void middleShow(Node node) { // 中序遍历
if (node != null) {
middleShow(node.getLeft());
System.out.print(node.getData() + " ");
middleShow(node.getRight());
}
}
public void backShow(Node node) { // 后序遍历
if (node != null) {
backShow(node.getLeft());
backShow(node.getRight());
System.out.print(node.getData() + " ");
}
}
public Node getRoot() { // 得到根节点
return root;
}
}
The focus is on the createTree(int index) function.
MainClass.java:
public class MainClass {
public static void main(String[] args) {
char[] chars = new char[] {'1', '2', '3', '4', '0', '5', '6', '7', '8', '0', '0', '9', 'A'};
Tree tree = new Tree(chars);
System.out.println("先序遍历");
tree.preShow(tree.getRoot());
System.out.println();
System.out.println("中序遍历");
tree.middleShow(tree.getRoot());
System.out.println();
System.out.println("后序遍历");
tree.backShow(tree.getRoot());
System.out.println();
}
}
operation result:
先序遍历
1 2 4 7 8 3 5 9 A 6
中序遍历
7 4 8 2 1 9 5 A 3 6
后序遍历
7 8 4 2 9 A 5 6 3 1
2. Based on the pre-order traversal to build
Node.java unchanged.
Tree.java:
public class Tree {
Node root; // 根节点
int size; // 树长度
String data; // 数的数据
int index;
public Tree(String data) {
this.data = data;
size = data.length();
index = 0;
root = createTree();
}
public Node createTree() { // 采用递归生成二叉树
char ch = data.charAt(index ++);
if(ch == '0')
return null;
else {
Node node = new Node(ch);
node.setLeft(createTree());
node.setRight(createTree());
return node;
}
}
public void preShow(Node node) { // 先序遍历
if (node != null) {
System.out.print(node.getData() + " ");
preShow(node.getLeft());
preShow(node.getRight());
}
}
public void middleShow(Node node) { // 中序遍历
if (node != null) {
middleShow(node.getLeft());
System.out.print(node.getData() + " ");
middleShow(node.getRight());
}
}
public void backShow(Node node) {
if (node != null) {
backShow(node.getLeft());
backShow(node.getRight());
System.out.print(node.getData() + " ");
}
}
public Node getRoot() {
return root;
}
}
MainClass.java:
public class MainClass {
public static void main(String[] args) {
String data = "AB0C00D00";
Tree tree = new Tree(data);
System.out.println("先序遍历");
tree.preShow(tree.getRoot());
System.out.println();
System.out.println("中序遍历");
tree.middleShow(tree.getRoot());
System.out.println();
System.out.println("后序遍历");
tree.backShow(tree.getRoot());
System.out.println();
}
}
operation result:
先序遍历
A B C D
中序遍历
B C A D
后序遍历
C B D A