二叉树

来源:互联网 发布:网络成瘾综合症的危害 编辑:程序博客网 时间:2024/06/05 06:25

排序二叉树是经常遇到的一个数据结构,相关的递归算法也是考察的重点。以下c++示例代码作为相关总结和备份:

[cpp] view plaincopyprint?
  1. #include <iostream>   
  2. using namespace std;  
  3.   
  4. typedef int T;  
  5.   
  6. //下面是关于二叉树的遍历、查找、删除、更新数据的代码(递归算法):   
  7. class bst  
  8. {  
  9.     struct Node{  
  10.         T data;  
  11.         Node* L;  
  12.         Node* R;  
  13.         Node(const T& d,Node* lp=NULL,Node* rp=NULL):data(d),L(lp),R(rp){}  
  14.     };  
  15.   
  16.     Node* root;  
  17.     int num;  
  18.   
  19.     public:  
  20.         bst():root(NULL),num(0)  
  21.         {}  
  22.   
  23.         void clear(Node* t){  
  24.             if(t==NULL)   
  25.                 return;  
  26.             clear(t->L);  
  27.             clear(t->R);  
  28.             delete t;  
  29.             t = NULL;  
  30.         }  
  31.   
  32.         ~bst()  
  33.         {  
  34.             clear(root);  
  35.         }  
  36.   
  37.         void clear(){  
  38.             clear(root);  
  39.             num = 0;  
  40.             root = NULL;  
  41.         }  
  42.   
  43.         bool empty(){  
  44.             return root==NULL;  
  45.         }  
  46.   
  47.         int size(){  
  48.             return num;  
  49.         }  
  50.   
  51.         T getRoot(){  
  52.             if(empty()) throw "empty tree";  
  53.             return root->data;  
  54.         }  
  55.   
  56.         //LNR-Travel  N(Node)、L(Left subtree)和R(Right subtree)  
  57.         void travel(Node* tree){  
  58.             if(tree==NULL) return;  
  59.             travel(tree->L);  
  60.             cout << tree->data << ' ';  
  61.             travel(tree->R);  
  62.         }  
  63.   
  64.         void travel(){  
  65.             travel(root);  
  66.             cout << endl;  
  67.         }  
  68.   
  69.         int height(Node* tree){  
  70.             if(tree==NULL) return 0;  
  71.             int lh = height(tree->L);  
  72.             int rh = height(tree->R);  
  73.             return 1+(lh>rh?lh:rh);  
  74.         }  
  75.   
  76.         int height(){  
  77.             return height(root);  
  78.         }  
  79.   
  80.         void insert(Node*& tree,const T& d){  
  81.         if(tree==NULL)  
  82.             tree = new Node(d);  
  83.         else if(d < tree->data)  
  84.             insert(tree->L,d);  
  85.         else  
  86.             insert(tree->R,d);  
  87.         }  
  88.   
  89.         void insert(const T& d){  
  90.             insert(root,d);  
  91.             num++;  
  92.         }  
  93.   
  94.         //Pass in the reference and return a reference for later write operation(such as modifiing data value)  
  95.         Node*& find(Node*& tree,const T& d){  
  96.             if(tree==NULL) return tree;  
  97.             if(tree->data == d) return tree;  
  98.             if(d < tree->data)  
  99.                 return find(tree->L,d);  
  100.             else  
  101.                 return find(tree->R,d);  
  102.         }  
  103.   
  104.         bool find(const T& d){  
  105.             return find(root,d)!=NULL;  
  106.         }  
  107.   
  108.         bool update(const T& od,const T& nd){  
  109.             Node* p = find(root,od);  
  110.             if(p==NULL)   
  111.                 return false;  
  112.             erase(od);  
  113.             insert(nd);  
  114.             return true;  
  115.         }  
  116.   
  117.         bool erase(const T& d){  
  118.             Node*& pt = find(root,d);  
  119.             if(pt==NULL)   
  120.                 return false;  
  121.   
  122.             combine(pt->L,pt->R);  
  123.             Node* p = pt;  
  124.             pt = pt->R;  
  125.             delete p;  
  126.             //don't forget to value the ptr as NULL  
  127.             p=NULL;  
  128.   
  129.             num--;  
  130.             return true;  
  131.         }  
  132.   
  133. //               A(9)   
  134. //              /  \   
  135. //             B(7) C(15)   
  136. //            /    /  \   
  137. //           D(5) E(13)F(21)    
  138.         //spend some time to understand this func  
  139.         /*this function only works for combination after deleting one note. Not the case that any one item is added into the binary tree.*/  
  140.     private:  
  141.         void combine(Node* lc,Node*& rc){  
  142.             if(lc==NULL)   
  143.                 return;  
  144.             if(rc==NULL)          //if the right sibling is empty, then, the note item replace its parent.    
  145.                 rc = lc;  
  146.             else   
  147.                 combine(lc,rc->L);//always "Gua" the note item(lc) to the left(est) leaf of his right sibling(rc)  
  148.         }         
  149. };  
  150.   
  151. int _tmain(int argc, _TCHAR* argv[])  
  152. {  
  153.     //input and construct the BST  
  154.     bst b;  
  155.     cout << "input some integers:";  
  156.     for(;;){  
  157.         int n;  
  158.         cin >> n;  
  159.         b.insert(n);  
  160.         if(cin.peek()=='\n')   
  161.             break;  
  162.     }  
  163.   
  164.     //find the old value node, then delete it and replace with the node with new value   
  165.     for(;;){  
  166.         cout << "input data pair:";  
  167.         int od,nd;  
  168.         cin >> od >> nd;  
  169.         if(od == -1 && nd == -1)  
  170.             break;  
  171.         b.update(od,nd);  
  172.     }  
  173.   
  174.     b.travel();  
  175.   
  176.     return 0;  
  177. }