MCPcopy Create free account
hub / github.com/codemistic/Data-Structures-and-Algorithms / Delete

Function Delete

Trees/BST.C:111–144  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

109}
110
111struct Node *Delete(struct Node *p, int key)
112{
113 struct Node *q = NULL;
114 if (p == NULL)
115 return NULL;
116 if (p->lchild == NULL && p->rchild == NULL)
117 {
118 if (p == root)
119 root = NULL;
120 free(p);
121 return NULL;
122 }
123
124 if (key < p->data)
125 p->lchild = Delete(p->lchild, key);
126 else if (key > p->data)
127 p->rchild = Delete(p->rchild, key);
128 else
129 {
130 if (Height(p->lchild) > Height(p->rchild))
131 {
132 q = InPre(p->lchild);
133 p->data = q->data;
134 p->lchild = Delete(p->lchild, q->data);
135 }
136 else
137 {
138 q = InSucc(p->rchild);
139 p->data = q->data;
140 p->rchild = Delete(p->rchild, q->data);
141 }
142 }
143 return p;
144}
145
146int main()
147{

Callers 1

mainFunction · 0.70

Calls 3

HeightFunction · 0.85
InPreFunction · 0.85
InSuccFunction · 0.85

Tested by

no test coverage detected