-
Notifications
You must be signed in to change notification settings - Fork 0
/
structure.cpp
164 lines (127 loc) · 3.75 KB
/
structure.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
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
//
// Created by Pramy on 2017/12/12.
//
#include <cstdlib>
#include <cstdio>
#include <cstring>
#include "structure.h"
/**
* 创建b树
* @param tree :b树
* @param m :阶数
* @return status:状态
*/
int createBTree(BTree &tree, int m){
if(m<3){
fprintf(stderr, "[%s][%d] Parameter 'max' must greater than 2.\n", __FILE__, __LINE__);
return Failure;
}
tree = (BTree)calloc(1, sizeof(Tree));
if(tree == nullptr){
fprintf(stderr, "[%s][%d] errmsg:[%d] %s!\n", __FILE__, __LINE__, errno, strerror(errno));
return Failure;
}
tree->max = m - 1;
tree->min = m/2-1;
if(m%2 != 0)
tree->min++;
tree->splitIndex = m/2;
tree->root = nullptr;//空树
return Success;
}
void destroyTree_Node(BTreeNode node){
if(node== nullptr)
return ;
for (int i = 0; i <= node->num; ++i) {
destroyTree_Node(node->child[i]);
}
destroyNode(node);
}
void destroyTree(BTree &bTree){
if(bTree== nullptr)
return;
destroyTree_Node(bTree->root);
free(bTree);
bTree= nullptr;
}
int createNodeByOrder(BTree &bTree, BTreeNode &bTreeNode, int recordSize){
int size = bTree->max+2;
bTreeNode = (BTreeNode)malloc(sizeof(BNode));
if(bTreeNode == nullptr){
fprintf(stderr, "[%s][%d] errmsg:[%d] %s\n", __FILE__, __LINE__, errno, strerror(errno));
return Failure;
}
bTreeNode->num=0;
bTreeNode->key = (KeyType *)malloc(size*sizeof(KeyType));
if(bTreeNode->key == nullptr){
free(bTreeNode);
bTreeNode= nullptr;
fprintf(stderr, "[%s][%d] errmsg:[%d] %s\n", __FILE__, __LINE__, errno, strerror(errno));
return Failure;
}
memset(bTreeNode->key,0,(size)* sizeof(KeyType));
bTreeNode->child = (BTreeNodeChild)malloc(size*sizeof(BTreeNodeChild));
if(bTreeNode->child == nullptr){
free(bTreeNode->key);
bTreeNode->key= nullptr;
free(bTreeNode);
bTreeNode = nullptr;
fprintf(stderr, "[%s][%d] errmsg:[%d] %s\n", __FILE__, __LINE__, errno, strerror(errno));
return Failure;
}
memset(bTreeNode->child, 0,(size)* sizeof(BTreeNodeChild));
bTreeNode->record = (Record*)malloc(recordSize*sizeof(Record));
bTreeNode->parent= nullptr;
return Success;
}
int createNode(BTree &bTree,BTreeNode &bTreeNode){
return createNodeByOrder(bTree,bTreeNode,1);
}
void destroyNode(BTreeNode &bTreeNode){
if(bTreeNode== nullptr)
return;
for (int i = 0; i <= bTreeNode->num; ++i) {
if(i!=bTreeNode->num)
printf("销毁:%d,",bTreeNode->key[i]);
bTreeNode->child[i] = nullptr;
}
free(bTreeNode->child);
bTreeNode->child= nullptr;
bTreeNode->parent = nullptr;
free(bTreeNode->key);
bTreeNode->key = nullptr;
free(bTreeNode->record);
bTreeNode->record = nullptr;
free(bTreeNode);
bTreeNode = nullptr;
}
int createResult(Result &result){
result = (Result)calloc(1, sizeof(ResultNode));
if(result== nullptr){
fprintf(stderr, "[%s][%d] errmsg:[%d] %s\n", __FILE__, __LINE__, errno, strerror(errno));
return Failure;
}
result->node = nullptr;
result->index = MIN_INT;
result->tag = Failure;
return Success;
}
void destroyResult(Result &result){
free(result);
result = nullptr;
}
void beforePrintBTree(BTreeNode node,int depth,int num){
if(node == nullptr)
return;
for (int k = 0; k < depth; ++k) {
printf("%s",k==0?"":" ");
}
printf("第%d层---第%d个节点:",depth,num);
for (int i = 0; i < node->num; ++i) {
printf("%c%d%c",i==0?'[':'\0',node->key[i],i==node->num-1?']':',');
}
printf("\n");
for (int j = 0; j <=node->num ; ++j) {
beforePrintBTree(node->child[j],depth+1,j);
}
}