平衡二叉樹的的插入

平衡二叉樹的建立:

#include <iostream>
#include <vector>
#include <map>
using namespace std;

  struct TreeNode {
      int val;
	  int bf;
      TreeNode *left;
      TreeNode *right;
      TreeNode(int x) : val(x),bf(0), left(NULL), right(NULL) {}
 };
 
class Solution {
public:
    TreeNode *sortedArrayToBST(vector<int> &num) {
        TreeNode *root = NULL;
		for(size_t i=0;i<num.size();++i){
			root = createTree(root,num[i]);
		}
		printBST(root,2);
		return root;
    }
    
private:
   void printBST(TreeNode *root,int space){
	   if(root){
	      cout<<root->val<<endl;
		  if(root->left){
			  for(int i=0;i<space;++i)
			       cout<<" ";
		  }
		  printBST(root->left,space+2);
		  if(root->right) {
			  for(int i=0;i<space;++i)
				  cout<<" ";
		  }
		  printBST(root->right,space+2);
	   }
   }
   
   TreeNode *createNode(int val){
       TreeNode *node = new TreeNode(val);
       return node;
   }
   TreeNode *createTree(TreeNode *root,int val){
       TreeNode *node = createNode(val);
       TreeNode *p = root;
       if(NULL==root) {
           root = node;
           return root;
       }
       
       TreeNode *a=root,*fa=NULL;//a表示距離插入位置最近,且平衡因子不爲0的結點,fa表示a的父節點
       TreeNode *pre=NULL;//pre表示插入結點的前驅結點
	   while(p){//找到插入結點
		   if(p->bf){
		      fa=pre;
			  a=p;
		   }
		   pre = p;
           if(node->val >= p->val){//右轉
                p=p->right;
           }else{
               p=p->left;
           }
       }
       if(node->val < pre->val){//插入結點
           pre->left = node;
       }else{
           pre->right = node;
       }
	   p=a;
       while(p!=node){
		   if(node->val < p->val){
		       p->bf+=1;
			   p=p->left;
		   }else{
			   p->bf-=1;
			   p=p->right;
		   }					
	   }
	   if(a->bf==2 || a->bf==-2){
		   if(a->bf==2 && a->left->bf==1){//LL型,結點插入不平衡結點的左子結點的左子樹
			   p=LL_Rotate(a);
		   }else if(a->bf==2 && a->left->bf==-1){//LR型,結點插入不平衡結點的左子結點的右子樹
			   p=LR_Rotate(a);
		   }else if(a->bf==-2 && a->right->bf==-1){//RR型,結點插入不平衡結點的右子結點的右子樹
			   p=RR_Rotate(a);
		   }else if(a->bf==-2 && a->right->bf==1){//RL型,結點插入不平衡結點的右子結點的左子樹
			   p=RL_Rotate(a);
		   }
		   if(fa){
			   if(node->val < fa->val){
				   fa->left = p;
			   }else{
				   fa->right = p;
			   }
		   }else{
			   root = p;
		   }
	   }
       return root;
   }
private:
   TreeNode* LL_Rotate(TreeNode *a){
       TreeNode *ca=a->left;
	   a->left=ca->right;
	   ca->right=a;
	   a->bf=0;
	   ca->bf=0;
	   return ca;
   }
   TreeNode* LR_Rotate(TreeNode *a){
	   TreeNode *ca=a->left,*c=ca->right;
	   ca->right = c->left;
	   a->left = c->right;
	   c->left = ca;
	   c->right = a;
	   switch(c->bf){
	   case 1:ca->bf=0;a->bf=-1;break;//插入結點位於c的左子樹
	   case 0:ca->bf=0;a->bf=0;break;//c是插入結點
	   case -1:ca->bf=1;a->bf=0;break;//插入結點位於c的右子樹
	   }
	   c->bf=0;
	   return c;
   }
   TreeNode* RR_Rotate(TreeNode *a){
	   TreeNode *ca = a->right;
	   a->right = ca->left;
	   ca->left = a;
	   ca->bf=0;
	   a->bf=0;
	   return ca;
   }
   TreeNode* RL_Rotate(TreeNode *a){
	   TreeNode *ca=a->right,*c=ca->left;
	   ca->left = c->right;
	   a->right = c->left;
	   c->left = a;
	   c->right = ca;
	   switch(c->bf){
	   case 1:ca->bf=-1;a->bf=0;break;//插入結點位於c的左子樹
	   case 0:ca->bf=0;a->bf=0;break;//c是插入結點
	   case -1:ca->bf=0;a->bf=1;break;//插入結點位於c的右子樹
	   }
	   c->bf=0;
	   return c;
   }

};

int main()
{
	Solution s;
	int a[]={1,2,3,4,5,6,7,8,9};
	vector<int> v(a,a+9);
	s.sortedArrayToBST(v);
	return 0;
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章