简体   繁体   English

递归二叉树插入

[英]Recursive binary tree insert

When I added node into binary tree and trying to show information about it, info isn't showing. 当我将节点添加到二叉树中并尝试显示有关它的信息时,信息未显示。 I think that i have some problems with references in recursive insert algorithm, but can't fix it. 我认为我在递归插入算法中有一些参考问题,但无法修复它。

package test;

class BinaryTree<T> {
    private static class Node<T> {
        int key;
        T data;
        Node<T> leftChild;
        Node<T> rightChild;

        public Node(int key,T data) {
            this.key = key;
            this.data = data;
        }
    }

    public Node<T> rootNode;

    public BinaryTree() {
        rootNode = null;
    }

    public Node<T> getRootNode() {
        return rootNode;
    }

    // insert node into binary tree
    public void insertNode(int key,T data, Node<T> rootNode) {
        // to create new node

        // if tree doesn't have root elements
        if(rootNode == null) {
            rootNode = new Node<T>(key,data);
            rootNode.leftChild = null;
            rootNode.rightChild = null;
        }
        else {
            Node<T> focusNode = rootNode;

            if(key >= focusNode.key) {
                insertNode(key,data,focusNode.rightChild);
            }
            else {
                insertNode(key,data,focusNode.leftChild);
            }
        }
    }

    // inorder traverse tree
    public void inOrderTraverseTree(Node<T> focusNode) {
        if(focusNode != null) {
            inOrderTraverseTree(focusNode.leftChild);
            System.out.println(focusNode.data);
            inOrderTraverseTree(focusNode.rightChild);
        }
    }
}

public class MyApp {
    public static void main(String[] args) {
        BinaryTree<String> bintree = new BinaryTree<String>();
        bintree.insertNode(3, "Boss", bintree.rootNode);
        bintree.inOrderTraverseTree(bintree.rootNode);
    }
}

If I'm adding node with this algorithm and trying to show info, it works. 如果我使用此算法添加节点并尝试显示信息,则可以正常工作。 How can i fix problem with recursive algorithm? 如何解决递归算法的问题?

public void addNode(int key, T name) {
        Node<T> newNode = new Node<T>(key,name);
        if(rootNode == null) {
            rootNode = newNode;
        }
        else {
            Node<T> focusNode = rootNode;
            Node<T> parent;
            while(true) {
                parent = focusNode;
                if(key < focusNode.key) {
                    focusNode = focusNode.leftChild;
                    if(focusNode == null) {
                        parent.leftChild = newNode;
                        return;
                    }
                }
                else {
                    focusNode = focusNode.rightChild;
                    if(focusNode == null) {
                        parent.rightChild = newNode;
                        return;
                    }
                }
            }
        }
    }

Thanks for any help. 谢谢你的帮助。

Quick glance on your code -- I spotted on this part where you're checking for null, rootNode variable is local to the function . 快速浏览一下代码 - 我发现你正在检查null的那一部分,rootNode变量是函数的本地变量。 Hence the new Node you created gets thrown away immediately after the function quits, it won't change your member field 因此,您创建的新节点在函数退出后立即被丢弃,它不会更改您的成员字段

    // if tree doesn't have root elements
    if(rootNode == null) {
        rootNode = new Node<T>(key,data);
        rootNode.leftChild = null;
        rootNode.rightChild = null;
    }

You need to use this.rootNode = new Node<T>(key,data); 你需要使用this.rootNode = new Node<T>(key,data); instead, or use a different local variable name to avoid confusion 相反,或使用不同的局部变量名称以避免混淆

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM