PAT Template----Tree 2019.09.01

树的结构体定义+申请结点

typedef struct NODE{
	int data;
	struct NODE *left;
	struct NODE *right;
}NODE;

NODE *root = new NODE;

序列建树----遍历操作

/*
input:
n
preorder sequence
inorder sequence
postorder sequence

8
1 11 12 17 20 5 8 15
12 11 20 17 1 15 8 5
12 20 17 11 15 8 5 1

*/

#include<cstdio>
#include<iostream>
#define MAX 100
using namespace std;

typedef struct NODE{
	int data;
	struct NODE *left;
	struct NODE *right;
}NODE;

int n;
int pre[MAX];
int in[MAX];
int post[MAX];

NODE *create1(int preL,int preR,int inL,int inR)
{
	//the follow two sentences are all ok
	if(preL>preR)return NULL; 
	if(inL>inR)return NULL;
	
	int k;
	for(int i=0;i<n;i++)
	  if(in[i]==pre[preL])k=i;
	int leftNum=k-inL;
	
	NODE *root=new NODE;
	root->data=pre[preL];//don't forget
	root->left=NULL;
	root->right=NULL;
	root->left=create1(preL+1,preL+leftNum,inL,k-1);
	root->right=create1(preL+leftNum+1,preR,k+1,inR);
	
	return root;
}

NODE *create2(int postL,int postR,int inL,int inR)
{
	//the follow two sentences are all ok
	if(postL>postR)return NULL; 
	if(inL>inR)return NULL;
	
	int k;
	for(int i=0;i<n;i++)
	  if(in[i]==post[postR])k=i;
	int leftNum=k-inL;
	
	NODE *root=new NODE;
	root->data=post[postR];//don't forget
	root->left=NULL;
	root->right=NULL;
	root->left=create2(postL,postL+leftNum-1,inL,k-1);
	root->right=create2(postL+leftNum,postR-1,k+1,inR);
	
	return root;
}

void preorder(NODE *root)
{
	if(root==NULL)return;
	cout<<root->data<<" ";
	preorder(root->left);
	preorder(root->right);
}

void inorder(NODE *root)
{
	if(root==NULL)return;
	inorder(root->left);
	cout<<root->data<<" ";
	inorder(root->right);
}

void postorder(NODE *root)
{
	if(root==NULL)return;
	postorder(root->left);
	postorder(root->right);
	cout<<root->data<<" ";
}

void levelorder(NODE *root)
{
	NODE *que[MAX];
	int front=-1,rear=-1;
	que[++rear]=root;
	
	while(front!=rear)
	{
		NODE *node=que[++front];
		cout<<node->data<<" ";
		if(node->left!=NULL)que[++rear]=node->left;//pending NULL is essential
		if(node->right!=NULL)que[++rear]=node->right;//pending NULL is essential
	}
	
}

int main()
{
	cin>>n;
	for(int i=0;i<n;i++)cin>>pre[i];
	for(int i=0;i<n;i++)cin>>in[i];
	for(int i=0;i<n;i++)cin>>post[i];
	
	NODE *root1=create1(0,n-1,0,n-1);
	NODE *root2=create2(0,n-1,0,n-1);
	cout<<root1->data<<endl;
	cout<<root2->data<<endl;
	
	preorder(root1);cout<<endl;
	preorder(root2);cout<<endl;
	inorder(root1);cout<<endl;
	inorder(root2);cout<<endl;
	postorder(root1);cout<<endl;
	postorder(root2);cout<<endl;
	levelorder(root1);cout<<endl;
	levelorder(root2);cout<<endl;
}

BST建树

void insert(NODE *&root,int data)//注意此处用引用
{
	if(root==NULL)
	{
		root = new NODE;
		root->left=NULL;
		root->right=NULL;
		root->data=data;
		return;
	}
	if(data<=root->data)insert(root->left,data);
	if(data>root->data)insert(root->right,data);
}

int main()
{
	NODE *root=NULL;
	int n;cin>>n;
	for(int i=0;i<n;i++)
	{
		int data;cin>>data;
		insert(root,data);
	}
}

层序完全二叉树性质建树

第一行为完全二叉树的结点数量

第二行为完全二叉树的层次遍历

8
98 72 86 60 65 12 23 50
#include<cstdio>
#include<iostream>
#include<vector>
using namespace std;



typedef struct NODE{
	int data;
	struct NODE *left;
	struct NODE *right;
}NODE;
int n;

int t=0;
void postorder(NODE *root)
{
	if(root==NULL)return;
	postorder(root->left);
	postorder(root->right);
	cout<<root->data;t++;
	if(t!=n)cout<<" ";
}

int main()
{
	cin>>n;
        vector<NODE*>node;
	for(int i=0;i<n;i++)//init
	{
		NODE *newNode = new NODE;
		newNode->left=NULL;
		newNode->right=NULL;
		cin>>newNode->data;
		node.push_back(newNode);
	}
	for(int i=0;i<n;i++)//build tree
	{
		if((2*i+1)<n)node[i]->left=node[2*i+1];
		if((2*i+2)<n)node[i]->right=node[2*i+2];
	}

	NODE *root=node[0];

	t=0;
	postorder(root);cout<<endl;		
} 

AVL建树模板

此处LL RR LR RL都指旋转操作 eg:LL(左单旋转)

理解不了就背(反正只会考到建树)

#include<cstdio>
#include<iostream>
#define MAX 100
using namespace std;

typedef struct NODE{
	int data;
	struct NODE *left;
	struct NODE *right;
}NODE;

NODE *LL(NODE *root)
{
	NODE *t=root->right;
	root->right=t->left;
	t->left=root;
	return t;
}

NODE *RR(NODE *root)
{
	NODE *t=root->left;
	root->left=t->right;
	t->right=root;
	return t;
}

NODE *LR(NODE *root)
{
	root->left=LL(root->left);
	return RR(root);
}

NODE *RL(NODE *root)
{
	root->right=RR(root->right);
	return LL(root);
}

int getHeight(NODE *root)
{
	if(root==NULL)return 0;
	return max(getHeight(root->left),getHeight(root->right)) +1;
}

NODE *bstInsert(NODE *root,int data)
{
	if(root==NULL)
	{
		root=new NODE;
		root->data=data;
		root->left=NULL;
		root->right=NULL;
	}
	else if(data < root->data)
	{
		root->left=bstInsert(root->left,data);
	}
	else
	{
		root->right=bstInsert(root->right,data);
	}
	
	return root;
}

NODE *avlInsert(NODE *root,int data)
{
	if(root==NULL)
	{
		root=new NODE;
		root->data=data;
		root->left=NULL;
		root->right=NULL;
	}
	else if(data < root->data)
	{
		root->left=avlInsert(root->left,data);
		if(getHeight(root->left)-getHeight(root->right)==2)
		{
			if(data<root->left->data)root=RR(root);
		    else root=LR(root);	
		}

	}
	else
	{
		root->right=avlInsert(root->right,data);
		if(getHeight(root->left)-getHeight(root->right)==-2)
		{
			if(data>root->right->data)root=LL(root);
		    else root=RL(root);	
		}

	}
	
	return root;
}


void levelorder(NODE *root)
{
	NODE *que[MAX];
	int front=-1,rear=-1;
	que[++rear]=root;
	
	while(front!=rear)
	{
		NODE *node=que[++front];
		cout<<node->data<<" ";
		if(node->left!=NULL)que[++rear]=node->left;//pending NULL is essential
		if(node->right!=NULL)que[++rear]=node->right;//pending NULL is essential
	}
	
}


int main()
{
	int n;cin>>n;
	int data;
	
	NODE *bstRoot=NULL;
	NODE *avlRoot=NULL;
	
	for(int i=0;i<n;i++)
	{
		cin>>data;
		bstRoot=bstInsert(bstRoot,data);
		avlRoot=avlInsert(avlRoot,data);
	}
	
	levelorder(bstRoot);cout<<endl;
	levelorder(avlRoot);cout<<endl;
	
}

递归求树高

int getHeight(NODE *root)
{
	if(root==NULL)return 0;
	return max(getHeight(root->left),getHeight(root->right))+1;
}

层序判断完全二叉树

#include<cstdio>
#include<iostream>
#include<algorithm>
#define MAX 100
#include<vector>
using namespace std;

typedef struct NODE{
	int data;
	struct NODE *left;
	struct NODE *right;
}NODE;

//n means the amount of tree's node
int n;


bool levelorder(NODE *root)
{
	vector<int>level;//save levelorder sequence
	NODE *que[MAX];
	int front=-1,rear=-1;
        int rank[MAX]={0};int r=1;

	que[++rear]=root;
	rank[r]=root->data;
	
	while(front!=rear)
	{
		NODE *newNode=que[++front];
		level.push_back(newNode->data);
		
		if(newNode->left!=NULL)
		{
			que[++rear]=newNode->left;
			rank[++r]=newNode->left->data;
		}
		else r++;
		
		if(newNode->right!=NULL)
		{
			que[++rear]=newNode->right;
			rank[++r]=newNode->right->data;
		}
		else r++;
	}

    //output the level order sequence
	for(int i=0;i<level.size();i++)
	{
		if(i==0)cout<<level[i];
		else cout<<" "<<level[i];
	}
	
    //judge if the tree is a complete binary tree
    //if it is,return true,else return false
	for(int i=1;i<=n;i++)
	  if(rank[i]==0)return false;
	return true;
	
}


int main()
{
    //suppose the tree has benn built
    //and the root is the tree's root

	bool isComplete=levelorder(root);
	if(isComplete)cout<<endl<<"YES";
	else cout<<endl<<"NO";
	
}

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值