package Tree;
public class SearchForNext {
/**
* 树的节点有指向父节点,寻找下一个中序遍历的节点
*/
public static NodeWithParent searchForNext(NodeWithParent now){
if(now.rchild != null) {
NodeWithParent tmp = now.rchild;
while(tmp.lchild != null){
tmp = tmp.lchild;
}
return tmp;
}
while(now.parent != null && now.parent.rchild == now){
now = now.parent;
}
return now.parent;
}
public static void printNode(NodeWithParent node){
if(node != null){
System.out.println(node.value);
}else{
System.out.println("该节点是空节点!");
}
}
public static void main(String[] args){
NodeWithParent node1 = new NodeWithParent(1);
NodeWithParent node2 = new NodeWithParent(2);
NodeWithParent node3 = n
二叉树的节点包含指向父节点的指针,给出树中一个元素,求出中序遍历的下一个元素
最新推荐文章于 2022-03-10 22:44:21 发布
package Tree;public class SearchForNext { /** * 树的节点有指向父节点,寻找下一个中序遍历的节点 */ public static NodeWithParent searchForNext(NodeWithParent now){ if(now.rchild != null) { ...
摘要由CSDN通过智能技术生成