二叉树操作合集

关于二叉树数据结构的一些操作合集(仅供参考)

#include<bits/stdc++.h>
using namespace std;
typedef struct treenode{
	int data;
	struct treenode *left,*right;
	int ltag,rtag;
}TNode,*Tree;

// 返回树的高度
int getHeight(TNode *p){
	if(!p)
		return 0;
	else return 1+max(getHeight(p->left),getHeight(p->right));
}

// 返回树的节点个数
int CountTreeNodes(Tree t){
	if(t==NULL)
		return 0;
	else return 1+CountTreeNodes(t->left)+CountTreeNodes(t->right);
}

// 判断是否是满二叉树 
bool isFullTree(Tree t){
	int n = getHeight(t);
	int nodes = CountTreeNodes(t);
	if(nodes == pow(2,n)-1)
		return true;
	else return false;
}
// --------------------------------------------------------------------------------------------------------//
// 平衡二叉树的调整

// 左左插入引起调整
void LL_Rotate(TNode *&A){
	TNode *B = A->left;
	A->left = B->right;
	B->right = A;
	A = B;
}

void RR_Rotate(TNode *&A){
	TNode *B = A->right;
	A->right = B->left;
	B->left = A;
	A = B;
}

void RL_Rotate(TNode *&A){
	LL_Rotate(A->right);
	RR_Rotate(A);
}

void LR_Rotate(TNode *&A){
	RR_Rotate(A->left);
	LL_Rotate(A);
}

// 平衡二叉树的插入
void InsertNode_AVL(Tree &t,int v){
	if(!t){
		t = (TNode *)malloc(sizeof(TNode));
		t->left = t->right = NULL;
		t->data = v;
		t->ltag = t->rtag = 0;
	}
	else if(v > t->data){
		InsertNode_AVL(t->right,v);
		if(getHeight(t->right)-getHeight(t->left)==2){
			if(v>t->right->data)
				RR_Rotate(t);
			else RL_Rotate(t);
		}
	}else{
		InsertNode_AVL(t->left,v);
		if(getHeight(t->left)-getHeight(t->right)==2){
			if(v < t->left->data)
				LL_Rotate(t);
			else LL_Rotate(t);
		}
	}
}
// 确定二叉树的最大宽度
int GetMaxWidth(Tree t){
	queue <TNode*> q;
	int maxwidth = 0;
	int width = 0;
	if(t){
		maxwidth = width =1;
		q.push(t);
		while(!q.empty()){
			TNode *p = q.front();
			q.pop();
			if(p->left)
				q.push(p->left);
			if(p->right)
				q.push(p->right);
			maxwidth = max(maxwidth,(int)q.size());
		}
	}
	return maxwidth;
}


// 打印二叉树
void printTree(Tree t){
	static int level = -1; //记录是第几层次
	int i;

	if (NULL == t)
		return;

	level++;
	printTree(t->right);
	level--;

	level++;
	for (i = 0; i < level; i++)
		printf("\t");
	printf("%2d\n", t->data);
	printTree(t->left);
	level--;
}

// 删除二叉排序数的节点
void DeleteNode(TNode *&t){
	if(t->left == t->right && t->left==NULL){
		free(t);
	}
	else if(t->left==NULL && t->right){
		// 接上右子树
		*(t) = *(t->right);
	}
	else if(t->right==NULL && t->left){
		// 接上左子树
		*(t) = *(t->left);
	}
	else{
		// 左右子树都需要考虑
		/*有两种方法,用该结点的直接前驱或直接后继(中序遍历)与该节点交换,再将交换后的直接前驱或直接后继删除即可*/
		// cout<<"Both!"<<endl;
		TNode *p = t->left,*pre = t->left;
		int value;
		while(p->right){
			pre = p;
			p=p->right;
		}
		pre->right = NULL;
		value = t->data;
		t->data = p->data;
		p->data = value;
		free(p);
	}
}

//非平衡二叉排序树的插入
void InsertNode(Tree &t,int v){
	if(t==NULL){
		t = (TNode *)malloc(sizeof(TNode));
		t->left = t->right = NULL;
		t->data = v;
		t->ltag = t->rtag = 0;
	}
	else{
		if(v > t->data)
			InsertNode(t->right,v);
		else InsertNode(t->left,v);
	}	
}

//二叉树中序线索化
void InThread(Tree &t,TNode *&pre){
	if(t){
		InThread(t->left,pre);
		if(t->left==NULL){
			t->left = pre;
			t->ltag = 1;
		}
		if(pre!=NULL && pre->right==NULL){
			pre->right = t;
			pre->rtag = 1;
		}
		pre = t;
		InThread(t->right,pre);
	}
}

// 返回指定值的节点
TNode * Serch(Tree t,int v){
	if(t){
		if(t->data == v)
			return t;
		if(v < t->data)
			return Serch(t->left,v);
		else return Serch(t->right,v);
	}
	else return NULL;
}

// 线索二叉树的下一个节点
TNode * next(TNode *&p){
	if(p){
		if(p->rtag==0){
		p = p->right;
		while(p->ltag==0)
			p=p->left;
					}
		else p = p->right;
	}
	return p;
}

// 线索二叉树的上一个节点
TNode * pre_node(TNode *&p){
	if(p){
		if(p->ltag==0){
			p=p->left;
			while(p->rtag==0)
			p=p->right;
					}
		else p=p->left;	
	}
	return p;
}


// 二叉树的遍历 非递归版本
//--------------------------------------------------------------------------------------------------------------------//
// 先序 根左右
void PreOrder(Tree t){
	stack<TNode *> s;
	while(t || !s.empty()){
		while(t){
			cout << t->data << ' ';
			s.push(t);
			t = t->left;
		}
		if(!s.empty()){
			TNode *p = s.top();
			s.pop();
			t = p->right;
		}
	}
	cout << endl;
}
// 中序 左根右 如果是排序树此方法输出的序列是有序的
void InOrder(Tree t){
	stack<TNode*> s;
	if(!t)
		return;
	while(t || !s.empty()){
		while(t){
			s.push(t);
			t = t->left;
		}
		if(!s.empty()){
			TNode *p = s.top();
			cout << p->data << ' ';
			s.pop();
			t = p->right;
		}
	}
	cout << endl;
}
// 后序 左右根
void PostOrder(Tree t){
	stack<TNode *> s;
	if(!t)
		return;
	TNode * cur;
	TNode *pre = NULL;
	s.push(t);
	while(!s.empty()){
		cur = s.top();
		if((cur->left==NULL && cur->right==NULL)||
			(pre!=NULL&&(pre==cur->left||pre==cur->right)))
		{
			cout << cur->data<<' ';
			s.pop();
			pre = cur;
		}
		else{
			if(cur->right)
				s.push(cur->right);
			if(cur->left)
				s.push(cur->left);
		}
	}
}



// 返回一个随机的序列
int *randList(int size,int type){
	int *b = new int[size];
	if(type==0){
		for(int i=0;i<size;i++)
		b[i] = rand()%size;
	}
		
	else{
		for(int i=1;i<=size;i++)
			b[i-1] = i;
	}
	return b;
}


int main(int argc, char const *argv[]){
	int size = 20;
	int *a = randList(size,1);
	// MergeSort(a,0,size-1);
	Tree t=NULL;
	for(int i=0;i<size;i++){
		cout << a[i] << " ";
		InsertNode_AVL(t,a[i]);
		// InsertNode(t,a[i]);
	}
	cout<<endl;
	// cout<<isFullTree(t)<<endl;
	// TNode *p = Serch(t,8);
	printTree(t);
	// InOrder(t);
	// cout <<"P: "<<p->data<<endl;
	// DeleteNode(p);
	// printTree(t);
	// TNode *pre = NULL;
	// InThread(t,pre);
	
	// cout<<"P->next: "<<next(p)->data<<endl;
	// cout<<"P->pre: "<<pre_node(p)->data<<endl;
	cout<<"The maxwidth:"<<GetMaxWidth(t)<<endl;
	PreOrder(t);
	InOrder(t);
	PostOrder(t);
	return 0;
}
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值