C++ 二叉树的实现

  1 #include<iostream.h>
  2 struct tree
  3 {
  4     int data;
  5     tree *left,*right;
  6 };
  7 class Btree
  8 {
  9     static int n;
 10     static int m;
 11 public:
 12     tree *root;
 13     Btree()
 14     {
 15         root=NULL;
 16     }
 17     void create_Btree(int);
 18     void Preorder(tree *);                  //先序遍历
 19     void inorder(tree *);                   //中序遍历
 20     void Postorder(tree *);                 //后序遍历
 21     void display1() {Preorder(root); cout<<endl;}
 22     void display2() {inorder(root);cout<<endl;}
 23     void display3() {Postorder(root); cout<<endl;}  
 24     int count(tree *);                      //计算二叉树的个数
 25     int findleaf(tree *);                   //求二叉树叶子的个数
 26     int findnode(tree *);                   //求二叉树中度数为1的结点数量,这是当初考数据结构时候的最后一道题目
 27 };                                          
 28 int Btree::n=0;
 29 int Btree::m=0;
 30 void Btree::create_Btree(int x)
 31 {
 32     tree *newnode=new tree;
 33     newnode->data=x;
 34     newnode->right=newnode->left=NULL;
 35     if(root==NULL)
 36         root=newnode;
 37     else
 38     {
 39         tree *back;
 40         tree *current=root;
 41         while(current!=NULL)
 42         {
 43             back=current;
 44             if(current->data>x)
 45                 current=current->left;
 46             else
 47                 current=current->right;
 48         }
 49         if(back->data>x)
 50             back->left=newnode;
 51         else
 52             back->right=newnode;
 53     }
 54 }
 55 int Btree::count(tree *p)
 56 {
 57     if(p==NULL)
 58         return 0;
 59     else
 60         return count(p->left)+count(p->right)+1;      //这是运用了函数嵌套即递归的方法。
 61 }
 62 void Btree::Preorder(tree *temp)    //这是先序遍历二叉树,采用了递归的方法。
 63 {
 64     if(temp!=NULL)
 65     {
 66         cout<<temp->data<<" ";
 67         Preorder(temp->left);
 68         Preorder(temp->right);
 69     }
 70 }
 71 void Btree::inorder(tree *temp)      //这是中序遍历二叉树,采用了递归的方法。
 72 {
 73     if(temp!=NULL)
 74     {
 75         inorder(temp->left);
 76         cout<<temp->data<<" ";
 77         inorder(temp->right);
 78     }
 79 }
 80 void Btree::Postorder(tree *temp)     //这是后序遍历二叉树,采用了递归的方法。
 81 {
 82     if(temp!=NULL)
 83     {
 84         Postorder(temp->left);
 85         Postorder(temp->right);
 86         cout<<temp->data<<" ";
 87     }
 88 }
 89 int Btree::findleaf(tree *temp)
 90 {
 91     if(temp==NULL)return 0;
 92     else
 93     {
 94         if(temp->left==NULL&&temp->right==NULL)return n+=1;
 95         else
 96         {
 97             findleaf(temp->left);
 98             findleaf(temp->right);
 99         }
100         return n;
101     }
102 }
103 int Btree::findnode(tree *temp)
104 {
105     if(temp==NULL)return 0;
106     else
107     {
108         if(temp->left!=NULL&&temp->right!=NULL)
109         {
110             findnode(temp->left);
111             findnode(temp->right);
112         }
113         if(temp->left!=NULL&&temp->right==NULL)
114         {
115             m+=1;
116             findnode(temp->left);
117         }
118         if(temp->left==NULL&&temp->right!=NULL)
119         {
120             m+=1;
121             findnode(temp->right);
122         }
123     }
124     return m;
125 }
126 
127 
128 void main()
129 {
130     Btree A;
131     int array[]={7,4,2,3,15,35,6,45,55,20,1,14,56,57,58};
132     int k;
133     k=sizeof(array)/sizeof(array[0]);
134     cout<<"建立排序二叉树顺序: "<<endl;
135     for(int i=0;i<k;i++)
136     {
137         cout<<array[i]<<" ";
138         A.create_Btree(array[i]);
139     }
140     cout<<endl;
141     cout<<"二叉树节点个数: "<<A.count(A.root)<<endl;
142     cout<<"二叉树叶子个数:"<<A.findleaf(A.root)<<endl;
143     cout<<"二叉树中度数为1的结点的数量为:"<<A.findnode(A.root)<<endl;
144     cout<<endl<<"先序遍历序列: "<<endl;
145     A.display1();
146     cout<<endl<<"中序遍历序列: "<<endl;
147     A.display2();
148     cout<<endl<<"后序遍历序列: "<<endl;
149     A.display3();
150 }

 

    原文作者:算法小白
    原文地址: https://www.cnblogs.com/elleniou/archive/2012/05/03/2480042.html
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞