c - Problem with pointers in binary search tree deletion -
I am trying to implement the operation of binary search trees and stuck on deletion. The use of indoor traversal as a representation of the tree is initially 10 11 14.
Removing node 10, the expected output is 11 14 but I'm getting 11 11 14.
Removing node 14 is expected to produce 11, but I am getting 0 11 67837.
Please tell why I am getting the wrong output I am not looking for any code :).
typedef struct _node {int data; Struct _node * left; Struct _node * right; } Node; Node * bstree_search (node * root, int key) {if (root == NULL} {return root; } // Depending on the binary search relationship, the key can be found either in the left, // right, or root. If (key> route-> data) back to bstree_search (root-> gt; right, key); Else if (key & lt; root- & gt; data) bstree_search (root-> left, key) returns; Return route; } Zero bstree_inert (node ** skillful, int value) {// address has passed since address (root is detected) if we can change root (* adroot == NULL) {* adroot = malloc (Sizeof (** adroot)); (* Adrott) - & gt; Data = value; (* Adrott) - & gt; Correct = (* Adrott) - & gt; Left = null; Return; } If (value> (* adroot) -> data) bstree_inert (& (* adroot) -> true, value); Other bstree_inert (& amp; (* adroot) - & gt; left, value); } Zero bstree_inorder_walk (node * root) {if (root! = NULL) {bstree_inorder_walk (root-> left); Printf ("% d", root-> data); Bstree_inorder_walk (root-> right); }} Zero bstree_delete (node ** adnode) {// node with no children or only one child node * node, * temp; Node = temp = * adnode; If ((* adnode) -> right == NULL || (* adnode) -> Left == tap) {if ((* adnode) -> right == NULL) {* adnode = ( * Adnode) - & gt; left; } Other {* adnode = (* adnode) - & gt; right; }} Other {// nodes with two children} free (temp); } Int main () {node * root = null; Node * needle = null; Int i, ames [] = {11,10,14}; For (i = 0; i
Please tell why I am getting wrong output.
Your Delete
function should also change the origin of the deleted node. For example, when you delete node 10, you need to set the root node
to the left
child on NULL
. Since you do not do this, when you later cross the tree, you can print the already free data.
I have not seen any code other than delete
, so after making this change, I can not make any guarantees about it.
Comments
Post a Comment