-
Notifications
You must be signed in to change notification settings - Fork 0
/
BINTREE.CPP
99 lines (67 loc) · 1.89 KB
/
BINTREE.CPP
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
class BinaryTree
{
//################################
private:
//################################
struct _bintree
{
CString data;
struct bintree * left;
struct bintree * right;
};
struct _bintree * root;
//============================================= PRIVATE
void KillWholeTree(struct _bintree * node);
//=============================================
//################################
public:
//################################
//========================================== Constructor
BinaryTree(void)
//==========================================
{
root = NULL;
};
//========================================== Destructor
~BinaryTree(void)
//==========================================
{
if (root!=NULL)
KillWholeTree(root);
};
//==========================================
int AddToTree(CString &newdata, struct _bintree * root);
//==========================================
// Adds a CString to the Tree
// Returns 1 if successfull
// 0 if strings already exists
}; // end of class BinaryTree
//========================================================
void BinaryTree::KillWholeTree(struct _bintree * node)
//========================================================
{
if (node->left != NULL)
KillWholeTree(&node->left);
if (node->right != NULL)
KillWholeTree(&node->right);
free(node);
};
//========================================================
int BinaryTree::AddToTree(CString &newdata, struct _bintree * root)
//========================================================
{
if (root == NULL) // empty node => AddToTree
{
root = (struct _bintree *)malloc(sizeof(struct _bintree));
root->left=NULL;
root->right=NULL;
root->data = newdata;
return 1;
}
if (strcmpi(newdata,root->data) == 0)
return 0;
if (strcmpi(newdata,root->data) < 0)
return (AddToTree(newdata,&root->left));
else
return (AddToTree(newdata,&root->right));
};