Binary Search Tree-> Inorder遍历方法不打印插入的值

Binary Search Tree-> Inorder traversal method not printing the value inserted

这是二叉搜索树删除的代码,当我尝试在树中插入元素并打印时,出现的值为零。我尝试使用调试技术,错误似乎来自 "void insert()" 方法,因为 root.key 元素不打印用户从 inorderRec() method 插入的元素。我还在学习树DS。提前谢谢大家。

节点class:

class Node { 

    int key; //elements
    Node left, right; //positions

    //constructor
    public Node(int item) {
        item = key;
        left = right = null;
    }
}

英国夏令时 CLASS:

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package binarysearchdeletion;

/**
 *
 * @author Srt
 */
 class BinarySearchDeletion {

     Node root;

    public BinarySearchDeletion() {
        root = null;
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // TODO code application logic here

        BinarySearchDeletion tree = new BinarySearchDeletion();


        System.out.println("print the statement");
         tree.insert(50);
         tree.inorder();

    }



    //delete method,, to delete keys
    void delete(int key) {
        root = deleteRec(root, key);
    }
    //delete recursion method

    Node deleteRec(Node root, int key) {
        //if tree is empty return the root
        if (root == null) {
            return root;
        }

        //deleteing the leaf node based on the input
        if (key < root.key) {
            root.left = deleteRec(root.left, key);
        } else if (key > root.key) {
            root.right = deleteRec(root.right, key);
        } //deleting node when the parent has only one child.
         else {
            // node with only one child or no child
            if (root.left == null) {
                return root.right;
            } else if (root.right == null) {
                return root.left;
            }

            //deleting node when the parent has two children(inorder traversal-> the samllest in the right sub-tree)   
            root.key = minValue(root.right);
            //delete the successor when it is placed and copied to the position of the deleted parent

            root.right = deleteRec(root.right, root.key);
        }
        return root;
    }

    //method for calling the samllest element greater than the input node to be deleted
    int minValue(Node root) {
        int minval = root.key;
        while (root.left != null) {
            minval = root.left.key;
            root = root.left;
        }
        return minval;
    }

    //calls the insert recursion method
    void insert(int key) {
       // System.out.println(key);
        root = insertRec(root, key); //the problem is here
      // System.out.println("insert method "+root.key);
    }
//inserting recursion method inserting the elements based on the control structure conditions

    Node insertRec(Node root, int key) {

        //if tree empty
        if (root == null) {
            root = new Node(key);
            return root;
        }
//inserting based on the BST properties and recur down
        if (key < root.key) {
            root.left = insertRec(root.left, key);
        } else if (key > root.key) {
            root.right = insertRec(root.right, key);
        }

        return root;
    }

    //calls inorder recursion method


    void inorderRec(Node root) {
         if (root != null) {
            inorderRec(root.left);
            System.out.println(root.key + " ");
            inorderRec(root.right);
        }
    }
    void inorder() {
      //  System.out.println(key);
        inorderRec(root);
    }

}

我得到的示例输出:

print the statement
0 

预期输出:

print the statement
50

您的节点 class 存在一个非常小的问题,导致您的程序给出错误的答案。您的 Node 构造函数如下所示:

public Node(int item) {
    item = key; // mistake here
    left = right = null;
}

问题是您将 item 的值设置为 key 的值(初始化为 0)而不是 key 到 item。您应该将其更改为:

public Node(int item) {
    key = item; //fixed
    left = right = null;
}