二叉排序树的建立与遍历 Anti

 1 #include<stdio.h>
 2 #include<stdlib.h>
 3 struct node
 4 {
 5     int data;
 6     struct node *left, *right;
 7 };
 8 
 9 void build(struct node **p, int k) //指向指针的指针。。
10 {
11     if(*p == NULL)
12     {
13         *p = (struct node *)malloc(sizeof(struct node));
14         (*p)->data = k;
15         (*p)->left = (*p)->right = NULL;
16     }
17     else if((*p)->data > k)
18         build(&(*p)->left, k);
19     else build(&(*p)->right, k);
20 }
21 
22 void inorder(struct node *p)
23 {
24     if(p == NULL)return;
25     inorder(p->left);
26     printf("%d", p->data);
27     inorder(p->right);
28 }
29 
30 void del(struct node *p) //释放内存
31 {
32     if(p == NULL)return;
33     del(p->left);
34     del(p->right);
35     free(p);
36 }
37 
38 int main()
39 {
40     int n, x;
41     scanf("%d", &n);
42     struct node *root = NULL;
43     while(n--)
44     {
45         scanf("%d", &x);
46         build(&root, x);
47     }
48     inorder(root);
49     del(root);
50     return 0;
51 }

第一次写博客好紧张啊。。。

这个传说中的二叉排序树写了好多遍才写对,一开始build函数传递的不是指向指针的指针,导致不输出,纠结了好久,睡了一下午来了灵感,幸福总是来的这么突然。

原文地址:https://www.cnblogs.com/wolfred7464/p/2996381.html