• 二叉树之AVL树


    高度为 h 的 AVL 树,节点数 N 最多2^h − 1; 最少N(h)=N(h− 1) +N(h− 2) + 1。
    最少节点数n 如以斐波那契数列可以用数学归纳法证明:
    即:
    N(0) = 0 (表示 AVL Tree 高度为0的节点总数)
    N(1) = 1 (表示 AVL Tree 高度为1的节点总数)
    N(2) = 2 (表示 AVL Tree 高度为2的节点总数)
    N(h)=N(h− 1) +N(h− 2) + 1 (表示 AVL Tree 高度为h的节点总数)
     
     




    #include <iostream.h>  #include <math.h>;  #include <stdlib.h>; //建立一个整数类型  #define MAXSIZE 512   typedef struct obj_n_t { int obj_key; } obj_node_t; //建立树结点的基本机构  typedef struct tree_n_t { int key; struct tree_n_t *left,*right; int height; } tree_node_t; //建立堆栈    tree_node_t *stack[MAXSIZE]; //warning!the tree can contain 256 leaves at most!  int i=0; //堆栈计数器 //堆栈清空  void stack_clear()  {  while(i!=0) { stack[i-1]=NULL; i--; } } //堆栈为空 int stack_empty() { return(i==0); } //入栈函数 int push(tree_node_t *node) { if(i<MAXSIZE) { stack[i++]=node; return(0); } else return(-1); } //出栈函数 tree_node_t *pop() { if(i>0) return(stack[--i]); else return(0); } //建立get_node函数,用于动态分配内存空间 tree_node_t *get_node() { tree_node_t *tmp; tmp=(tree_node_t *)malloc(sizeof(tree_node_t)); return(tmp); } //建立return_node函数,用于释放内存 void return_node(tree_node_t *free_node) { free(free_node); } //建立左旋转函数 void left_rotation(tree_node_t *node) { tree_node_t *tmp; int tmp_key; tmp=node->left; tmp_key=node->key; node->left=node->right; node->key=node->right->key; node->right=node->left->right; node->left->right=node->left->left; node->left->left=tmp; node->left->key=tmp_key; } //建立右旋转函数 void right_rotation(tree_node_t *node) { tree_node_t *tmp; int tmp_key; tmp=node->right; tmp_key=node->key; node->right=node->left; node->key=node->left->key; node->left=node->right->left; node->right->left=node->right->right; node->right->right=tmp; node->right->key=tmp_key; } int rebalance(tree_node_t *node) { int finished=0; while(!stack_empty()&&!finished) { int tmp_height,old_height; node=pop(); //back to the root along the search path old_height=node->height; if(node->left->height-node->right->height==2) { if(node->left->left->height-node->right->height==1) { right_rotation(node); node->right->height=node->right->left->height+1; node->height=node->right->height+1; } else { left_rotation(node->left); right_rotation(node); tmp_height=node->left->left->height; node->left->height=tmp_height+1; node->right->height=tmp_height+1; node->height=tmp_height+2; } } else if(node->left->height-node->right->height==-2) { if(node->right->right->height-node->left->height==1) { left_rotation(node); node->left->height=node->left->right->height+1; node->height=node->left->height+1; } else { right_rotation(node->right); left_rotation(node); tmp_height=node->right->right->height; node->left->height=tmp_height+1; node->right->height=tmp_height+1; node->height=tmp_height+2; } } else { if(node->left->height>node->right->height) node->height=node->left->height+1; else node->height=node->right->height+1; } if(node->height==old_height) finished=1; } stack_clear(); return(0); } //建立creat_tree函数,用于建立一颗空树 tree_node_t *creat_tree() { tree_node_t *root; root=get_node(); root->left=root->right=NULL; root->height=0; return(root); //build up an empty tree.the first insert bases on the empty tree. } //建立find函数,用于查找一个对象 obj_node_t *find(tree_node_t *tree,int query_key) { tree_node_t *tmp; if(tree->left==NULL) return(NULL); else { tmp=tree; while(tmp->right!=NULL) { if(query_key<tmp->key) tmp=tmp->left; else tmp=tmp->right; } if(tmp->key==query_key) return((obj_node_t*)tmp->left); else return(NULL); } } //建立插入函数 int insert(tree_node_t *tree,obj_node_t *new_obj) { tree_node_t *tmp; int query_key,new_key; query_key=new_key=new_obj->obj_key; if(tree->left==NULL) { tree->left=(tree_node_t *)new_obj; tree->key=new_key; tree->height=0; tree->right=NULL; } else { stack_clear(); tmp=tree; while(tmp->right!=NULL) { //use stack to remember the path from root to the position at which the new object should be inserted. //then after inserting,we can rebalance from the parrent node of the leaf which pointing to new object to the root node. push(tmp); if(query_key<tmp->key) tmp=tmp->left; else tmp=tmp->right; } if(tmp->key==query_key) return(-1); else { tree_node_t *old_leaf,*new_leaf; //It must allocate 2 node space in memory. //One for the new one,another for the old one. //the previous node becomes the parrent of the new node. //when we delete a leaf,it will free two node memory spaces as well. old_leaf=get_node(); old_leaf->left=tmp->left; old_leaf->key=tmp->key; old_leaf->right=NULL; old_leaf->height=0; new_leaf=get_node(); new_leaf->left=(tree_node_t *)new_obj; new_leaf->key=new_key; new_leaf->right=NULL; new_leaf->height=0; if(tmp->key<new_key) { tmp->left=old_leaf; tmp->right=new_leaf; tmp->key=new_key; } else { tmp->left=new_leaf; tmp->right=old_leaf; } tmp->height=1; } } rebalance(tmp); return(0); } //建立删除函数 int del(tree_node_t *tree,int key) { tree_node_t *tmp,*upper,*other; if(tree->left==NULL) return(-1); else if(tree->right==NULL) { if(tree->key==key) { tree->left=NULL; return(0); } else return(-1); } else { tmp=tree; stack_clear(); while(tmp->right!=NULL) { upper=tmp; push(upper); if(key<tmp->key) { tmp=upper->left; other=upper->right; } else { tmp=upper->right; other=upper->left; } } if(tmp->key!=key) return(-1); else { upper->key=other->key; upper->left=other->left; upper->right=other->right; upper->height=upper->height-1; return_node(tmp); return_node(other); rebalance(pop()); //here must pop,then rebalance can run from the parrent of upper,because upper has become a leaf. return(0); } } } //建立测试遍历函数 int travel(tree_node_t *tree) { stack_clear(); if(tree->left==NULL) push(tree); else if(tree->left!=NULL) { int m=0; push(tree); while(i!=m) { if(stack[m]->left!=NULL && stack[m]->right!=NULL) { push(stack[m]->left); push(stack[m]->right); } m++; } } return(0); } //建立测试函数 int test_structure(tree_node_t *tree) { travel(tree); int state=-1; while(!stack_empty()) { --i; if(stack->right==NULL && stack->height==0) //this statement is leaf,but also contains an empty tree state=0; else if(stack->left!=NULL && stack->right!=NULL) { if(abs(stack->height-stack->height)<=1) state=0; else { state=-1; stack_clear(); } } } stack_clear(); return(state); } //建立remove_tree函数 int remove_tree(tree_node_t *tree) { travel(tree); if(stack_empty()) return(-1); else { while(!stack_empty()) { return_node(pop()); } return(0); } } void main() { tree_node_t *atree=NULL; obj_node_t obj[256],*f; //MAXSIZE=n(number of leaf)+(n-1) number of node int n,j=0; cout<<"Now Let's start this program! There is no tree in memory. "; int item; while(item!=0) { cout<<" Root address = "<<atree<<" "; cout<<" 1.Create a tree "; cout<<" 2.Insert a int type object "; cout<<" 3.Test the structure of the tree "; cout<<" 4.Find a object "; cout<<" 6.Delete a object "; cout<<" 7.Remove the Tree "; cout<<" 0.Exit "; cout<<" Please select:"; cin>>item; cout<<" "; switch(item) { case 1: { atree=creat_tree(); cout<<" A new empty tree has been built up! "; break; } case 2: { if(atree!=NULL) { while(n!=3458) { cout<<"Please insert a new object. Only one object every time(3458 is an end code) : "; cin>>n; if(n!=3458) { obj[j].obj_key=n; if(insert(atree,&obj[j])==0) { j++; cout<<"Integer "<<n<<" has been input! "; } else cout<<" Insert failed! "; } } } else cout<<" No tree in memory,insert Fail! "; break; } case 3: { if(atree!=NULL) { n=test_structure(atree); if(n==-1) cout<<" It's not a correct AVL tree. "; if(n==0) cout<<" It's a AVL tree "; } else cout<<" No tree in memory,Test Fail! "; break; } case 4: { if(atree!=NULL) { cout<<" What do you want to find? : "; cin>>n; f=find(atree,n); if(f==NULL) { cout<<" Sorry,"<<n<<" can't be found! "; } else { cout<<" Object "<<f->obj_key<<" has been found! "; } } else cout<<" No tree in memory,Find Fail! "; break; } case 5: { if(atree!=NULL) { travel(atree); for(int count=0;count<i;count++) { cout<<" "<<stack[count]->key<<","; } } else cout<<" No tree in memory,Travel Fail! "; break; } case 6: { if(atree!=NULL) { cout<<" Which object do you want to delete? "; cin>>n; if(del(atree,n)==0) { cout<<" "<<n<<" has been deleted! "; } else cout<<" No this object "; } else cout<<" No tree in memory,Delete Fail! "; break; } case 7: { if(atree!=NULL) { remove_tree(atree); cout<<" The Tree has been removed! "; atree=NULL; } else cout<<" No tree in memory,Removing Fail! "; break; } default: cout<<" No this operation! "; } n=0; } }

      

  • 相关阅读:
    int vs Integer
    贫民窟里的WPF系列讲座(一)
    微软虚拟化技术构建高效开发与测试环境(四)
    贫民窟里的WPF系列讲座(二)
    WPF的WEBCAST内容
    微软虚拟化技术构建高效开发与测试环境(三)
    微软虚拟化技术构建高效开发与测试环境(五)
    微软虚拟化技术构建高效开发与测试环境(二)
    手把手教用XNA开发winphone7游戏(一)
    手把手教用XNA开发winphone7游戏(二)
  • 原文地址:https://www.cnblogs.com/tslDream/p/4756045.html
Copyright © 2020-2023  润新知