0

これはバイナリ検索ツリー削除のコードで、ツリーに要素を挿入して印刷しようとすると、表示される値はゼロになります。私はroot.key要素がinorderRec() methodからのユーザー挿入要素を印刷しないので、デバッグ技術を使用して試しましたが、エラーは"void insert()"メソッドから来ているように見えます。私はまだ木DSを学んでいます。事前にみんなありがとう。バイナリ検索ツリー - > Inorderトラバーサルメソッドが挿入された値を出力しない

Nodeクラス:

class Node { 

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

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

BSTの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 

答えて

1

あなたのプログラムに間違った答えを与えているNodeクラスには、ごく小さな問題があります。ノードのためのあなたのコンストラクタは次のようになります。

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

問題は、あなたの代わりにアイテムへの鍵の(0に初期化される)、キーの値に項目の値を設定していることです。変更する必要があります:

public Node(int item) { 
    key = item; //fixed 
    left = right = null; 
} 
関連する問題