将树转换为其 preOrder 数组(递归地)
Convert tree to its preOrder Array (recursively)
我正在尝试将树转换为其预序数组,例如,如果树是这样的:
1
/ \
2 3
________
那么它的 preOrder 数组应该是 | 1 | 2 | 3 |
- - - - - - - -
这里有一些树输入:
// 2 3 4 5 6 7 8 -1 -1 -1 -1 -1 -1 -1 -1
// 10 9 4 -1 -1 5 8 -1 6 -1 -1 3 -1 -1 -1
// 1 2 9 3 4 -1 10 5 -1 -1 5 -1 -1 -1 -1 -1 6 -1 7 -1 8 -1 -1
// 1 2 6 3 7 -1 -1 4 -1 -1 8 5 -1 -1 9 -1 -1 -1 10 -1 -1
// 1 2 3 -1 -1 -1 -1
我的代码 Main.java :
public class Main {
public static void main(String[] args) throws QueueEmptyException {
Scanner in = new Scanner(System.in);
BinaryTreeNode<Integer> root = BinaryTreeNode.takeInput_LEVEL_WISE(in); // tree input taken
BinaryTreeNode.print_Binary_Tree_LEVEL_WISE(root);
// create its preOrder array
int pre[] = BinaryTreeNode.preOrder_Array(root);
for (int val : pre) {
System.out.print(val + " ");
}
// create its postOrder array
}
}
这是我的树节点:
public class BinaryTreeNode<T> {
public T data;
public BinaryTreeNode<T> left;
public BinaryTreeNode<T> right;
BinaryTreeNode(T data) {
this.data = data;
left = null;
right = null;
}
}
取输入法:(这个没问题)
public static BinaryTreeNode<Integer> takeInput_LEVEL_WISE(Scanner in) {
Queue<BinaryTreeNode<Integer>> q = new LinkedList<>();
System.out.println("enter root ");
int data = in.nextInt();
if (data == -1) // no root is formed
return null;
BinaryTreeNode<Integer> root = new BinaryTreeNode<>(data);
q.add(root);
int left, right;
while (!q.isEmpty()) {
BinaryTreeNode<Integer> currentRoot = q.poll();
System.out.println("enter left of " + currentRoot.data + " : ");
left = in.nextInt();
if (left == -1) {
currentRoot.left = null;
} else {
BinaryTreeNode<Integer> leftChild = new BinaryTreeNode<>(left);
currentRoot.left = leftChild;
q.add(leftChild);
}
System.out.println("enter right of " + currentRoot.data + " : ");
right = in.nextInt();
if (right == -1) {
currentRoot.right = null;
} else {
BinaryTreeNode<Integer> rightChild = new BinaryTreeNode<>(right);
currentRoot.right = rightChild;
q.add(rightChild);
}
}
return root;
}
打印函数的代码:(这个也没有问题)
public static void print_Binary_Tree_LEVEL_WISE(BinaryTreeNode<Integer> root) {
Queue<BinaryTreeNode<Integer>> q = new LinkedList<>();
q.add(root);
String print = "";
while (q.size() != 0) { // until not empty
BinaryTreeNode<Integer> currentRoot = q.poll();
print = currentRoot.data + ":";
// adding the right and left
if (currentRoot.left != null) {
q.add(currentRoot.left);
print += "L:" + currentRoot.left.data + ",";
} else {
print += "L:" + -1 + ",";
}
if (currentRoot.right != null) {
q.add(currentRoot.right);
print += "R:" + currentRoot.right.data;
} else {
print += "R:" + -1;
}
System.out.println(print);
}
}
这是 PreOrder 数组的代码:(在此中遇到问题)
public static int[] preOrder_Array(BinaryTreeNode<Integer> root) {
if (root == null)
return new int[0]; // array of 0 size
int n = noOfNodesIN_Tree(root);
int pre[] = new int[n];
preOrder_Array_Helper(pre, root, 0);
return pre;
}
private static void preOrder_Array_Helper(int[] pre, BinaryTreeNode<Integer> root, int index) {
if (root == null)
return; //-> base case
pre[index] = root.data;
// index++;
if (index == pre.length) {
return;
} else { // call for recursion
preOrder_Array_Helper(pre, root.left, index + 1);
preOrder_Array_Helper(pre, root.right, index + 1);
}
}
我理解这个问题,这是由于递归中的索引(值不断覆盖相同location/index),但我不知道如何解决这个问题,我该如何制作索引增量。
这可以通过 arrayList 轻松完成,但我想用数组来完成。
您可以 return 最后更新的索引作为方法的结果并在递归调用中使用它:
private static int preOrder_Array_Helper(int[] pre, BinaryTreeNode<Integer> root, int index) {
if (root == null)
return index; //return the same as get
pre[index] = root.data;
index++;
if (index == pre.length) {
return index; //return new value after add
} else {
index = preOrder_Array_Helper(pre, root.left, index); //get last after left branch visit
return preOrder_Array_Helper(pre, root.right, index); //use new index in right branch
}
}
或者您可以使用 List<Integer>
来避免所有这些索引管理问题:
public static List<Integer> preOrder_Array(BinaryTreeNode<Integer> root) {
if (root == null)
return new ArrayList<>(); // array of 0 size
List<Integer> pre = new ArrayList<>();
preOrder_Array_Helper(pre, root);
return pre;
}
private static void preOrder_Array_Helper(List<Integer> pre, BinaryTreeNode<Integer> root) {
if (root == null)
return;
pre.add(root.data);
preOrder_Array_Helper(pre, root.left);
preOrder_Array_Helper(pre, root.right);
}
我正在尝试将树转换为其预序数组,例如,如果树是这样的:
1
/ \
2 3
________
那么它的 preOrder 数组应该是 | 1 | 2 | 3 |
- - - - - - - -
这里有一些树输入:
// 2 3 4 5 6 7 8 -1 -1 -1 -1 -1 -1 -1 -1
// 10 9 4 -1 -1 5 8 -1 6 -1 -1 3 -1 -1 -1
// 1 2 9 3 4 -1 10 5 -1 -1 5 -1 -1 -1 -1 -1 6 -1 7 -1 8 -1 -1
// 1 2 6 3 7 -1 -1 4 -1 -1 8 5 -1 -1 9 -1 -1 -1 10 -1 -1
// 1 2 3 -1 -1 -1 -1
我的代码 Main.java :
public class Main {
public static void main(String[] args) throws QueueEmptyException {
Scanner in = new Scanner(System.in);
BinaryTreeNode<Integer> root = BinaryTreeNode.takeInput_LEVEL_WISE(in); // tree input taken
BinaryTreeNode.print_Binary_Tree_LEVEL_WISE(root);
// create its preOrder array
int pre[] = BinaryTreeNode.preOrder_Array(root);
for (int val : pre) {
System.out.print(val + " ");
}
// create its postOrder array
}
}
这是我的树节点:
public class BinaryTreeNode<T> {
public T data;
public BinaryTreeNode<T> left;
public BinaryTreeNode<T> right;
BinaryTreeNode(T data) {
this.data = data;
left = null;
right = null;
}
}
取输入法:(这个没问题)
public static BinaryTreeNode<Integer> takeInput_LEVEL_WISE(Scanner in) {
Queue<BinaryTreeNode<Integer>> q = new LinkedList<>();
System.out.println("enter root ");
int data = in.nextInt();
if (data == -1) // no root is formed
return null;
BinaryTreeNode<Integer> root = new BinaryTreeNode<>(data);
q.add(root);
int left, right;
while (!q.isEmpty()) {
BinaryTreeNode<Integer> currentRoot = q.poll();
System.out.println("enter left of " + currentRoot.data + " : ");
left = in.nextInt();
if (left == -1) {
currentRoot.left = null;
} else {
BinaryTreeNode<Integer> leftChild = new BinaryTreeNode<>(left);
currentRoot.left = leftChild;
q.add(leftChild);
}
System.out.println("enter right of " + currentRoot.data + " : ");
right = in.nextInt();
if (right == -1) {
currentRoot.right = null;
} else {
BinaryTreeNode<Integer> rightChild = new BinaryTreeNode<>(right);
currentRoot.right = rightChild;
q.add(rightChild);
}
}
return root;
}
打印函数的代码:(这个也没有问题)
public static void print_Binary_Tree_LEVEL_WISE(BinaryTreeNode<Integer> root) {
Queue<BinaryTreeNode<Integer>> q = new LinkedList<>();
q.add(root);
String print = "";
while (q.size() != 0) { // until not empty
BinaryTreeNode<Integer> currentRoot = q.poll();
print = currentRoot.data + ":";
// adding the right and left
if (currentRoot.left != null) {
q.add(currentRoot.left);
print += "L:" + currentRoot.left.data + ",";
} else {
print += "L:" + -1 + ",";
}
if (currentRoot.right != null) {
q.add(currentRoot.right);
print += "R:" + currentRoot.right.data;
} else {
print += "R:" + -1;
}
System.out.println(print);
}
}
这是 PreOrder 数组的代码:(在此中遇到问题)
public static int[] preOrder_Array(BinaryTreeNode<Integer> root) {
if (root == null)
return new int[0]; // array of 0 size
int n = noOfNodesIN_Tree(root);
int pre[] = new int[n];
preOrder_Array_Helper(pre, root, 0);
return pre;
}
private static void preOrder_Array_Helper(int[] pre, BinaryTreeNode<Integer> root, int index) {
if (root == null)
return; //-> base case
pre[index] = root.data;
// index++;
if (index == pre.length) {
return;
} else { // call for recursion
preOrder_Array_Helper(pre, root.left, index + 1);
preOrder_Array_Helper(pre, root.right, index + 1);
}
}
我理解这个问题,这是由于递归中的索引(值不断覆盖相同location/index),但我不知道如何解决这个问题,我该如何制作索引增量。
这可以通过 arrayList 轻松完成,但我想用数组来完成。
您可以 return 最后更新的索引作为方法的结果并在递归调用中使用它:
private static int preOrder_Array_Helper(int[] pre, BinaryTreeNode<Integer> root, int index) {
if (root == null)
return index; //return the same as get
pre[index] = root.data;
index++;
if (index == pre.length) {
return index; //return new value after add
} else {
index = preOrder_Array_Helper(pre, root.left, index); //get last after left branch visit
return preOrder_Array_Helper(pre, root.right, index); //use new index in right branch
}
}
或者您可以使用 List<Integer>
来避免所有这些索引管理问题:
public static List<Integer> preOrder_Array(BinaryTreeNode<Integer> root) {
if (root == null)
return new ArrayList<>(); // array of 0 size
List<Integer> pre = new ArrayList<>();
preOrder_Array_Helper(pre, root);
return pre;
}
private static void preOrder_Array_Helper(List<Integer> pre, BinaryTreeNode<Integer> root) {
if (root == null)
return;
pre.add(root.data);
preOrder_Array_Helper(pre, root.left);
preOrder_Array_Helper(pre, root.right);
}