当我用来insert()将新节点插入二叉树时,即使根节点已经有左右子节点,它也只会在子节点的位置插入新节点。它不是访问子节点来使二叉树更深层次。
抱歉英语不好。
class Node
{
int key;
String value;
Node lc = null;
Node rc = null;
Node(int k,String v)
{
key = k;
value = v;
}
public String toString()
{
return value + "is" + key;
}
}
class BT
{
Node root;
public void insert(int k,String v)
{
Node newnode = new Node(k,v);
if(root == null)
{
System.out.println("root");
root = newnode;
return;
}
Node n = root;
while(n != null)
{
if(newnode.key <= n.key)
{
n = n.lc;
System.out.println("left");
if(n==null){n = newnode; break;}
}
else
{
n = n.rc;
System.out.println("right");
if(n==null){n = newnode; break;}
}
}
System.out.println("loop ended");
return;
}
}
public class test
{
public static void main(String arg[])
{
BT list = new BT();
list.insert(19,"one");
list.insert(67,"sixtyseven");
list.insert(5,"five");
list.insert(12,"twelve");
list.insert(67,"sixtyseven");
}
}
万千封印
qq_花开花谢_0
相关分类