0

Dies ist der Code für die Binärsuche Tree Deletion, wenn ich versuche, die Elemente in den Baum einzufügen und zu drucken, die Werte erscheinen Nullen. Ich habe versucht, Debugging-Techniken zu verwenden, scheint der Fehler von der "void insert()" Methode zu kommen, da das root.key Element das Benutzer eingefügt Element aus der inorderRec() method nicht gedruckt. Ich lerne immer noch Baum DS. Vielen Dank im Voraus Jungs.Binary Search Tree-> Inorder Traversal Methode nicht den Wert eingefügt

Node-Klasse:

class Node { 

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

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

BST KLASSE:

/* 
* 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); 
    } 

} 

SAMPLE Ausgabe, die ich LERNEN AM:

print the statement 
0 

ERWARTET Ausgang:

print the statement 
50 

Antwort

1

Es gibt ein sehr kleines Problem mit Ihrer Node-Klasse, das dazu führt, dass Ihr Programm die falsche Antwort gibt. Ihr Konstruktor für Knoten sieht wie folgt aus:

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

Das Problem ist, dass Sie den Wert des Elements auf den Wert des Schlüssels setzen (die auf 0 initialisiert wird) anstelle von Schlüssel zum Artikel. Sie sollten es folgendermaßen ändern:

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