我正在编写一个java程序,它创建一个非二叉树并实现一个后序遍历。我已经设置了树,但是我仍然停留在如何实现非二叉树的后序上。这个程序使用链表来存储孩子。对于二叉树,这很简单,但我不知道如何迭代和遍历链表,并在它找到具有空子节点的节点后打印
我见过在java中遍历一个非二叉树这不起作用,因为我不能添加方法,也不能像那样迭代链表
/***DO NOT ADD A NEW IMPORT DECLARATION HERE***/
/***DO NOT MAKE ANY CHANGE TO CLASS A5 EXCEPT THE PLACEHOLDER TO FILL IN***/
/***YOU CANNOT ADD A NEW FIELD VARIABLE***/
/***YOU CANNOT ADD A NEW METHOD DECLARATION***/
public class A5 {
public static void main(String[] args) {
//---------------------------------------------------------------------
TreeNode root = new TreeNode(1);
MyGenericLinkedList children = new MyGenericLinkedList();
TreeNode two = new TreeNode(2);
TreeNode three = new TreeNode(3);
TreeNode four = new TreeNode(4);
TreeNode five = new TreeNode(5);
TreeNode six = new TreeNode(6);
TreeNode seven = new TreeNode(7);
TreeNode eight = new TreeNode(8);
TreeNode nine = new TreeNode(9);
TreeNode ten = new TreeNode(10);
TreeNode eleven = new TreeNode(11);
TreeNode twelve = new TreeNode(12);
TreeNode thirteen = new TreeNode(13);
TreeNode fourteen = new TreeNode(14);
children.add(two);
children.add(three);
children.add(four);
root.setChildren(children);
children.remove(0);
children.remove(0);
children.remove(0);
children.add(five);
children.add(six);
two.setChildren(children);
children.remove(0);
children.remove(0);
children.add(ten);
children.add(eleven);
four.setChildren(children);
children.remove(0);
children.remove(0);
children.add(seven);
children.add(eight);
children.add(nine);
six.setChildren(children);
children.remove(0);
children.remove(0);
children.remove(0);
children.add(twelve);
ten.setChildren(children);
children.remove(0);
children.add(thirteen);
children.add(fourteen);
twelve.setChildren(children);
children.remove(0);
children.remove(0);
//---------------------------------------------------------------------
/***DO NOT MAKE ANY CHANGE TO THE FOLLOWING CODE***/
MyGenericTree<Integer> tree = new MyGenericTree<Integer>(root);
tree.traverseInPostOrder();
}
}
/***DO NOT MAKE ANY CHANGE TO CLASS MyGenericTree EXCEPT THE PLACEHOLDER TO FILL IN***/
/***YOU CANNOT ADD A NEW FIELD VARIABLE***/
/***YOU CANNOT ADD A NEW METHOD DECLARATION***/
class MyGenericTree<T> {
private TreeNode<T> root = null;
public MyGenericTree(TreeNode<T> root) {
this.root = root;
}
public void traverseInPostOrder() {
traverseInPostOrder(root);
}
public void traverseInPostOrder(TreeNode<T> node) {
//---------------------------------------------------------------------
//---------------------------------------------------------------------
}
}
/***DO NOT MAKE ANY CHANGE TO CLASS TreeNode***/
class TreeNode<N> {
N data = null;
TreeNode<N> parent = null;
MyGenericLinkedList<TreeNode<N>> children = null;
public TreeNode(N data) {
this.data = data;
}
public void setChildren(MyGenericLinkedList<TreeNode<N>> children) {
this.children = children;
}
}
/***DO NOT MAKE ANY CHANGE TO CLASS MyGenericLinkedList***/
class MyGenericLinkedList<S> {
Node<S> front;
public MyGenericLinkedList() {
front = null;
}
public void add(S value) {
if (front == null) {
front = new Node<S>(value);
} else {
Node<S> current = front;
while (current.next != null) {
current = current.next;
}
current.next = new Node<S>(value);
}
}
public S get(int index) {
Node<S> current = front;
for (int i = 0; i < index; i++) {
current = current.next;
}
return (S)current.data;
}
public void remove(int index) {
if (index == 0) {
front = front.next;
} else {
Node<S> current = front;
for (int i = 0; i < index - 1; i++) {
current = current.next;
}
current.next = current.next.next;
}
}
}
/***DO NOT MAKE ANY CHANGE TO CLASS Node***/
class Node<X> {
X data;
Node<X> next;
public Node(X data) {
this.data = data;
this.next = null;
}
public Node(X data, Node<X> next) {
this.data = data;
this.next = next;
}
}```
暂无答案!
目前还没有任何答案,快来回答吧!