二叉树常用方法

二叉树定义: 

struct Node{
	int data;
	Node* lchild;
	Node* rchild;
};

二叉树创建

1:(通过输入数组,排序二叉树为例):

//创建新节点
Node * newNode(int x){
	Node *root=new Node;
	root->data=x;
	root->lchild=root->rchild=NULL;
	return root;
}
//插入
void insert(Node* &root,int x){
	if(root==NULL) root=newNode(x);
	else if(x<root->data) {
		insert(root->lchild,x);
	}else {
		insert(root->rchild,x);
	}
}
//构建
Node * create(int a[],int n){
	Node *root=NULL;
	for(int i=0;i<n;i++){
		insert(root,a[i]);
	}
	return root;
}

2:通过先序序列和中序序列创建

Node * create(int preL,int preR,int inL,int inR){
	if(preL>preR)return NULL;
	Node* root=new Node;
	root->data=pre[preL];
	int k;
	for(k=inL;k<=inR;k++){
		if(in[k]==pre[preL]){
			break;
		}
	}
	int numLeft=k-inL;
	root->lchild=create(preL+1,preL+numLeft,inL,k-1);
	root->rchild=create(preL+numLeft+1,preR,k+1,inR);
	return root;
}

判断两棵二叉树是否一致:

bool compare(Node* root1,Node* root2){
	if(root1==NULL&&root2==NULL)return true;
	if((root1!=NULL&&root2==NULL)||(root2!=NULL&&root1==NULL))return false;
	if(root1->data!=root2->data)return false;
	return compare(root1->lchild,root2->lchild)&&compare(root1->rchild,root2->rchild);	
}

判断两个节点是否为兄弟节点:

bool judgeSibling(Node* root, int a,int b){
	if(root==NULL) return false;
	if(root->lchild!=NULL&&root->rchild!=NULL){
		if((root->lchild->data==a&&root->rchild->data==b)||(root->lchild->data==b&&root->rchild->data==a)){
			return true;
		}
	}
	return judgeSibling(root->lchild,a,b)?true:judgeSibling(root->rchild,a,b);
}

判断节点a是否是节点b的父亲节点:

bool judgeParent(Node* root,int a,int b){
	if(root==NULL)return false;
	if(root->data==a){
		if(root->lchild!=NULL&&root->lchild->data==b){
			return true;
		}else if(root->rchild!=NULL&&root->rchild->data==b){
			return true;
		}
	}
	return judgeParent(root->lchild,a,b)?true:judgeParent(root->rchild,a,b);
}

获取节点的深度:

int getLayer(Node* root,int x,int deep){
	if(root==NULL)return-1;
	if(root->data==x)return deep;
	int temp=getLayer(root->lchild,x,deep+1);
	return 	temp==-1?getLayer(root->rchild,x,deep+1):temp;
}

判断两个节点是否在同一层:

bool judgeLayer(Node* root,int a,int b){
	int A=getLayer(root,a,1);
	int B=getLayer(root,b,1);
	if(A==B&&A!=-1)return true;
	return false;
}

平衡二叉树AVL(定义,构建):

//节点定义
struct Node{
	int data;
	int height;//高度
	Node* lchild;
	Node* rchild; 
};

//创建新节点
Node * newNode(int x){
	Node *root=new Node;
	root->data=x;
	root->height=1;//初始为1; 
	root->lchild=root->rchild=NULL;
	return root;
}

int getHeight(Node* root){
	if(root==NULL)return 0;
	return root->height;
}
//获取节点平衡因子
int getBalanceFactor(Node* root){
	return getHeight(root->lchild)-getHeight(root->rchild);
}
//更新节点高度
int updateHeight(Node* root){
	root->height=max(getHeight(root->lchild),getHeight(root->rchild))+1;
}

//左旋
void L(Node* &root){
	Node* temp=root->rchild;
	root->rchild=temp->lchild;
	temp->lchild=root;
	updateHeight(root);
	updateHeight(temp);
	root=temp;
} 
//右旋
void R(Node* &root){
	Node* temp=root->lchild;
	root->lchild=temp->rchild;
	temp->rchild=root;
	updateHeight(root);
	updateHeight(temp);
	root=temp;
} 
//插入
void insert(Node* &root,int v){
	if(root==NULL){
		root=newNode(v);
		return;
	}
	if(v<root->data){
		insert(root->lchild,v);
		updateHeight(root);
		if(getBalanceFactor(root)==2){
			if(getBalanceFactor(root->lchild)==1){//LL型
				R(root);
			}else if(getBalanceFactor(root->lchild)==-1){//LR型
				L(root->lchild);
				R(root);
			}
		}
	}else{
		insert(root->rchild,v);
		updateHeight(root);
		if(getBalanceFactor(root)==-2){
			if(getBalanceFactor(root->rchild)==-1){//RR型
				L(root);
			}else if(getBalanceFactor(root->rchild)==1){//RL型
				R(root->rchild);
				L(root);
			}
		} 
	}
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值