二分查找树

普通二叉查找树

#include<iostream>
using namespace std;

/*AVL DataStructs*/
typedef struct AVLNode *AVLTree;
struct AVLNode
{
	int data;
	AVLTree left;
	AVLTree right;
};

/*insert AVLTree*/
AVLTree insertAVL(AVLTree t, int data)
{
	if (!t)
	{
		t = (AVLTree)malloc(sizeof(struct AVLNode));
		t->data = data;
		t->left = NULL;
		t->right = NULL;
	}
	else if (data < t->data)
	{
		t->left = insertAVL(t->left, data);
	}
	else if (data >= t->data)
	{
		t->right = insertAVL(t->right, data);
	}
	return t;
}

void printTree(AVLTree t,AVLTree maxPoint1)
{
	if(t)
	{
		printTree(t->left,maxPoint1);
		if(t == maxPoint1)
			{cout<<t->data;}
		else
			{cout<<t->data<<" ";}
		printTree(t->right,maxPoint1); 
	}
}

AVLTree maxPoint(AVLTree t)
{
	while(t->right)
	{
		t=t->right;
	}
	return t;
}


int main()
{
	int num;
	cin >> num;
	AVLTree  t = NULL;
	for (int i = 0; i < num; i++)
	{
		int data;
		cin >> data;
		t = insertAVL(t, data);
	}
	printTree(t,maxPoint(t));
	
	return 0;
}



AVL树

#include<iostream>
using namespace std;

/*AVL DataStructs*/
typedef struct AVLNode *AVLTree;
struct AVLNode
{
	int data;
	AVLTree left;
	AVLTree right;
	int height;
};

int MAX(int a,int b)
{
	return a>b?a:b;
} 

/*height of tree*/
int getHeight(AVLTree t)
{
	if (!t)
	{
		return 0;
	}
	else
	{
		int hL = 0, hR = 0;
		if (t->left) hL = getHeight(t->left);
		if (t->right) hR = getHeight(t->right);
		return MAX(hL, hR) + 1;
	}

}

/*LL*/
AVLTree singleLRotation(AVLTree A)
{
	AVLTree B = A->left;
	A->left = B->right;
	B->right = A;
	A->height = MAX(getHeight(A->left), getHeight(A->right)) + 1;
	B->height = MAX(getHeight(B->left), getHeight(B->right)) + 1;
	return B;
}
/*RR*/
AVLTree singleRRotation(AVLTree A)
{
	AVLTree B = A->right;
	A->right = B->left;
	B->left = A;
	A->height = MAX(getHeight(A->left), getHeight(A->right)) + 1;
	B->height = MAX(getHeight(B->left), getHeight(B->right)) + 1;
	return B;
}

/*LR*/
AVLTree doubleLRRotation(AVLTree A)
{
	A->left = singleRRotation(A->left);
	return singleLRotation(A);
}

/*RL*/
AVLTree doubleRLRotation(AVLTree A)
{
	A->right = singleLRotation(A->right);
	return singleRRotation(A);
}

/*insert AVLTree*/
AVLTree insertAVL(AVLTree t, int data)
{
	if (!t)
	{
		t = (AVLTree)malloc(sizeof(struct AVLNode));
		t->data = data;
		t->height = 0;
		t->left = NULL;
		t->right = NULL;
	}
	else if (data < t->data)
	{
		t->left = insertAVL(t->left, data);
		if (getHeight(t->left) - getHeight(t->right) == 2)
		{
			if (data < t->left->data)
			{
				/*LL*/
				t = singleLRotation(t);
			}
			else
			{
				/*LR*/
				t = doubleLRRotation(t);
			}
		}
	}
	else if (data >= t->data)
	{
		t->right = insertAVL(t->right, data);
		if (getHeight(t->right) - getHeight(t->left) == 2)
		{
			if (data >= t->right->data)
			{
				/*RR*/
				t = singleRRotation(t);
			}
			else
			{
				/*RL*/
				t = doubleRLRotation(t);
			}
		}
	}

	t->height = MAX(getHeight(t->left), getHeight(t->right)) + 1;
	return t;
}

void printTree(AVLTree t,AVLTree maxPoint1)
{
	if(t)
	{
		printTree(t->left,maxPoint1);
		if(t == maxPoint1)
			{cout<<t->data;}
		else
			{cout<<t->data<<" ";}
		printTree(t->right,maxPoint1); 
	}
}

AVLTree maxPoint(AVLTree t)
{
	while(t->right)
	{
		t=t->right;
	}
	return t;
}


int main()
{
	int num;
	cin >> num;
	AVLTree  t = NULL;
	for (int i = 0; i < num; i++)
	{
		int data;
		cin >> data;
		t = insertAVL(t, data);
	}
	printTree(t,maxPoint(t));
	
	return 0;
}



转载于:https://my.oschina.net/willyliu/blog/366300

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值