-
Notifications
You must be signed in to change notification settings - Fork 0
/
deleteLargest.c
52 lines (43 loc) · 1.14 KB
/
deleteLargest.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 -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
//You must free the memory of the node that is deleted
//If the tree is empty simply return the empty tree
//For example
/*
// 5 5
/ / \ / \
// 2 7 ---> 2 7
// / \ /
// 6 8 6
//
*/
//You may assume there are no duplicates in the tree
treeLink deleteLargest (treeLink tree) {
// CHANGE THE CODE HERE
treeLink curr = tree;
if (curr == NULL){
return curr;
}
//traverse the tree
if(curr->right != NULL){
curr->right = deleteLargest(curr->right);
// return tree;
// no right subtree which means you have hit the max
} else {
curr = tree->left;
free(tree);
// return curr;
}
return curr;
}