国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 學(xué)院 > 開(kāi)發(fā)設(shè)計(jì) > 正文

AVL Tree平衡二叉樹

2019-11-14 09:20:18
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友
#include <iostream>#include <cstdio>using namespace std;// AVLTree平衡二叉樹(Balanced Binary Tree)/高度平衡的二叉查找樹 // 插入、刪除 typedef int DataType;typedef struct node{	DataType data;	int bf;				// 平衡因子 	struct node *lchild, *rchild;}AVLNode, *AVLTree;// 右單旋轉(zhuǎn)(LL旋轉(zhuǎn)): *a的左子樹的左子樹上 插入新結(jié)點(diǎn) void RotateLL(AVLNode *&a){	AVLNode *b = a->lchild;	a->lchild = b->rchild;	b->rchild = a;	b->bf = a->bf = 0;	a = b;}// 左單旋轉(zhuǎn)(RR旋轉(zhuǎn)): *a的右子樹的右子樹上 插入新結(jié)點(diǎn) void RotateRR(AVLNode *&a){	AVLNode *b = a->rchild;	a->rchild = b->lchild;	b->lchild = a;	b->bf = a->bf = 0;	a = b;}// 先左后右雙旋轉(zhuǎn)(LR旋轉(zhuǎn)): *a的左子樹的右子樹上 插入新結(jié)點(diǎn) void RotateLR(AVLNode *&a){	AVLNode *b = a->lchild, *c = b->rchild;	b->rchild = c->lchild;				// 左單旋轉(zhuǎn) 	c->lchild = b;						// 左單旋轉(zhuǎn) 	if(c->bf <= 0)		b->bf = 1;	else		b->bf = 0;	a->lchild = c->rchild;				// 右單旋轉(zhuǎn) 	c->rchild = a;						// 右單旋轉(zhuǎn) 	if(c->bf == -1)		a->bf = 0;	else		a->bf = -1;	c->bf = 0;	a = c;}// 先右后左雙旋轉(zhuǎn)(RL旋轉(zhuǎn)): *a的右子樹的左子樹上 插入新結(jié)點(diǎn) void RotateRL(AVLNode *&a){	AVLNode *b = a->rchild, *c = b->lchild;	b->lchild = c->rchild;				// 右單旋轉(zhuǎn) 	c->rchild = b;						// 右單旋轉(zhuǎn) 	if(c->bf >= 0)		b->bf = -1;	else		b->bf = 0;	a->rchild = c->lchild;				// 左單旋轉(zhuǎn) 	c->lchild = a;						// 左單旋轉(zhuǎn) 	if(c->bf == 1)		a->bf = 0;	else		a->bf = 1;	c->bf = 0;	a = c;}// 先序遞歸遍歷 void PReOrder(AVLTree root){	if(root != NULL)	{		printf("%4d", root->data);		PreOrder(root->lchild);		PreOrder(root->rchild);	}}void PreBF(AVLTree root)	// 先序遍歷平衡因子 {	if(root != NULL)	{		printf("%4d", root->bf);		PreBF(root->lchild);		PreBF(root->rchild);	}}void InOrder(AVLTree root)	// 中序遞歸遍歷 {	if(root != NULL)	{		PreOrder(root->lchild);		printf("%4d", root->data);		PreOrder(root->rchild);	}}// 計(jì)算樹的平衡因子 int count(AVLNode *r){	if(r->lchild == NULL && r->rchild == NULL)	{		r->bf = 0;		return 1;	}	else	{		int lhigh, rhigh;		if(r->lchild != NULL)		{			lhigh = count(r->lchild);		}		else			lhigh = 0;				if(r->rchild != NULL)		{			rhigh = count(r->rchild);		}		else			rhigh = 0;				r->bf = lhigh - rhigh;		// 因子 = 左子樹高度 - 右子樹高度 		if(lhigh > rhigh)			return lhigh + 1;		else			return rhigh + 1;	}}// 一個(gè)結(jié)點(diǎn)一個(gè)結(jié)點(diǎn)地添加/刪除,然后調(diào)整,所以平衡因子不會(huì)超過(guò)2/-2 // 在root樹種,添加值為x的結(jié)點(diǎn),并且調(diào)整成為平衡二叉樹 AVLNode* InsertAndBalance(AVLTree &root, DataType x){	AVLNode *s, *p, *f;		if(root == NULL)	{		root = new AVLNode;		if(root == NULL)			return NULL;		root->data = x;		root->bf = 0;		root->lchild = NULL;		root->rchild = NULL;		return root;	}	else	{		if(x > root->data)		{			root->rchild = InsertAndBalance(root->rchild, x);						count(root);	// 修改結(jié)點(diǎn)的bf值,并返回高度(此處可以優(yōu)化,需要重新遞歸求平衡因子) 						if(root->bf == 2)		// 如果不平衡,進(jìn)行旋轉(zhuǎn) 			{				if(root->lchild->bf == 1)					RotateLL(root);				else if(root->lchild->bf == -1)					RotateLR(root);				count(root);			}			else if(root->bf == -2)			{				if(root->rchild->bf == -1)					RotateRR(root);				else if(root->rchild->bf == 1)					RotateRL(root);				count(root);			}					}		else if(x < root->data)		{			root->lchild = InsertAndBalance(root->lchild, x);						count(root);	// 修改結(jié)點(diǎn)的bf值,并返回高度(此處可以優(yōu)化,需要重新遞歸求平衡因子) 						if(root->bf == 2)		// 如果不平衡,進(jìn)行旋轉(zhuǎn) 			{				if(root->lchild->bf == 1)					RotateLL(root);				else if(root->lchild->bf == -1)					RotateLR(root);				count(root);			}			else if(root->bf == -2)			{				if(root->rchild->bf == -1)					RotateRR(root);				else if(root->rchild->bf == 1)					RotateRL(root);				count(root);			}					}			}		return root;}// 在root樹種,刪除結(jié)點(diǎn)x,并且調(diào)整成為平衡二叉樹 AVLNode* RemoveAndBalance(AVLTree &root, DataType x){	AVLNode *s, *p, *f;		if(root == NULL)	{		return root;	}	else	{		if(x > root->data)		{			root->rchild = RemoveAndBalance(root->rchild, x);						count(root);	// 修改結(jié)點(diǎn)的bf值,并返回高度(此處可以優(yōu)化,需要重新遞歸求平衡因子) 						if(root->bf == 2)		// 如果不平衡,進(jìn)行旋轉(zhuǎn) 			{				if(root->lchild->bf == 1)					RotateLL(root);				else if(root->lchild->bf == -1)					RotateLR(root);				count(root);			}			else if(root->bf == -2)			{				if(root->rchild->bf == -1)					RotateRR(root);				else if(root->rchild->bf == 1)					RotateRL(root);				count(root);			}					}		else if(x < root->data)		{			root->lchild = RemoveAndBalance(root->lchild, x);						count(root);	// 修改結(jié)點(diǎn)的bf值,并返回高度(此處可以優(yōu)化,需要重新遞歸求平衡因子) 						if(root->bf == 2)		// 如果不平衡,進(jìn)行旋轉(zhuǎn) 			{				if(root->lchild->bf == 1)					RotateLL(root);				else if(root->lchild->bf == -1)					RotateLR(root);				count(root);			}			else if(root->bf == -2)			{				if(root->rchild->bf == -1)					RotateRR(root);				else if(root->rchild->bf == 1)					RotateRL(root);				count(root);			}					}		else if(x == root->data)		{			AVLNode *s, *f;			if(root->lchild == NULL && root->rchild == NULL)	// 葉子的時(shí)候 			{				delete root;				return NULL;			}			else if(root->lchild == NULL && root->rchild != NULL)			{				s = root->rchild;				delete root;				return s;			}			else if(root->lchild != NULL && root->rchild == NULL)			{				s = root->lchild;				delete root;				return s;			}			else if(root->lchild != NULL && root->rchild != NULL)			{				s = root->lchild;				if(s->rchild != NULL)				{					while(s->rchild != NULL)					{						f = s;						s = s->rchild;					}					f->rchild = NULL;					root->data = s->data;					if(s->lchild != NULL)					{						if(f->data > s->lchild->data)							f->lchild = s->lchild;						else							f->rchild = s->lchild;					}				}				else		// 左子樹沒(méi)有右結(jié)點(diǎn)時(shí) 				{					root->data = s->data;					root->lchild = s->lchild;				}				delete s;				return root;			}		}	}		return root;}int main(){	int high;	AVLNode *s, *f;	AVLTree root = NULL;	InsertAndBalance(root, 53);	InsertAndBalance(root, 17);	InsertAndBalance(root, 9);	InsertAndBalance(root, 45);	InsertAndBalance(root, 23);	InsertAndBalance(root, 78);	InsertAndBalance(root, 65);	InsertAndBalance(root, 66);	InsertAndBalance(root, 67);	//InsertAndBalance(root, 94);	//InsertAndBalance(root, 81);	//InsertAndBalance(root, 88);	//high = count(root);	//cout << "Tree high is " << high << endl;	cout << "數(shù)值: ";	PreOrder(root);	cout << endl;	cout << "因子: ";	PreBF(root);	cout << endl << endl;		RemoveAndBalance(root, 45);	RemoveAndBalance(root, 9);	cout << "先序遍歷: ";	PreOrder(root);	cout << endl;	cout << "中序遍歷: ";	InOrder(root);	cout << endl;	cout << "平衡因子: ";	PreBF(root);	cout << endl << endl;		RemoveAndBalance(root, 17);	RemoveAndBalance(root, 53);	InsertAndBalance(root, 94);	cout << "先序遍歷: ";	PreOrder(root);	cout << endl;	cout << "中序遍歷: ";	InOrder(root);	cout << endl;	cout << "平衡因子: ";	PreBF(root);	cout << endl << endl;			return 0;}
上一篇:

下一篇:STM32F4移植ucos_II

發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 宜君县| 余干县| 贺州市| 汉源县| 墨玉县| 临潭县| 文化| 治多县| 安庆市| 兰西县| 宣武区| 方山县| 昭苏县| 韩城市| 宝清县| 同仁县| 兴隆县| 临泽县| 洪江市| 东辽县| 鄂伦春自治旗| 开鲁县| 准格尔旗| 铜山县| 舞钢市| 麻城市| 德惠市| 三台县| 苗栗市| 会泽县| 大理市| 新河县| 麻城市| 长阳| 防城港市| 云龙县| 洞口县| 东丰县| 平定县| 乃东县| 麟游县|