-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdeleteLargestSoln.c
52 lines (44 loc) · 1.07 KB
/
deleteLargestSoln.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
// Implement the deleteLargest function below
//
// Compile and test the function using
// gcc -Wall -Werror -o deleteLargest deleteLargest.c deleteLargestTest.o
// ./deleteLargest
#include <stdio.h>
#include <stdlib.h>
#include "deleteLargest.h"
typedef struct _treeNode {
Item item;
treeLink left, right;
} treeNode;
//Delete the largest item in the tree and return the resulting tree
//If the tree is empty simply return the empty tree
//For example
/*
// 5 5
/ / \ / \
// 2 7 ---> 2 7
// / \ /
// 6 8 6
//
*/
treeLink deleteLargest (treeLink tree) {
// CHANGE THE CODE HERE
if(tree == NULL){
return NULL;
}
treeLink prev = NULL;
treeLink curr = tree;
while(curr->right != NULL){
prev = curr;
curr = curr->right;
}
printf("Del %d\n",curr->item);
if(prev == NULL){
tree = curr->left;
free(curr);
return tree;\
}
prev->right = curr->left;
free(curr);
return tree;
}