-
Notifications
You must be signed in to change notification settings - Fork 0
/
bistree.c
146 lines (137 loc) · 2.9 KB
/
bistree.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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
#include <stdlib.h>
#include <string.h>
#include "bitree.h"
#include "bistree.h"
int bistree_insert(BisTree *bst, const void *data){
BisTreeNode *pNode;
if((pNode = (BisTreeNode *)malloc(sizeof(BisTreeNode))) == NULL){
return -1;
}
pNode->left = NULL;
pNode->right = NULL;
pNode->count = 1;
pNode->data = (void *)data;
/* add root node */
if(bst->root == NULL){
bst->root = pNode;
bst->size = 1;
return 0;
}
BisTreeNode **ppNode = &(bst->root);
while(1){
int temp = bst->compare(data, (*ppNode)->data);
if(temp < 0){
if((*ppNode)->left == NULL){
(*ppNode)->left = pNode;
bst->size++;
return 0;
}
ppNode = &((*ppNode)->left);
}
else if(temp > 0){
if((*ppNode)->right == NULL){
(*ppNode)->right = pNode;
bst->size++;
return 0;
}
ppNode = &((*ppNode)->right);
}
else{
(*ppNode)->count++;
free(pNode);
return (*ppNode)->count;
}
}
}
int bistree_remove(BisTree *bst, const void *data){
if(bst->root == NULL){
return -1;
}
//BisTreeNode *pParentNode = NULL;
BisTreeNode *pNode;
BisTreeNode **ppNode = &(bst->root);
while(1){
int temp = bst->compare(data, (*ppNode)->data);
if(temp < 0){
if((*ppNode)->left == NULL){
return -1;
}
//pParentNode = *ppNode;
ppNode = &((*ppNode)->left);
}
else if(temp > 0){
if((*ppNode)->right == NULL){
return -1;
}
//pParentNode = *ppNode;
ppNode = &((*ppNode)->right);
}
else{
if((*ppNode)->count > 1){
(*ppNode)->count--;
return (*ppNode)->count;
}
else{
/* count is 1, should remove node */
if((*ppNode)->left == NULL && (*ppNode)->right == NULL){
if(bst->destroy != NULL){
bst->destroy((*ppNode)->data);
}
pNode = *ppNode;
*ppNode = NULL;
free(pNode);
bst->size--;
return 0;
}
else if((*ppNode)->left != NULL && (*ppNode)->right == NULL){
if(bst->destroy != NULL){
bst->destroy((*ppNode)->data);
}
pNode = *ppNode;
*ppNode = (*ppNode)->left;
free(pNode);
bst->size--;
return 0;
}
else if((*ppNode)->left == NULL && (*ppNode)->right != NULL){
if(bst->destroy != NULL){
bst->destroy((*ppNode)->data);
}
pNode = *ppNode;
*ppNode = (*ppNode)->right;
free(pNode);
bst->size--;
return 0;
}
else{
pNode = (*ppNode)->left;
BisTreeNode *parent = NULL;
while(pNode->right != NULL){
parent = pNode;
pNode = pNode->right;
}
if(parent == NULL){
(*ppNode)->left->right = (*ppNode)->right;
pNode = *ppNode;
*ppNode = (*ppNode)->left;
return 0;
}
if(pNode->left != NULL){
parent->right = pNode->left;
}
else{
parent->right = NULL;
}
free(*ppNode);
*ppNode = pNode;
bst->size--;
return 0;
}
}
}
}
return -1;
}
int bistree_linearize(const BisTree *bst, List *lst){
return bitree_inorder(bst->root, lst);
}