删除节点二叉搜索树(显示树)

时间:2017-11-10 05:01:08

标签: c++ binary-tree binary-search-tree nodes

这是我的二叉搜索树代码。我在使用deleteNode函数时遇到错误。而不是删除它设置节点等于零。

    #include <iostream>                                                                                                               
       #include <stdlib.h>                                                                                                               
       #include <stdio.h>                                                                                                                
       #include <iomanip>                                                                                                                

      using namespace std;                                                                                                              

      struct Node {                                                                                                                     
         int data;                                                                                                                     
          Node *left, *right;                                                                                                           
          Node() {left = right = NULL;}                                                                                                 
      };                                                                                                                                

      class Bst {                                                                                                                       
         public:                                                                                                                       
              int count;                                                                                                                
              int space;                                                                                                                
              Node *root;                                                                                                               
              Bst() {                                                                                                                   
                  root = NULL;                                                                                                          
                  count = 0;                                                                                                            
                  space = 0;                                                                                                            
              }                                                                                                                         
              void insertNode(int data) {                                                                                               
                  Node *newNode = new Node;                                                                                             
                  newNode->data = data;                                                                                                 
                  insert_r(root, newNode);                                                                                              
              }                                                                                                                         
              void insert_r(Node *&root, Node *newNode) {                                                                               
                  if (root == NULL) {                                                                                                   
                      root = newNode;                                                                                                   
                      return;                                                                                                           
                  }                                                                                                                     
                  if (newNode->data < root->data) {                                                                                     
                      insert_r(root->left, newNode); 
                 }                                                                                                                     
                  else if (newNode->data > root->data) {                                                                                
                      insert_r(root->right, newNode);                                                                                   
                  }                                                                                                                     
                  else{                                                                                                                 
                      Node *newNode = NULL;                                                                                             
                      delete newNode;                                                                                                   
                  }                                                                                                                     
              }
             void display_bst() {                                                                                                      
                  display_tree_r(root, space);                                                                                          
              }                                                                                                                         
              void display_tree_r(Node *root, int space) {                                                                              
                  if(root != NULL) {                                                                                                    
                      if(root->right) {                                                                                                 
                          display_tree_r(root->right, space + 4);                                                                       
                      }                                                                                                                 
                      if (space) {                                                                                                      
                          cout << setw(space) << ' ';                                                                                   
                      }                                                                                                                 
                      if (root->right) cout << " /\n" << setw(space) << ' ';                                                            
                      cout << root->data << "\n ";                                                                                      
                      if(root->left) {                                                                                                  
                         cout << setw(space) << ' ' << " \\\n";                                                                        
                         display_tree_r(root->left, space + 4);                                                                        
                     }                                                                                                                 
                 }                                                                                                                     
             }
             void remove(int num) {                                                                                                    
                     deleteNode(num, root);                                                                                            
             }                                                                                                                         
             void deleteNode(int num, Node *root) {                                                                                    
                      if (num < root->data)                                                                                            
                          deleteNode(num, root->left);                                                                                 
                      else if (num > root->data)                                                                                       
                          deleteNode(num, root->right);                                                                                
                      else                                                                                                             
                          makeDeletion(root);                                                                                          
                      }                                                                                                                
             void makeDeletion(Node *&nodePtr) {                                                                                       
                          Node *tempNodePtr = NULL;                                                                                    
                          if (nodePtr == NULL)                                                                                         
                          cout << "Cannot delete empty node.\n";                                                                       
                      else if (nodePtr->right == NULL) {                                                                               
                              tempNodePtr = nodePtr;                                                                                   
                              nodePtr = nodePtr->left; // Reattach the left child.                                                     
                              delete tempNodePtr;                                                                                      

                              }                                                                                                        
                      else if (nodePtr->left == NULL) {                                                                                
                              tempNodePtr = nodePtr;                                                                                   
                              nodePtr = nodePtr->right; // Reattach the right child.                                                   
                              delete tempNodePtr;                                                                                      

                              }                                                                                                        
                          else {                                                                                                       
                                  tempNodePtr = nodePtr->right;                                                                        
                                  while (tempNodePtr->left)                                                                            
                                  tempNodePtr = tempNodePtr->left;                                                                     
                                  tempNodePtr->left = nodePtr->left;                                                                   
                              tempNodePtr = nodePtr;                                                                                   
                                  nodePtr = nodePtr->right;                                                                            
                              delete tempNodePtr;                                                                                      

                              }                                                                                                        
                     }                                                                                                                 

     };

int main() {                                                                                                                                                                                            

     Bst bst;                                                                                                                                                                                            
     bst.insertNode(5);                                                                                                                                                                                  
     bst.insertNode(8);                                                                                                                                                                                  
     bst.insertNode(7);                                                                                                                                                                                  
     bst.insertNode(9);                                                                                                                                                                                  
     bst.insertNode(2);                                                                                                                                                                                  
     bst.insertNode(3);                                                                                                                                                                                  
     bst.insertNode(1);                                                                                                                                                                                  

     bst.display_bst();                                                                                                                                                                                  
     cout << endl << endl << endl;                                                                                                                                                                       
     cout << endl << endl << endl;                                                                                                                                                                       
     bst.remove(5);                                                                                                                                                                                      

     bst.display_bst();                                                                                                                                                                                  

     return 0;                                                                                                                                                                                           
 }    

这是我得到的输出我希望树自我修复,下一个节点出现但我得到了第二棵树。我的问题是如何解决它,为什么它将5设置为零。

       9                                                                                                                                                                                                   
      /                                                                                                                                                                                                     
    8                                                                                                                                                                                                       
      \                                                                                                                                                                                                     
        7                                                                                                                                                                                                   
  /                                                                                                                                                                                                         

5                                                                                                                                                                                                          
   \                                                                                                                                                                                                        
        3                                                                                                                                                                                                   
      /                                                                                                                                                                                                     
    2                                                                                                                                                                                                       
      \                                                                                                                                                                                                     
        1                                                                                                                                                                                                   






        9                                                                                                                                                                                                   
      /                                                                                                                                                                                                     
    8                                                                                                                                                                                                       
      \                                                                                                                                                                                                     
        7                                                                                                                                                                                                   
          \                                                                                                                                                                                                 
                3                                                                                                                                                                                           
              /                                                                                                                                                                                             
            2                                                                                                                                                                                               
              \                                                                                                                                                                                             
                1                                                                                                                                                                                           
  /                                                                                                                                                                                                         
 0                                                                                                                                                                                                          
   \                                                                                                                                                                                                        
        3                                                                                                                                                                                                   
      /                                                                                                                                                                                                     
    2                                                                                                                                                                                                       
      \                                                                                                                                                                                                     
        1

0 个答案:

没有答案