有关二叉树的基本操作:
1、初始化一个二叉树;
2,、建立一个二叉树;
3、在二叉树中建立一个节点;
4、在二叉树中删除一个节点;
5、在二叉树中查找一个节点;
6、二叉树的先序遍历;
7、二叉树的中序遍历;
8、二叉树的后序遍历;
9、二叉树的层序遍历;
10、销毁二叉树;等等
二叉树的重要性质:
性质一:二叉树的第i层上之多有pow(2,i-1)个节点;
性质二:深度为k的二叉树至多有pow(2,k)-1各节点
性质三:在任意二叉树中,若叶子节点为n,度为1的节点n1,度为二的节点n2,那么有n=n2+1;
先序遍历:
先序遍历的步骤:(1)访问根节点(2)先序遍历左子书(3)先序遍历右子树
先序遍历的递归算法如下:
void firstorder(Bnode *p)
{
if(p!=NULL)
{
printf("%c",p->data);
firstorder(p->lch);
firstorder(p->rch);
}
}
中序遍历:
中序遍历的步骤:(1)中序遍历左子树(2)访问根节点(3)中序遍历右子树
中序遍历的递归算法如下:
void middleorder(Bnode *p)
{
if(p)
{
middleorder(p->lch);
printf("%c",p->data);
middleorder(p->rch);
}
}
后序遍历:
后序遍历的步骤:(1)后序遍历左子树(2)后序遍历右子树(3)访问根节点
后序遍历的递归算法如下:
void lastorder(Bnode *p)
{
if(p!=NULL)
{
lastorder(p->lch);
lastorder(p->rch);
printf("%c",p->data);
}
}
层序遍历:层序遍历靠队列来实现
void levelorder(Bnode *root) // 队列实现
{
queue<Bnode*>q;
if(root!=NULL)
q.push(root);
while(!q.empty())
{
root=q.front();
q.pop();
if(root)
{
cout<<root->data;
q.push(root->l);
q.push(root->r);
}
}
}
来个题:
数据结构实验之二叉树二:遍历二叉树(原题链接)
Time Limit: 1000 ms Memory Limit: 65536 KiB
Problem Description
已知二叉树的一个按先序遍历输入的字符序列,如abc,,de,g,,f,,, (其中,表示空结点)。请建立二叉树并按中序和后序的方式遍历该二叉树。
Input
连续输入多组数据,每组数据输入一个长度小于50个字符的字符串。
Output
每组输入数据对应输出2行:
第1行输出中序遍历序列;
第2行输出后序遍历序列。
Sample Input
abc,,de,g,,f,,,
Sample Output
cbegdfa
cgefdba
Hint
Source
xam
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
struct node
{
char date;
struct node*lchild,*rchild;
};
char str[55];
int num;
void zhongxu(struct node*root)
{
if(root)
{
zhongxu(root->lchild);
printf("%c",root->date);
zhongxu(root->rchild);
}
}
void houxu(struct node*root)
{
if(root)
{
houxu(root->lchild);
houxu(root->rchild);
printf("%c",root->date);
}
}
struct node*create()//返回值为struct onde类型的指针;
{
struct node*root;
if(str[num]==',')
{
num++;
root=NULL;
}
else
{
root=(struct node*)malloc(sizeof(struct node));
root->date=str[num++];
root->lchild=create();
root->rchild=create();
}
return root;
}
int main()
{
struct node*root;
while(~scanf("%s",str))
{
num=0;
root=create();
zhongxu(root);
printf("\n");
houxu(root);
printf("\n");
}
}
已知一个二叉树的先序遍历和中序遍历来建立二叉树,这题与上题有所不同的是并不知道节点的左右孩子是否为空,但是我们已知两个条件就可以建立唯一的二叉树出来。
数据结构上机测试4.1:二叉树的遍历与应用1(原题链接)
Time Limit: 1000 ms Memory Limit: 65536 KiB
Problem Description
输入二叉树的先序遍历序列和中序遍历序列,输出该二叉树的后序遍历序列。
Input
第一行输入二叉树的先序遍历序列;
第二行输入二叉树的中序遍历序列。
Output
输出该二叉树的后序遍历序列。
Sample Input
ABDCEF
BDAECF
Sample Output
DBEFCA
Hint
Source
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct node
{
char data;
struct node *l,*r;
}Bnode;
Bnode *creat(int len,char firstorder[],char middleorder[])
{
int i;
Bnode *t;
if(len==0)return NULL;
else
{
t=(Bnode *)malloc(sizeof(Bnode));
t->data=firstorder[0];
for(i=0;i<len;i++)
{
if(middleorder[i]==firstorder[0])break;
}
t->l=creat(i,firstorder+1,middleorder);
t->r=creat(len-i-1,firstorder+i+1,middleorder+i+1);
return t;
}
}
void lastorder(Bnode *p)
{
if(p)
{
lastorder(p->l);
lastorder(p->r);
printf("%c",p->data);
}
}
int main()
{
int len;
Bnode *tree;
char firstorder[100],middleorder[100];
scanf("%s%s",firstorder,middleorder);
len=strlen(firstorder);
tree=creat(len,firstorder,middleorder);
lastorder(tree);
return 0;
}
若已知中序遍历和后序遍历来建立二叉树:
Bnode *creat(int len,char lastorder[],char middleorder[])
{
int i;
Bnode *t;
if(len==0)return NULL;
else
{
t=(Bnode *)malloc(sizeof(Bnode));
t->data=lastorder[len-1];
for(i=0; i<len; i++)
{
if(middleorder[i]==lastorder[len-1])break;
}
t->l=creat(i,lastorder,middleorder);
t->r=creat(len-i-1,lastorder+i,middleorder+i+1);
return t;
}
}
统计叶子数(度为一的节点)
typedef struct node
{
char data;
struct node *l,*r;
} Bnode;
int numfleaf(Bnode *p)
{
if(p)
{
if(p->l==NULL&&p->r==NULL)num++;
//若题目要求输出叶子节点则加上 cout<<p->data<<" ";
else
{
numfleaf(p->l);
numfleaf(p->r);
}
}
return num;
}
求树的高度(深度);
int high(struct node *root)
{
int h,lh,rh;
if(root==NULL)
h=0;
else
{
lh=high(root->l);
rh=high(root->r);
if(lh>rh)
h=lh+1;
else
h=rh+1;
}
return h;
}
建立一个排序二叉树
在树结构中,有一种特殊的二叉树叫做排序二叉树,直观的理解就是——(1).每个节点中包含有一个关键值 (2).任意一个节点的左子树(如果存在的话)的关键值小于该节点的关键值 (3).任意一个节点的右子树(如果存在的话)的关键值大于该节点的关键值。现给定一组数据,请你对这组数据按给定顺序建立一棵排序二叉树,并输出其中序遍历的结果。
树结构练习——排序二叉树的中序遍历(原题链接)
Time Limit: 1000 ms Memory Limit: 65536 KiB
Problem Description
在树结构中,有一种特殊的二叉树叫做排序二叉树,直观的理解就是——(1).每个节点中包含有一个关键值 (2).任意一个节点的左子树(如果存在的话)的关键值小于该节点的关键值 (3).任意一个节点的右子树(如果存在的话)的关键值大于该节点的关键值。现给定一组数据,请你对这组数据按给定顺序建立一棵排序二叉树,并输出其中序遍历的结果。
Input
输入包含多组数据,每组数据格式如下。
第一行包含一个整数n,为关键值的个数,关键值用整数表示。(n<=1000)
第二行包含n个整数,保证每个整数在int范围之内。
Output
为给定的数据建立排序二叉树,并输出其中序遍历结果,每个输出占一行。
Sample Input
1
2
2
1 20
Sample Output
2
1 20
Hint
Source
赵利强
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
struct node
{
int data;
struct node *ltree, *rtree;
};
int a[1000];
int k;
struct node *creat(struct node *root, int x)
{
if(root == NULL)
{
root = (struct node *)malloc(sizeof(struct node));
root->ltree = NULL;
root->rtree = NULL;
root->data = x;
}
else
{
if(x>root->data)
{
root->rtree = creat(root->rtree, x);
}
else
{
root->ltree = creat(root->ltree, x);
}
}
return root;
};
void showz(struct node *root)
{
if(root)
{
showz(root->ltree);
a[k++] = root->data;
showz(root->rtree);
}
}
int main()
{
int n, i;
struct node *root;
while(~scanf("%d", &n))
{
k = 0;
int x;
root = NULL;
for(i=0;i<n;i++)
{
scanf("%d", &x);
root = creat(root, x);
}
showz(root);
for(i = 0;i<k;i++)
{
if(i==k-1) printf("%d\n", a[i]);
else printf("%d ", a[i]);
}
}
return 0;
}
二叉排序树