通常のバイナリツリーを作成するには、2つの方法があります。1つは配列ストレージに基づいており、もう1つは事前注文トラバーサルに基づいています。
1.アレイベース。
デフォルト:配列の要素が「0」[文字列]と表示されている場合は、ノードが存在しないことを意味します。
配列の内容が次のようになっているとします。1234 0 5 6 7 8 0 0
910ツリーの形状は次のとおりです。
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;
}
}
非常に簡単です。つまり、getメソッドとsetメソッドが定義されています。
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;
}
}
焦点はcreateTree(int index)関数にあります。
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();
}
}
演算結果:
先序遍历
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.Node.javaを変更せずにビルドするための事前注文トラバーサルに基づきます。
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();
}
}
演算結果:
先序遍历
A B C D
中序遍历
B C A D
后序遍历
C B D A