您的位置:首页 > 编程语言 > Java开发

FWNX-4,.5 JAVA VERSION - SUCCESS OF IN_ORDER_TRAVERSE

2013-12-29 23:38 281 查看
4.5 Write an algorithm to find the NEXT?node (i.e., in-order successor) of a given node in a binary search tree where each node has a link to its parent.

package test_kepler;

public class GandT extends Treeandgraph {
public GandT()
{
rebuild();
}
void in_order(Node root)
{
if(root.left_child!=null)
{
in_order(root.left_child);
}
if(root!=null)
{
root.print();
}
if(root.right_child!=null)
{
in_order(root.right_child);
}
}

void pre_order(Node root)
{
if(root!=null)
{
root.print();
}
if(root.left_child!=null)
{
pre_order(root.left_child);
}
if(root.right_child!=null)
{
//root.right_child.print();
pre_order(root.right_child);
}
}
void after_order(Node root)
{
if(root.left_child!=null)
{
after_order(root.left_child);
}
if(root.right_child!=null)
{
after_order(root.right_child);
}
if(root!=null)
{
root.print();
}
}
public Node thisRoot;
public Node [] proot;
void rebuild()
{
Node [] t = new Node[10];
proot=t;
for(int i = 0;i<10;++i)
{
t[i] = new Node();
}
t[0].setLeftChild(t[1]);
t[0].setRightChild(t[2]);

t[1].setLeftChild(t[3]);

t[2].setRightChild(t[4]);
t[4].setLeftChild(t[5]);
t[4].setRightChild(t[6]);
t[5].setLeftChild(t[7]);
t[5].setRightChild(t[8]);
t[6].setRightChild(t[9]);

thisRoot = t[0];
}
Node getparentleft(Node find)
{
Node it = find;
while(it.parent!=null)
{
if(it.parent.left_child == it)
{
return it.parent;
}
it = it.parent;
}
return null;
}
Node getleftmost(Node find)
{
Node it = find;
while(it.left_child!=null)
{
it=it.left_child;
}
return it;
}
int test()
{
//Node temp = getParentWithLeftChild(proot[8]);
// System.out.println(temp.thisid);
Node tem1 = getleftmost(proot[2].right_child);
System.out.println(tem1.thisid);

System.out.println("main");
for(int i = 0;i<10;++i)
{
Node kk = getInOrderSucc(proot[i]);
//kk.print();
if(kk == null)
{
System.out.println(proot[i].thisid+"'s success is "+ " ~end~ ");
}
else
{
System.out.println(proot[i].thisid+"'s success is "+kk.thisid);

}
}
for(int i = 0;i<10;++i)
{
proot[i].print();
}
// return temp.thisid;
return 0;
}
Node getInOrderSucc(Node root)
{
if(root==null)
{
System.out.println("null p");
return null;
}
// if root does not have any children;
if(root.left_child == null && root.right_child == null)
{
if(root.parent.left_child == root)
{
System.out.println("meet");
return root.parent;
}
else
{
if(root.parent.right_child == root)
{
return getparentleft(root);
}
}
}
//if root have one or more children
if(root.left_child != null)
{
if(root.right_child!=null)
{
return root.right_child;
}
else// care about this when it doese not have right child but have left child; then return to its parent
{
return root.parent;
}
}
else
{
if(root.left_child == null && root.right_child!=null)
{
return getleftmost(root.right_child);
}
else
{
System.out.println("meet");
return null;
}
}

}
public static void main(String[] args) {
// TODO Auto-generated method stub
GandT gt = new GandT();
System.out.println("in order");
gt.in_order(gt.thisRoot);
System.out.println("pre_order");
gt.pre_order(gt.thisRoot);
System.out.println("after_order");
gt.after_order(gt.thisRoot);
gt.test();
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐