Note that there are some explanatory texts on larger screens.

plurals
  1. POTrouble in deleting root node in binary search tree
    text
    copied!<p>I am trying to delete root node from my BST and then printing the tree <strong>inorder</strong>.The root deletion seems to be a problem, all other nodes are getting deleted successfully . </p> <p>Root is 20.</p> <p>inOrderPrint 5 6 7 8 9 10 17 18 20 23 24 25 29 55 56 57 58 59</p> <p>provide a node to delete</p> <p>20</p> <p>after deletion</p> <p><strong>5 6 7 8 9 10 17 18 20</strong> 5 6 7 8 9 10 17 18 23 24 25 29 55 56 57 58 59</p> <p>As you can see after deletion the bintree is not as expected. Bold keys are unwanted. Below is my code</p> <pre><code>void treeDeleteNode (binTreeT **tree, binTreeT *node) { binTreeT *succs; binTreeT *parent; binTreeT *root = *tree; if (node-&gt;rchild == NULL) { transplantTree (&amp;root, node, node-&gt;lchild); } else if (node-&gt;lchild == NULL) { transplantTree (&amp;root, node, node-&gt;rchild); } else { succs = treeMin (node-&gt;rchild); parent = getParentNode (root, succs); if (parent != node) { transplantTree (&amp;root, succs, succs-&gt;rchild); succs-&gt;rchild = node-&gt;rchild; } transplantTree (&amp;root, node, succs); succs-&gt;lchild = node-&gt;lchild; } } void transplantTree (binTreeT **root, binTreeT *old, binTreeT *new) { binTreeT *rootRef = *root; binTreeT *parent; parent = getParentNode(rootRef, old); if (NULL == parent) { *root = new; } else { if (parent-&gt;lchild == old) { parent-&gt;lchild = new; } else { parent-&gt;rchild = new; } } } binTreeT* treeMin (binTreeT *tree) { while (tree-&gt;lchild != NULL) { tree = tree-&gt;lchild; } return tree; } binTreeT* getParentNode(binTreeT *root, binTreeT* node) { binTreeT *parent = NULL; while (root-&gt;data != node-&gt;data) { parent = root; if (node-&gt;data &lt; root-&gt;data) { root = root-&gt;lchild; } else if(node-&gt;data &gt; root-&gt;data) { root = root-&gt;rchild; } } return parent; } void inOrderPrint (binTreeT *tree) { if (NULL != tree) { inOrderPrint (tree-&gt;lchild); printf("%d \t", tree-&gt;data); inOrderPrint (tree-&gt;rchild); } } </code></pre> <p>..... any help appreciated.....</p>
 

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