-
Notifications
You must be signed in to change notification settings - Fork 0
/
insertTree.c
136 lines (132 loc) · 4.08 KB
/
insertTree.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
#define _CRT_SECURE_NO_WARNINGS
#include "class.h"
#include "classTree.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
// insert a recipe name node into the tree
bool insertTree_recipe_name(struct t_recipe** root, struct recipe* insert)
{
if (*root == NULL)
{
*root = creatTree_recipe(insert);
return true;
}
// create a new node for the tree
struct t_recipe* current = *root;
while (1)
{
if (strcmp((insert)->name, current->r_node->name) < 0)
{
if (current->left == NULL)
{
// when the left child is null, create a new node and link it to the left child
current->left = creatTree_recipe(insert);
return true;
}
else
{
current = current->left;
}
}
else // strcmp((*insert)->name, current->r_node->name) > 0
{
// when the recipe name is greater than the current node's recipe name
if (current->right == NULL)
{
// when the right child is null, create a new node and link it to the right child
current->right = creatTree_recipe(insert);
return true;
}
else
{
current = current->right;
}
}
}
}
// use the custom order id as the standard to insert nodes into the tree
bool insertTree_order_id(struct t_order** root, struct order* insert)
{
if (*root == NULL)
{
*root = creatTree_order(insert);
return true;
}
// recursively find the insertion location
struct t_order* current = *root;
while (1)
{
if (strcmp((insert)->order_id, current->o_node->order_id) < 0)
{
// when the order id is less than the current node's order id
if (current->left == NULL)
{
// when the left child is null, create a new node and link it to the left child
current->left = creatTree_order(insert);
return true;
}
else
{
current = current->left;
}
}
else // strcmp((insert)->order_id, current->o_node->order_id) > 0
{
// when the order id is greater than the current node's order id
if (current->right == NULL)
{
// when the right child is null, create a new node and link it to the right child
current->right = creatTree_order(insert);
return true;
}
else
{
current = current->right;
}
}
}
}
// use the merchant name as the standard to insert nodes into the tree
bool insertTree_Merchant_name(struct t_Merchant** root, struct Merchant* insert)
{
if (*root == NULL)
{
*root = creatTree_Merchant(insert);
return true;
}
// recursively find the insertion location
struct t_Merchant* current = *root;
while (1)
{
if (strcmp((insert)->name, current->M_node->name) < 0)
{
// when the merchant name is less than the current node's merchant name
if (current->left == NULL)
{
// when the left child is null, create a new node and link it to the left child
current->left = creatTree_Merchant(insert);
return true;
}
else
{
current = current->left;
}
}
else // strcmp((insert)->name, current->M_node->name) > 0
{
// when the merchant name is greater than the current node's merchant name
if (current->right == NULL)
{
// when the right child is null, create a new node and link it to the right child
current->right = creatTree_Merchant(insert);
return true;
}
else
{
current = current->right;
}
}
}
}