树与二叉树(一)

1.树的定义与性质

树是一种数据结构,是由有限结点组成的具有层次关系的集合。每个元素称为一个结点,两个结点之间通过边连接,结点向下延伸出子结点。没有父结点的结点是根结点(root),每个结点有0个或多个子结点,每个非根结点有且只有一个父结点
在这里插入图片描述

树有以下基本性质:
1.树可以没有结点,没有结点称为空树
2.树的层次要从根结点算起,根结点是第一层
3.结点的子树个数(子结点个数)称为结点的,一棵树中结点最大的度称为树的度
4.树中不存在环,边数一定等于结点数-1
5.度为0的结点称为叶子结点,当树中只有根结点一个结点时,根结点也是叶子结点
6.结点的深度(和层数一样):从根结点(深度为1)自顶向下逐层累加到该结点的值;结点的高度:从最底层的叶子结点(高度为1)自底向上逐层累加到该结点的值。树的深度和数的高度分别是结点的最大深度和最大高度,因此树的深度和高度是相等的。
7.若干棵树的集合称为森林

2.二叉树

二叉树是每个结点最多有两个子树的树结构,且有左右之分
二叉树与度为2的树的区别:两种树结构的结点的子结点个数都不超过2,但是二叉树是严格区分左右子树的,不能随意交换左右子树的位置。
两种特殊的二叉树:
1.满二叉树:每一层的结点个数都达到了当层能达到的最大结点数
在这里插入图片描述
2.完全二叉树:除了最下面一层,每一层的结点个数都达到了当层能达到的最大结点数,且最下层的叶子结点集中在树的左侧
在这里插入图片描述

3.二叉树的存储结构和基本操作

1.存储结构
二叉树一般使用链表,与一般链表的区别在于每一个结点有两个指针域,分别指向左子树和右子树的根结点的地址,如果某个子树不存在则指向NULL。

struct node{
	typename data;
	node *lchild;
	node *rchild;
};

创建一个新结点:

node* newNode(int data){
	node *p=new node;
	p->data=data;
	p->lchild=NULL;
	p->rchild=NULL;
	return p;    //返回新创建结点的地址
}

2.结点的查找与修改

//查找数据域为x的结点并将数据修改为newdata
void search(node *root,int x,int newdata){
	if(root==NULL)
		return;    //空树(递归边界)
	if(root->data==x)
		root->data=newdata;
	search(root->lchild,x,newdata);    //搜索左子树
	search(root->rchild,x,newdata);    //搜索右子树
}

3.结点的插入

void insert(node* &root,int x){
	if(root==NULL){
		root=newNode(x);
		return;
	}
	if(...){
		insert(root->lchild,x);
	}
	else{
		insert(root->rchild,x);
	}
}

传递根结点指针时使用了引用&,目的是修改原变量(将新建节点的地址赋给原root)

4.二叉树的创建

node* create(int data[],int n){
	node *root=NULL;
	for(int i=0;i<n;i++){
		insert(root,data[i]);
	}
	return root;
}

5.完全二叉树的存储结构
对于一颗完全二叉树,除了二叉链表的存储方式,还可以使用更方便的编号方式:根结点编号为1,从上到下从左到右依次对其他节点进行编号。假设一个节点编号为x,那么它的左孩子节点编号一定为2x,右孩子一定为2x+1。
在一颗结点个数为n的完全二叉树中,判断编号为x的结点是否为叶子结点的标志:2x>n;判断是否为空结点的标志:x>n。

4.二叉树的遍历

1.先序遍历
遍历顺序:根结点->左子树->右子树
先序遍历中序列的第一个一定是根结点

void pre_order(node *root){
	if(root==NULL)
		return;
	printf("%d\n",root->data);  //访问根结点root
	pre_order(root->lchild);
	pre_order(root->rchild);
}

2.中序遍历
遍历顺序:左子树->根结点->右子树
中序遍历中只要知道根结点,就可以确定左子树和右子树

void in_order(node *root){
	if(root==NULL)
		return;
	in_order(root->lchild);
	printf("%d\n",root->data);
	in_order(root->rchild);
}

3.后序遍历
遍历顺序:左子树->右子树->根结点
后序遍历中序列最后一个一定是根结点

void post_order(node *root){
	if(root==NULL)
		return;
	post_order(root->lchild);
	post_order(root->rchild);
	printf("%d\n",root->data);
}

4.层序遍历
从根节点开始以逐层向下,层内从左到右的顺序遍历二叉树(BFS)

void layer_order(node *root){
	queue<node*> q;
	q.push(root);
	while(!q.empty()){
		node *t=q.front();
		q.pop();
		printf("%d\n",t->data);
		if(t->lchild!=NULL)
			q.push(t->lchild);
		if(t->rchild!=NULL)
			q.push(t->rchild);
	}
}

如果要计算结点的层数:

struct node{
	node *lchild;
	node *rchild;
	int data;
	int layer;    //层数
};
void layer_order(node *root){
	queue<node*> q;
	q.push(root);
	root->layer=1;    //根结点层数为1
	while(!q.empty()){
		node *t=q.front();
		q.pop();
		printf("%d\n",t->data);
		if(t->lchild!=NULL){
			q.push(t->lchild);
			t->lchild->layer=t->layer+1;
		}
		if(t->rchild!=NULL){
			q.push(t->rchild);
			t->rchild->layer=t->layer+1;
		}
	}
}

5.通过中序遍历和先序遍历重建二叉树
给出先序遍历pre[preL,preR]和中序遍历in[inL,inR],通过先序遍历可以得到二叉树的根结点pre[preL],接下来就可以在中序遍历中找到根结点的位置k,中序序列中:左子树区间为[inL,k-1],右子树区间为[k+1,inR];先序序列中:左子树区间为[preL+1,preL+k-inL],右子树区间为[preL+k-inL+1,preR]。

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;
	}  //左子树结点个数:k-inL
	root->lchild=create(preL+1,preL+k-inL,inL,k-1);
	root->rchild=create(preL+k-inL+1,preR,k+1,inR);
	return root;
}

中序遍历可以搭配先序、后序、层序中的任意一个重建一颗唯一的二叉树,但是后三者两两之间无法构建唯一的二叉树。

实例:
PAT 1020 Tree Traversals
Suppose that all the keys in a binary tree are distinct positive integers. Given the postorder and inorder traversal sequences, you are supposed to output the level order traversal sequence of the corresponding binary tree.

Input Specification:
Each input file contains one test case. For each case, the first line gives a positive integer N (≤30), the total number of nodes in the binary tree. The second line gives the postorder sequence and the third line gives the inorder sequence. All the numbers in a line are separated by a space.

Output Specification:
For each test case, print in one line the level order traversal sequence of the corresponding binary tree. All the numbers in a line must be separated by exactly one space, and there must be no extra space at the end of the line.

Sample Input:

7
2 3 1 5 7 6 4
1 2 3 4 5 6 7

Sample Output:

4 1 6 3 5 7 2

题意:二叉树每个结点都是值唯一的正整数,给出后序遍历和中序遍历,输出二叉树的层序遍历。

思路:后序遍历最后一个是根结点,由此可以找到中序遍历中的根结点,得到两种遍历中左右子树的区间,递归创建二叉树,最后层序遍历输出即可。

AC代码:

#include <stdio.h>
#include <queue>
using namespace std;

const int maxn=35;
int n;
int post[maxn],in[maxn];
struct node{
    int data;
    node *lchild;
    node *rchild;
};

node* create(int postL,int postR,int inL,int inR){
    if(postL>postR)
        return NULL;
    node *root=new node;
    root->data=post[postR];
    int k;
    for(k=inL;k<=inR;k++){
        if(in[k]==post[postR])
            break;
    }  //左子树结点个数:k-inL
    root->lchild=create(postL,postL+k-inL-1,inL,k-1);
    root->rchild=create(postL+k-inL,postR-1,k+1,inR);
    return root;
}

void layer_order(node *root){
    queue<node*> q;
    q.push(root);
    while(!q.empty()){
        node *t=q.front();
        q.pop();
        if(t->data==root->data)
            printf("%d",t->data);
        else
            printf(" %d",t->data);
        if(t->lchild!=NULL)
            q.push(t->lchild);
        if(t->rchild!=NULL)
            q.push(t->rchild);
    }
}

int main(){
    scanf("%d",&n);
    for(int i=1;i<=n;i++)
        scanf("%d",&post[i]);
    for(int i=1;i<=n;i++)
        scanf("%d",&in[i]);
    node *root=create(1,n,1,n);
    layer_order(root);
    return 0;
}

5.二叉树的静态实现

1.存储结构
静态二叉链表是指结点的左右指针域使用int型代替,用来表示左右子树的根结点在数组中的下标。

struct node{
	int data;
	int lchild;
	int rchild;
}Node[maxn];

2.生成新结点

int index=0;
int newNode(int data){
	Node[index].data=data;
	Node[index].lchild=-1;    //数组范围是0~maxn-1,可以用-1或maxn表示空
	Node[index].rchild=-1;
	return index++;
}

3.查找

//查找数据域为x的结点并将数据域改为newdata
void search(int root,int x,int newdata){
	if(root==-1)
		return;
	if(Node[root].data==x)
		Node[root].data=newdata;
	search(Node[root].lchild,x,newdata);
	search(Node[root].rchild,x,newdata);
}

4.插入结点

void insert(int &root,int x){
	if(root==-1){
		root=newNode(x);
		return;
	}
	if(...){
		insert(Node[root].lchild,x);
	}
	else{
		insert(Node[root].rchild,x);
	}
}

5.创建二叉树

int create(int data[],int n){
	int root=-1;
	for(int i=0;i<n;i++){
		insert(root,data[i]);
	}
	return root;
}

6.遍历

//先序遍历
void pre_order(int root){
	if(root==-1)
		return;
	printf("%d\n",Node[root].data);
	pre_order(Node[root].lchild);
	pre_order(Node[root].rchild);
}
//中序遍历
void in_order(int root){
	if(root==-1)
		return;
	in_order(Node[root].lchild);
	printf("%d\n",Node[root].data);
	in_order(Node[root].rchild);
}
//后序遍历
void post_order(int root){
	if(root==-1)
		return;
	post_order(Node[root].lchild);
	post_order(Node[root].rchild);
	printf("%d\n",Node[root].data);
}
//层序遍历
void layer_order(int root){
	queue<int> q;
	q.push(root);
	while(!q.empty()){
		int t=q.front();
		q.pop();
		printf("%d\n",Node[t].data);
		if(Node[t].lchild!=-1)
			q.push(Node[t].lchild);
		if(Node[t].rchild!=-1)
			q.push(Node[t].rchild);
	}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值