package com.cnu.ds.tree;
import java.util.LinkedList;
import java.util.Queue;
import java.util.Stack;
public class Tree {
public static void main(String[] args) {
TreeNode treeNode = new TreeNode();
treeNode.t = 1;
treeNode.left = new TreeNode();
treeNode.left.t = 2;
treeNode.right = new TreeNode();
treeNode.right.t = 3;
treeNode.left.left = new TreeNode();
treeNode.left.left.t = 4;
treeNode.left.right = new TreeNode();
treeNode.left.right.t = 5;
treeNode.right.left = new TreeNode();
treeNode.right.left.t = 6;
treeNode.right.right = new TreeNode();
treeNode.right.right.t = 7;
// //////////////////////
treeNode.left.left.left = new TreeNode();
treeNode.left.left.left.t = 8;
treeNode.left.left.right = new TreeNode();
treeNode.left.left.right.t = 9;
treeNode.left.right.left = new TreeNode();
treeNode.left.right.left.t = 10;
treeNode.left.right.right = new TreeNode();
treeNode.left.right.right.t = 11;
treeNode.right.left.left = new TreeNode();
treeNode.right.left.left.t = 12;
treeNode.right.left.right = new TreeNode();
treeNode.right.left.right.t = 13;
treeNode.right.right.left = new TreeNode();
treeNode.right.right.left.t = 14;
treeNode.right.right.right = new TreeNode();
treeNode.right.right.right.t = 15;
levelOrder(treeNode);
levelOrderReverse(treeNode);
}
public static void levelOrderReverse(TreeNode root) {
Queue<TreeNode> treeNodes = new LinkedList<>();
TreeNode newRoot = root;
treeNodes.add(root);
treeNodes.add(null);
Stack<Integer> stack = new Stack<>();
Queue<TreeNode> queue = new LinkedList<>();
boolean flip = false;
while (!treeNodes.isEmpty()) {
root = treeNodes.remove();
if (root == null) {
if (flip) {
while (!queue.isEmpty()) {
root = queue.remove();
int r = stack.pop();
int l = stack.pop();
root.left.t = r;
root.right.t = l;
}
}
flip = !flip;
if (treeNodes.isEmpty()) {
break;
}
System.out.println();
treeNodes.add(null);
} else {
if (root.left != null) {
treeNodes.add(root.left);
}
if (null != root.right) {
treeNodes.add(root.right);
}
if (flip) {
stack.push(root.t);
} else {
queue.add(root);
}
}
}
System.out.println();
levelOrder(newRoot);
}
public static void levelOrder(TreeNode root) {
Queue<TreeNode> treeNodes = new LinkedList<>();
treeNodes.add(root);
treeNodes.add(null);
Queue<TreeNode> queue = new LinkedList<>();
while (!treeNodes.isEmpty()) {
root = treeNodes.remove();
if (root == null) {
if (treeNodes.isEmpty()) {
break;
}
System.out.println();
treeNodes.add(null);
} else {
if (root.left != null) {
treeNodes.add(root.left);
}
if (null != root.right) {
treeNodes.add(root.right);
}
queue.add(root);
System.out.print(root.t + " ");
}
}
}
}
输出: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
1 3 2 4 5 6 7 15 14 13 12 11 10 9 8
以上是网站中建议的以下问题的代码段的一部分。 反转二叉树的备用级节点。 一个 / \ b c / \ / \ d e f g / \ / \ / \ / \ 我是谁?
修改树: 一个 / \ c b / \ / \ d e f g / \ / \ / \ / \ o n m l k j i h
最初我收到错误,因为“TreeNode无法解析为类型” 然后我导入java.swing后得到错误“无法实例化类型TreeNode”
请建议
答案 0 :(得分:0)
您无法TreeNode
new
实例化TreeNode
,因为TreeNode node = new SomeClass();
是interface。但是你可以说:
SomeClass
其中TreeNode
是实现{{1}}接口的类。
答案 1 :(得分:0)
TreeNode是一个接口,接口永远不能使用new运算符或关键字进行实例化。您可以使用TreeNode作为对象引用或句柄,但是您必须使用一些具有new关键字的实现类来创建实例。访问here以了解有关TreeNode及其实现类的更多信息。
答案 2 :(得分:0)
您无法实例化interface
。TreeNode
是一个只能实现或引用任何实现TreeNode
的类的接口。
答案 3 :(得分:0)
像这样尝试org.primefaces.model.DefaultTreeNode:
TreeNode root = new DefaultTreeNode("SomeID", null);