亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

二叉樹插入問題.....insert() 只將新節點放入根節點的子節點中

二叉樹插入問題.....insert() 只將新節點放入根節點的子節點中

明月笑刀無情 2023-06-21 16:37:30
當我用來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");    }}
查看完整描述

2 回答

?
萬千封印

TA貢獻1891條經驗 獲得超3個贊

您永遠不會更改lc和rc鏈接。嘗試這樣的事情:


        if(newnode.key <= n.key)

        { 

            if(n.lc==null){n.lc = newnode; break;}

            n = n.lc;

            System.out.println("left");

        }

        else

        { 

            if(n.rc==null){n.rc = newnode; break;}

            n = n.rc;

            System.out.println("right");

         } 


查看完整回答
反對 回復 2023-06-21
?
qq_花開花謝_0

TA貢獻1835條經驗 獲得超7個贊

如果使用遞歸呢?認識起來就更清楚了。


public final class BinaryTree {


    private static final boolean ADD_TO_PARENT = true;

    private static final boolean ALREADY_ADDED = false; 


    private Node root;


    public void add(int key, String value) {

        Node node = new Node(key, value);


        if (add(node, root) == ADD_TO_PARENT)

            root = node;

    }


    private static boolean add(Node node, Node parent) {

        if (parent == null)

            return ADD_TO_PARENT;

        if (node.key <= parent.key) {

            if (add(node, parent.left) == ADD_TO_PARENT)

                parent.left = node;

        } else if (add(node, parent.right) == ADD_TO_PARENT)

            parent.right = node;


        return ALREADY_ADDED;

    }


    public static final class Node {


        private final int key;

        private final String value;

        private Node left;

        private Node right;


        public Node(int key, String value) {

            this.key = key;

            this.value = value;

        }


        @Override

        public String toString() {

            return value + " is " + key;

        }

    }


}


查看完整回答
反對 回復 2023-06-21
  • 2 回答
  • 0 關注
  • 184 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號