Note that there are some explanatory texts on larger screens.

plurals
  1. POupdating the value of node key in BST
    primarykey
    data
    text
    <pre><code>template &lt;class T&gt; void BinaryTree&lt;T&gt;::UpdateKey(T newkey,T oldkey) { TreeNode&lt;T&gt; *temp,*temp1,*temp2,*temp3; temp=Root; //checking to see if the new value clashes with any existing value while (temp!=NULL) { if (temp-&gt;key==newkey) { cout&lt;&lt;"New key already exists in the tree.CannotUpdate!"; cout&lt;&lt;endl; return; } if (newkey&gt;temp-&gt;key) { temp=temp-&gt;RightChild; } else if (newkey&lt;temp-&gt;key) { temp=temp-&gt;LeftChild; } } temp=Root; while (temp!=NULL) { if (temp-&gt;key==oldkey) { temp1=temp-&gt;Parent; temp-&gt;key=newkey; if (temp1-&gt;LeftChild==temp) //if the node is the left child of its parent { if (temp1-&gt;key&lt;temp-&gt;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-&gt;RightChild; while (temp-&gt;LeftChild!=NULL) { temp=temp-&gt;LeftChild; } //exchanging key/value of the two nodes temp2=temp; temp3=temp1-&gt;LeftChild; temp-&gt;key=temp3-&gt;key; temp-&gt;value=temp3-&gt;value; temp3-&gt;key=temp2-&gt;key; temp3-&gt;value=temp2-&gt;value; } cout&lt;&lt;"Value updated successfully1"&lt;&lt;endl; return; } //if the node is right child of its parent else if (temp1-&gt;RightChild==temp) { //if key of parent is greater than its right child if (temp1-&gt;key&gt;temp-&gt;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-&gt;LeftChild; while (temp-&gt;RightChild!=NULL) { temp=temp-&gt;RightChild; } //exchange value/key of the last node with the node whose value //is updated temp2=temp; temp3=temp1-&gt;RightChild; temp-&gt;key=temp3-&gt;key; temp-&gt;value=temp3-&gt;value; temp3-&gt;key=temp2-&gt;key; temp3-&gt;value=temp2-&gt;value; } cout&lt;&lt;"Value updated successfully2&lt;&lt;endl; return; } //comparing newkey with the left child to see if its less than the latter if (temp-&gt;LeftChild!=NULL &amp;&amp; newkey&lt;(temp-&gt;LeftChild)-&gt;key) { temp-&gt;key=newkey; temp1=temp; temp=temp-&gt;LeftChild; while (temp-&gt;RightChild!=NULL) { temp=temp-&gt;RightChild; } temp2=temp1; temp1-&gt;key=temp-&gt;key; temp1-&gt;value=temp-&gt;value; temp-&gt;key=temp2-&gt;key; temp-&gt;value=temp2-&gt;value; cout&lt;&lt;"Value updated successfully3"&lt;&lt;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-&gt;RightChild!=NULL &amp;&amp; newkey&gt;(temp-&gt;RightChild)-&gt;key) { temp-&gt;key=newkey; temp1=temp; while (temp-&gt;LeftChild!=NULL) { temp=temp-&gt;LeftChild; } temp2=temp1; temp1-&gt;key=temp-&gt;key; temp1-&gt;value=temp-&gt;value; temp-&gt;key=temp2-&gt;key; temp-&gt;value=temp2-&gt;value; cout&lt;&lt;"Value updated successfully4"&lt;&lt;endl; return; } } //traversing the tree by comparing oldkey with node key and going in the right //direction if (oldkey&lt;temp-&gt;key) temp=temp-&gt;LeftChild; else if (oldkey&gt;temp-&gt;key) temp=temp-&gt;RightChild; } } </code></pre> <p>I have built the BST tree using a file that has a string followed by a number and so the nodes have a key/value pair.The problem is when I update the node(i.e changing the key of the node and rearranging the node so that it is in the correct position on the tree), it doesn't correctly give it that key because when I search for that key(using search function;My search function is working fine,i have tested it thoroughly),i get no matching key in the tree.I have already taken help for some other functions and they can be found here:<br> <a href="https://stackoverflow.com/questions/15094225/binary-search-tree-delete-function-not-working">Binary Search Tree--delete function not working</a></p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload