-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain16.c
63 lines (54 loc) · 1.21 KB
/
main16.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
//
// Created by ge on 2023/12/13.
//
#include <stdlib.h>
#include <stdio.h>
// 二叉排序树 无重复值
typedef struct TreeNode{
int data;
struct TreeNode* lchild;
struct TreeNode* rchild;
}TreeNode;
TreeNode *bstSearch(TreeNode *T, int key){
if (T) {
if (T->data == key){
return T;
} else if (T->data > key){
return bstSearch(T->lchild, key);
} else{
return bstSearch(T->rchild, key);
}
}else {
return NULL;
}
}
void bstInsert(TreeNode** T, int key){
if (*T == NULL) {
*T = (TreeNode *) malloc(sizeof(TreeNode));
(*T)->data = key;
(*T)->lchild = NULL;
(*T)->rchild = NULL;
}else if ((*T)->data == key) {
return;
}else if ((*T)->data > key) {
bstInsert(&(*T)->lchild, key);
} else {
bstInsert(&((*T)->rchild),key);
}
}
void preOrder(TreeNode* T ){
if (T){
printf("%d ", T->data);
preOrder(T->lchild);
preOrder(T->rchild);
}
}
int main (){
TreeNode *T = NULL;
int nums[6] = {8,6,10,9,11,23};
for (int i = 0; i < 6; i++) {
bstInsert(&T, nums[i]);
}
preOrder(T);
printf("\n");
}