由于才疏学浅,平时做题很少建立一棵完整的树。因此觉得二叉查找树又啰嗦又没用,直到今天实验课,让我极度之无语,一个小破题儿差点没整死我... ...于是才决心好好整理一下介个曾经被我藐视过得数据结构...

  我打算先从今天的实验课的那道题说起,然后再系统写一棵二叉查找树,也为以后总结各种数的变形打好基础 ~

  实验课的题目是这样的:

题目2

给出一个整数序列,请按照顺序建立二叉查找树(BST),然后层次化输出,即先输出根结点,然后是根结点的左孩子、根结点的右孩子,一层一层,从左到右地输出。

例:输入顺序为37,24,42,32,7,40,2,42,120。对应的二叉查找树如下所示

层次化输出为:37 24 42 7 32 40 42 2 120

 

我第一次的错误代码是这样的:

1 #include<iostream>
2  using namespace std;
3 #include<queue>
4  struct Node{
5 int value;
6 Node *left,*right;
7 Node(int v){
8 value = v;
9 left = right =NULL;
10 }
11 };
12  int main(){
13 int n,value;
14 cin>>n>>value;
15 n--;
16 Node *root = new Node(value);
17 /**///无法建立该树,应该是指针引用的问题 !!!???
18 while(n--){
19 cin>>value;
20 Node *next = root;
21 while(next!=NULL){
22 if(value<next->value)next=next->left;
23 else next = next->right;
24 }
25 next = new Node(value);
      //针对21行到25行的错误,除了后面提到的转为递归的方法解决外,其实可以进行小小的改动
      //只是当初对指针用的不是很熟练,居然忘了让指针移动...
      while(true){
        if(value<next->value){
          if(next->left!=NULL)next = next->left;
          else{
            next->left = new Node(value);
            next = next->left;
            break;
          }
            
        else{
          if(next->right!=value)next = next->right;
          else{
            next->right = new Node(value);
            next = next->right;
            break;
          }
                    
26 }
27 cout<<endl<<"层次遍历:";
28 print2(root);
29 return 0;
30 }
31 //按层次遍历该树
32 void print2(Node *root){
33 queue<Node *>nodes;
34 nodes.push(root);
35 Node *next = NULL;
36 while(nodes.size()!=0){
37 next = nodes.front();
38 nodes.pop();
39 cout<<next->value<<" ";
40 if(next->left!=NULL)
41 nodes.push(next->left);
42 if(next->right!=NULL)
43 nodes.push(next->right);
44 }
45 cout<<endl;
46 }

错误的关键是由于对第20行代码的错误理解 ,说到底是对指针操作的错误理解 !!!

Node*next = root;只是将root的值赋给next,那么在第25行的时候,对next进行操作,实际上root根本就没有发生变化。

后来想想,单独起一个insert()插入函数,递归调用,应该能对root值进行更改了,结果...第二次错误代码:

1 #include<iostream>
2 using namespace std;
3 #include<queue>
4 struct Node{
5 int value;
6 Node *left,*right;
7 Node(int v){
8 value = v;
9 left = right =NULL;
10 }
11 };
12
13 void insert(int k,Node * t){
14 if(t==NULL){
15 t = new Node(k);
16 }
17 else {
18 if(k<t->value)
19 insert(k,t->left);
20 else
21 insert(k,t->right);
22 }
23 }
24 //按层次遍历该树
25 void print2(Node *root){
26 queue<Node *>nodes;
27 nodes.push(root);
28 Node *next = NULL;
29 while(nodes.size()!=0){
30 next = nodes.front();
31 nodes.pop();
32 cout<<next->value<<" ";
33 if(next->left!=NULL)
34 nodes.push(next->left);
35 if(next->right!=NULL)
36 nodes.push(next->right);
37 }
38 cout<<endl;
39 }
40
41
42 int main(){
43 int n,value;
44 cin>>n>>value;
45 n--;
46 Node *root = new Node(value);
47 /**///无法建立该树,应该是指针引用的问题 !!!???
48 while(n--){
49 cin>>value;
50 Node *next = root;
51 insert(value,next);
52 }
53 cout<<endl<<"层次遍历:";
54 print2(root);
55 return 0;
56 }
57
58 /*
59 9
60 37 24 42 32 7 40 2 42 120
61 */

改完后结果还是不行,树还是建立不起来,当时几乎崩溃,果断关机走人... ...下午又想了想,只加了一个符号... ...好了...泪奔

在程序的第13行,参数列表中,应该加上引用符号&,即为:void insert(int key,Node * &t)

道理不说了,小屁孩而都知道...跟当年分析swap(int a,int b)  ,和swap(int &a,int &b)的不同之处,道理一摸一样... ...狗熊掰棒子,学了后面忘了前面... ...

此题到此结束,二叉树的完整代码即相关操作如下:

posted on 2011-05-17 21:42  geeker  阅读(597)  评论(0编辑  收藏  举报