-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBinarySortTree.c
More file actions
120 lines (110 loc) · 2.85 KB
/
BinarySortTree.c
File metadata and controls
120 lines (110 loc) · 2.85 KB
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
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
#include <string.h>
#include "define.h"
/**
* 顺序二叉树的搜索
*/
int static searchBST(PBinaryNode tree, int key, PBinaryNode parent, PBinaryNode *res)
{
//tree是指向树中某一个节点的指针
if(tree == NULL){
*res = parent;
return 0;
}
//变量res:如果找到就返回父母节点的地址如果找不到就返回“最亲近的节点”的地址
else if(tree->data == key){
*res = tree;
return 1;
}
else if(tree->data > key) return searchBST(tree->lChild, key, tree, res);
else return searchBST(tree->rChild, key, tree, res);
};
/**
* 顺序二叉树的插入
*/
int static insertBST(PBinaryNode *tree, int value)
{
PBinaryNode res, node;
//如果找不到
if(searchBST(*tree, value, NULL, &res) == 0){
//新的二叉树节点
node = (BinaryNode *)malloc(sizeof(BinaryNode));
node->data = value;
node->lChild = node->rChild = NULL;
//如果是空的二叉树,新节点就是根节点
if(res == NULL) *tree = node;
else if(res->data > value) res->lChild= node;
else res->rChild = node;
return 1;
}
else return 0;
}
/**
* 删除一个节点
*/
int static delete(PBinaryNode *tree)
{
PBinaryNode tmp = *tree;
//用左子树中的值最大的那个节点替换要被删除的节点
PBinaryNode s = (*tree)->lChild;
//右子树为空
if((*tree)->rChild == NULL){
*tree = (*tree)->lChild;
free(tmp);
}
//左子树为空
else if((*tree)->lChild == NULL){
*tree = (*tree)->rChild;
free(tmp);
}
else{
//一路向右
while(s->rChild != NULL){
tmp = s;
s = s->rChild;
}
(*tree)->data = s->data;
if(tmp == (*tree)) tmp->lChild = s->lChild;
else tmp->rChild = s->lChild;
}
return 1;
}
/**
* 寻找并删除顺序二叉树中的一个节点
*/
int static deleteBST(PBinaryNode *tree, int value)
{
if((*tree) == NULL) return 0;
if((*tree)->data == value) return delete(tree);
else if((*tree)->data > value) return deleteBST(&((*tree)->lChild), value);
else return deleteBST(&((*tree)->rChild), value);
}
/**
* 二叉树的中序遍历
* @param node
* @return
*/
int static middleTraversal(PBinaryNode node)
{
if(node == NULL) return 0;
else{
middleTraversal(node->lChild);
printf("%d\n", node->data);
middleTraversal(node->rChild);
return 0;
}
}
int testBinarySortTree()
{
int a[] = {34,68,12,0,58,79,110,119,45,21,33,9,10,2,8,7};
int size = sizeof(a) / sizeof(a[0]);
PBinaryNode tree = NULL;
for(int i = 0; i < size; i++){
insertBST(&tree,a[i]);
}
middleTraversal(tree);
deleteBST(&tree, 8);
middleTraversal(tree);
}