码迷,mamicode.com
首页 > 其他好文 > 详细

01--(2)数据结构——二叉树

时间:2016-04-19 13:53:11      阅读:207      评论:0      收藏:0      [点我收藏+]

标签:

一、二叉树的定义

二叉树是n(n>=0)个结点的有限集合,该集合或者为空集(称为空二叉树),或者由一个根结点和两颗互不相交的、分别称为根结点的左子树和右子树的二叉树组成。

二、二叉树的特点

1、每个结点最多有两颗子树。

2、左子树和右子树是有序的。

3、即使树中只有一课子树也要区分左右子树。

二叉树具有五种基本形态:

1、空二叉树。

2、只有一个根结点。

3、根结点只有左树。

4、根结点只有右树。

5、根结点既有左子树又有右子树。

三、特殊二叉树

1、斜树

所有的结点都只有左子树的二叉树叫做左斜树,所有结点都是只有右子树的二叉树叫右斜树。

2、满二叉树

在一颗二叉树中,如果所有分支结点都存在左子树和右子树,并且所有叶子都在同一层上,这样树称为满二叉树。

3、完全二叉树

对一颗具有n个结点的二叉树按层次编号,如果编号i (1<=i <=n)的结点与同样深度的满二叉树中编号为i的结点在二叉树中位置完全相同,则这棵树称为完全二叉树。

四、二叉树的性质

1、在二叉树的第i层上至多有2^(i-1)个结点(i>=1)

2、深度为k的二叉树至多有2^k-1个结点(k>=1)

3、对任何一颗二叉树T,如果其终端结点数为n0,深度为2的结点树为n2,则n0 = n2 + 1;

4、具有n个结点的完全二叉树的深度为log2^n+1

5、如果对一颗有n个结点的完全二叉树(其深度为log2^n+1)的结点按层序编号,对任一结点有:

(1)如果i=1,则结点i是二叉树的根,无双亲;如果i>1,则其双亲是结点[i/2].

(2)如果2i >n,则结点i无左孩子;否则其左孩子是结点2i

(3)如果2i+1>n,则结点i无右孩子,否则其右孩子是结点2i+1.

五、二叉树的实现

 

[cpp] view plain copy
 
 print?技术分享技术分享
  1. #include <iostream>  
  2. using namespace std;  
  3. typedef char T;  
  4.   
  5. class bst{  
  6.     //定义二叉查找树  
  7.     struct Node{  
  8.         T data;  
  9.         Node* L;  
  10.         Node* R;  
  11.         Node(const T& d):data(d), L(), R(){}  
  12.         Node(const T& d, Node *l, Node *r):data(d), L(l), R(r){}  
  13.     };  
  14.     typedef Node* tree;  
  15.     Node * rp;  
  16.     int n;  
  17. public:  
  18.     bst():rp(), n(){}  
  19.     //清空  
  20.     void clear(){  
  21.         clear(rp);  
  22.         n = 0;  
  23.     }  
  24.     //析构函数  
  25.     ~bst(){  
  26.         clear();  
  27.     }  
  28.     //插入  
  29.     void insert(const T& d){  
  30.         insert(rp, new Node(d));  
  31.         ++n;  
  32.     }  
  33.     //查找  
  34.     tree& find(const T& d){  
  35.         return find(rp, d);  
  36.     }  
  37.     //遍历  
  38.     void travel()const{  
  39.         travel(rp);  
  40.         cout << endl;  
  41.     }  
  42.     //判断树是否为空  
  43.     bool empty()const{  
  44.         return rp==NULL;  
  45.     }  
  46.     //返回树的结点数  
  47.     int size()const{  
  48.         return n;  
  49.     }  
  50.     //移除某个结点  
  51.     bool remove(const T& d){  
  52.         tree& t = find(d);  
  53.         if(t == NULL) return false;  
  54.         Node* p = t;  
  55.         if(t->L != NULL){  
  56.             insert(t->R, t->L);  
  57.         }  
  58.         t = t->R;  
  59.         delete p;  
  60.         return true;  
  61.     }  
  62.   
  63.     //获取根结点数据  
  64.     const T& root()const{  
  65.         if(rp == NULL) return NULL;  
  66.         else return rp->data;  
  67.     }  
  68.   
  69.     //插入结点  
  70.     void insert(tree& t, Node *p){  
  71.         if(t == NULL){ //如果根结点为NULL,空树  
  72.             t = p;    
  73.         }else if(p->data < t->data){  
  74.             insert(t->L, p); //插入左子树  
  75.         }else{  
  76.             insert(t->R, p); //插入右子树  
  77.         }  
  78.     }  
  79.   
  80.     //查找数据  
  81.     //返回以d为根的子树的根指针  
  82.     tree& find(tree& t, const T& d){  
  83.         if(t == NULL){  
  84.             return t; //没找到   
  85.         }else if(d == t->data){  
  86.             return t; //找到了  
  87.         }else if(d < t->data){  
  88.             return find(t->L, d);  
  89.         }else{  
  90.             return find(t->R, d);   
  91.         }  
  92.     }  
  93.   
  94.     //遍历二叉树  
  95.     void travel(tree t)const{  
  96.         if(t != NULL){  
  97.             //中序遍历  
  98.             travel(t->L);  
  99.             cout << t->data << ‘ ‘;  
  100.             travel(t->R);  
  101.         }  
  102.     }  
  103.   
  104.     //清空树  
  105.     void clear(tree& t){  
  106.         if(t != NULL){  
  107.             //后序遍历  
  108.             clear(t->L);  
  109.             clear(t->R);  
  110.             delete t;  
  111.             t = NULL;  
  112.         }  
  113.     }  
  114.   
  115.     //获取树的层数  
  116.     int high(tree& t){  
  117.         if(t == NULL) return 0;   
  118.         int lh = high(t->L);  
  119.         int rh = high(t->R);  
  120.         //最高子树高度再加根结点高度1  
  121.         return 1 + (lh > rh ? lh : rh);  
  122.     }  
  123.   
  124.   
  125.   
  126. };   
  127.   
  128. int main(){  
  129.     bst b;  
  130.     b.insert(‘k‘);  
  131.     b.insert(‘s‘);  
  132.     b.insert(‘f‘);  
  133.     b.insert(‘t‘);  
  134.     b.insert(‘a‘);  
  135.     b.insert(‘m‘);  
  136.     b.insert(‘x‘);  
  137.     b.insert(‘e‘);  
  138.     b.insert(‘w‘);  
  139.     b.insert(‘b‘);  
  140.     b.insert(‘u‘);  
  141.     b.insert(‘j‘);  
  142.     b.travel();  
  143.     cout << "**********remove k,m,u,j******" << endl;  
  144.     b.remove(‘k‘);  
  145.     b.remove(‘m‘);  
  146.     b.remove(‘u‘);  
  147.     b.remove(‘j‘);  
  148.     b.travel();  
  149.     cout << "**********remove root*********" << endl;  
  150.     while(!b.empty()) b.remove(b.root);  
  151.     cout << "size:" << b.size() << endl;  
  152.     b.travel();  
  153.   
  154.     return 0;  
  155. }  

01--(2)数据结构——二叉树

标签:

原文地址:http://www.cnblogs.com/wohenben/p/5407833.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!