ενημέρωση της τιμής των βασικών κόμβων στο BST

ψήφοι
0
template <class T>
void BinaryTree<T>::UpdateKey(T newkey,T oldkey)
{
     TreeNode<T> *temp,*temp1,*temp2,*temp3;
     temp=Root;
      //checking to see if the new value clashes with any existing value
     while (temp!=NULL)
     {
         if (temp->key==newkey)
         {
            cout<<New key already exists in the tree.CannotUpdate!;                  
            cout<<endl;                 
            return;
         }
         if (newkey>temp->key)
         {
            temp=temp->RightChild;
         }
         else if (newkey<temp->key)
         {
            temp=temp->LeftChild;
         }
     }

     temp=Root;                                  
     while (temp!=NULL)
     {      
         if (temp->key==oldkey)
         {
             temp1=temp->Parent;
             temp->key=newkey;
             if (temp1->LeftChild==temp)  //if the node is the left child of its parent
             {
                 if (temp1->key<temp->key)
                 {
                     //move to right child of node whose key has to be changed and  
                    //then keep on moving to the LeftChild of every node until last 
                     //node is reached and exchange the key/value 
                     temp=temp->RightChild;                     
                     while (temp->LeftChild!=NULL)
                     {
                         temp=temp->LeftChild;
                     }
                     //exchanging key/value of the two nodes
                     temp2=temp;
                     temp3=temp1->LeftChild;
                     temp->key=temp3->key;
                     temp->value=temp3->value;
                     temp3->key=temp2->key;
                     temp3->value=temp2->value;
                 }
                 cout<<Value updated successfully1<<endl;
                 return;
             }
             //if the node is right child of its parent
             else if (temp1->RightChild==temp)
             {
                   //if key of parent is greater than its right child
                  if (temp1->key>temp->key)
                  {
                  //move to the leftchild of the node whose value has to change
                  //and then keep moving to right child until the last node
                      temp=temp->LeftChild;
                      while (temp->RightChild!=NULL)
                      {
                          temp=temp->RightChild;
                      }
                     //exchange value/key of the last node with the node whose value
                      //is updated
                      temp2=temp;
                      temp3=temp1->RightChild;
                      temp->key=temp3->key;
                      temp->value=temp3->value;
                      temp3->key=temp2->key;
                      temp3->value=temp2->value;                  
                  }     
                  cout<<Value updated successfully2<<endl;
                                                                                                             return;     
             }                  
             //comparing newkey with the left child to see if its less than the latter
             if (temp->LeftChild!=NULL && newkey<(temp->LeftChild)->key)
             {

                 temp->key=newkey;                 
                 temp1=temp;        
                 temp=temp->LeftChild;
                 while (temp->RightChild!=NULL)
                 {
                     temp=temp->RightChild;
                 }           
                 temp2=temp1;                            
                 temp1->key=temp->key;
                 temp1->value=temp->value;
                 temp->key=temp2->key;
                 temp->value=temp2->value;

                 cout<<Value updated successfully3<<endl;
                 return;
             }
             //comparing with the right child if it is greater than the latter
             //and moving key/value to the right spot if condition is met
             else if (temp->RightChild!=NULL && newkey>(temp->RightChild)->key)
             {
                 temp->key=newkey;
                 temp1=temp;
                 while (temp->LeftChild!=NULL)
                 {
                     temp=temp->LeftChild;
                 }
                 temp2=temp1;                            
                 temp1->key=temp->key;
                 temp1->value=temp->value;
                 temp->key=temp2->key;
                 temp->value=temp2->value; 

                cout<<Value updated successfully4<<endl;
                return;
             }

         }
         //traversing the tree by comparing oldkey with node key and going in the right
         //direction
         if (oldkey<temp->key)
             temp=temp->LeftChild;
         else if (oldkey>temp->key)
            temp=temp->RightChild;    
     }
}

Έχω χτίσει το δέντρο BST χρησιμοποιώντας ένα αρχείο που έχει μια σειρά που ακολουθείται από έναν αριθμό και έτσι οι κόμβοι έχουν ένα κλειδί / τιμή pair.The πρόβλημα είναι όταν μπορώ να ενημερώσω τον κόμβο (δηλαδή αλλάζοντας το κλειδί του κόμβου και την αναδιάταξη του κόμβου έτσι ώστε είναι στη σωστή θέση πάνω στο δέντρο), δεν σωστά δώσει το κλειδί γιατί όταν ψάχνω για αυτό το κλειδί (χρησιμοποιώντας τη λειτουργία αναζήτησης? My λειτουργία αναζήτησης λειτουργούν καλά, έχω δοκιμαστεί διεξοδικά), παίρνω καμία αντιστοίχιση κλειδί στο tree.I έχουν ήδη λάβει βοήθεια για κάποιες άλλες λειτουργίες και μπορούν να βρεθούν εδώ:
Δυαδική Αναζήτηση Tree - διαγραφή λειτουργία δεν λειτουργεί

Δημοσιεύθηκε 26/02/2013 στις 19:04
πηγή χρήστη
Σε άλλες γλώσσες...                            


1 απαντήσεις

ενημέρωση της τιμής των βασικών κόμβων στο BST

ψήφοι
0
template <class T>
void BinaryTree<T>::UpdateKey(T newkey,T oldkey)
{
     TreeNode<T> *temp,*temp1,*temp2,*temp3;
     temp=Root;
      //checking to see if the new value clashes with any existing value
     while (temp!=NULL)
     {
         if (temp->key==newkey)
         {
            cout<<"New key already exists in the tree.CannotUpdate!";                  
            cout<<endl;                 
            return;
         }
         if (newkey>temp->key)
         {
            temp=temp->RightChild;
         }
         else if (newkey<temp->key)
         {
            temp=temp->LeftChild;
         }
     }

     temp=Root;                                  
     while (temp!=NULL)
     {      
         if (temp->key==oldkey)
         {
             temp1=temp->Parent;
             temp->key=newkey;
             if (temp1->LeftChild==temp)  //if the node is the left child of its parent
             {
                 if (temp1->key<temp->key)
                 {
                     //move to right child of node whose key has to be changed and  
                    //then keep on moving to the LeftChild of every node until last 
                     //node is reached and exchange the key/value 
                     temp=temp->RightChild;                     
                     while (temp->LeftChild!=NULL)
                     {
                         temp=temp->LeftChild;
                     }
                     //exchanging key/value of the two nodes
                     temp2=temp;
                     temp3=temp1->LeftChild;
                     temp->key=temp3->key;
                     temp->value=temp3->value;
                     temp3->key=temp2->key;
                     temp3->value=temp2->value;
                 }
                 cout<<"Value updated successfully1"<<endl;
                 return;
             }
             //if the node is right child of its parent
             else if (temp1->RightChild==temp)
             {
                   //if key of parent is greater than its right child
                  if (temp1->key>temp->key)
                  {
                  //move to the leftchild of the node whose value has to change
                  //and then keep moving to right child until the last node
                      temp=temp->LeftChild;
                      while (temp->RightChild!=NULL)
                      {
                          temp=temp->RightChild;
                      }
                     //exchange value/key of the last node with the node whose value
                      //is updated
                      temp2=temp;
                      temp3=temp1->RightChild;
                      temp->key=temp3->key;
                      temp->value=temp3->value;
                      temp3->key=temp2->key;
                      temp3->value=temp2->value;                  
                  }     
                  cout<<"Value updated successfully2<<endl;
                                                                                                             return;     
             }                  
             //comparing newkey with the left child to see if its less than the latter
             if (temp->LeftChild!=NULL && newkey<(temp->LeftChild)->key)
             {

                 temp->key=newkey;                 
                 temp1=temp;        
                 temp=temp->LeftChild;
                 while (temp->RightChild!=NULL)
                 {
                     temp=temp->RightChild;
                 }           
                 temp2=temp1;                            
                 temp1->key=temp->key;
                 temp1->value=temp->value;
                 temp->key=temp2->key;
                 temp->value=temp2->value;

                 cout<<"Value updated successfully3"<<endl;
                 return;
             }
             //comparing with the right child if it is greater than the latter
             //and moving key/value to the right spot if condition is met
             else if (temp->RightChild!=NULL && newkey>(temp->RightChild)->key)
             {
                 temp->key=newkey;
                 temp1=temp;
                 while (temp->LeftChild!=NULL)
                 {
                     temp=temp->LeftChild;
                 }
                 temp2=temp1;                            
                 temp1->key=temp->key;
                 temp1->value=temp->value;
                 temp->key=temp2->key;
                 temp->value=temp2->value; 

                cout<<"Value updated successfully4"<<endl;
                return;
             }

         }
         //traversing the tree by comparing oldkey with node key and going in the right
         //direction
         if (oldkey<temp->key)
             temp=temp->LeftChild;
         else if (oldkey>temp->key)
            temp=temp->RightChild;    
     }
}

Έχω χτίσει το δέντρο BST χρησιμοποιώντας ένα αρχείο που έχει μια σειρά που ακολουθείται από έναν αριθμό και έτσι οι κόμβοι έχουν ένα κλειδί / τιμή pair.The πρόβλημα είναι όταν μπορώ να ενημερώσω τον κόμβο (δηλαδή αλλάζοντας το κλειδί του κόμβου και την αναδιάταξη του κόμβου έτσι ώστε είναι στη σωστή θέση πάνω στο δέντρο), δεν σωστά δώσει το κλειδί γιατί όταν ψάχνω για αυτό το κλειδί (χρησιμοποιώντας τη λειτουργία αναζήτησης? My λειτουργία αναζήτησης λειτουργούν καλά, έχω δοκιμαστεί διεξοδικά), παίρνω καμία αντιστοίχιση κλειδί στο tree.I έχουν ήδη λάβει βοήθεια για κάποιες άλλες λειτουργίες και μπορούν να βρεθούν εδώ:
Δυαδική Αναζήτηση Tree - διαγραφή λειτουργία δεν λειτουργεί

Απαντήθηκε 26/02/2013 στις 19:04
πηγή χρήστη

Cookies help us deliver our services. By using our services, you agree to our use of cookies. Learn more