简体   繁体   English

二叉搜索树中的插入方法

[英]Insert method in Binary Search Tree

when I insert nodes into the binary tree and go test it out in the main by printing it in order the output is always wrong I have tried other solutions but it just keeps getting into bigger problems and in the output, it will be displayed in the wrong order I check the display its perfect but the problem is with the insert method (insert method insert non repeated keys).当我将节点插入二叉树并 go 主要通过按顺序打印来测试它时 output 总是错误的错误的顺序我检查了它的完美显示,但问题在于插入方法(插入方法插入非重复键)。

class BSTNode<T> {
    public int key;
    public T data;
    public BSTNode<T> left, right;

    
    public BSTNode(int k, T val) {
        key = k;
        data = val;
        left = right = null;
    }

    public BSTNode(int k, T val, BSTNode<T> l, BSTNode<T> r) {
        key = k;
        data = val;
        left = l;
        right = r;
    }
}

public class BST<T> {
    BSTNode<T> root, current;

    public BST() {
        root = current = null;
    }

    public boolean empty() {
        return root == null;
    }

    public boolean full() {
        return false;
    }

    public T retrieve() {
        return current.data;
    }

    public boolean findkey(int tkey) {
        BSTNode<T> p = root, q = root;

        if (empty())
            return false;
        int nb = 0;
        while (p != null) {
            q = p;
            nb++;
            if (p.key == tkey) {
                current = p;
                return true;
            } else if (tkey < p.key)
                p = p.left;
            else
                p = p.right;
        }

        current = q;
        return false;
    }
    
    public boolean insert(int key, T val) {
        BSTNode<T> p = current, q = current;

        while (p != null) {
            q = p;
            if (p.key == key) {
                return false;
            } else if (key < p.key)
                p = p.left;
            else
                p = p.right;
        }

        p = new BSTNode<T>(key, val);
        if (empty()) {
            root = current = p;
            return true;
        } else {
            // current is pointing to parent of the new key
            if (key < current.key)
                current.left = p;
            else
                current.right = p;
            current = p;
            return true;
        }
    }

    public void display() {
        if (root == null)
            System.out.println("BST IS EMPTY");
        else
            displayin(root);
        System.out.println();
    }

    private void displayin(BSTNode<T> p) {
        if (p != null) {
            displayin(p.left);
            System.out.print(p.key + " " + p.data + " ,  ");
            displayin(p.right);
        }
    }
}

In your insert method, the first code part:在您的insert方法中,第一个代码部分:

        while (p != null) {
            q = p;
            if (p.key == key) {
                current = p;
                return true;
            } else if (key < p.key)
                p = p.left;
            else
                p = p.right;
        }

simply returns true when the key you want to write to is found.当找到要写入的密钥时,只需返回true It does however not insert val here at all.但是,它根本不会在此处插入val You could have detected this by calling the insert method on some tree and then printing that tree, comparing the outcome that you had expected with the actual outcome.您可以通过在某棵树上调用 insert 方法然后打印该树并将您预期的结果与实际结果进行比较来检测到这一点。 In automated form, you could do this with JUnit tests.在自动形式中,您可以使用 JUnit 测试来执行此操作。

I think I know want you want (now).我想我知道你想要(现在)。 I have changed the "display"-Method to toString, rewrote insert and implemented a form of bfs for toString.我已将“display”-Method 更改为 toString,重写了 insert 并为 toString 实现了一种bfs形式。

import java.util.ArrayList;

class BSTNode<T> {

    public int key;
    public T data;
    public BSTNode<T> left, right;

    public BSTNode(int k, T val) {
        key = k;
        data = val;
        left = right = null;
    }

    public BSTNode(int k, T val, BSTNode<T> l, BSTNode<T> r) {
        key = k;
        data = val;
        left = l;
        right = r;
    }

    @Override
    public String toString() {
        return "[" + key + ":" + data + "]";
    }
}

public class BST<T> {

    BSTNode<T> root, current;

    public BST() {
        root = current = null;
    }

    public boolean empty() {
        return root == null;
    }

    public T retrieve() {
        return current.data;
    }

    public boolean findkey(int tkey) {
        BSTNode<T> current = root;
        while (current != null) {
            if (current.key == tkey)
                return true;
            if (tkey < current.key)
                current = current.left;
            else
                current = current.right;
        }
        return false;
    }
    
    public boolean insert(int key, T val) {
        BSTNode<T> node = new BSTNode<>(key, val);
        if (root == null) {
            root = node;
        } else {
            BSTNode<T> current = root;
            while (current != null) {
                if (key == current.key)
                    return false;
                if (key < current.key) {
                    if (current.left == null) {
                        current.left = node;
                        return true;
                    }
                    current = current.left;
                } else {
                    if (current.right == null) {
                        current.right = node;
                        return true;
                    }
                    current = current.right;
                }
            }
        }
        return false;
    }

    public String toString() {
        if (root == null)
            return "BST IS EMPTY";
        ArrayList<BSTNode<T>> visited = new ArrayList<>();
        ArrayList<BSTNode<T>> queue = new ArrayList<>();
        queue.add(root);
        while (!queue.isEmpty()) {
            BSTNode<T> vertex = queue.remove(0);
            if (vertex != null) {
                queue.add(vertex.left);
                queue.add(vertex.right);
            }
            visited.add(vertex);
        }
        return visited.toString();
    }
}

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

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