一、基本概念
结点:包含一个数据元素及若干指向其它结点的分支信息。
结点的度:一个结点的子树个数称为此结点的度。
叶结点:度为0的结点,即无后继的结点,也称为终端结点。
分支结点:度不为0的结点,也称为非终端结点。
孩子结点:一个结点的直接后继称为该结点的孩子结点。
双亲结点:一个结点的直接前驱称为该结点的双亲结点。
兄弟结点:同一双亲结点的孩子结点之间互称兄弟结点。
祖先结点:一个结点的祖先结点是指从根结点到该结点的路径上的所有结点。
子孙结点:一个结点的直接后继和间接后继称为该结点的子孙结点。
树的度: 树中所有结点的度的最大值。
结点的层次:从根结点开始定义,根结点的层次为1,根的直接后继的层次为2,依此类推。
树的高度(深度): 树中所有结点的层次的最大值。
有序树:在树T中,如果各子树之间是有先后次序的,则称为有序树。
森林: ()棵互不相交的树的集合。将一棵非空树的根结点删去,树就变成一个森林;反之,给森林增加一个统一的根结点,森林就变成一棵树。
二叉树:我们把满足①每个结点的度都不大于2;②每个结点的孩子结点次序不能任意颠倒(一左一右)两个条件 的树形结构叫做二叉树。
满二叉树:深度为k且有 个结点的二叉树。
完全二叉树:深度为的所有结点都是满二叉二、二叉树的基本二、二叉树的基本性质
#include二叉树的遍历 先序遍历using namespace std; struct node{ int fa,len,ch[1005]; }arr[1005]; int main() { int n,a,b; scanf("%d",&n); for(int i=1;i<=n;i++){ scanf("%d%d",&a,&b); arr[b].len++; arr[a].fa=b; arr[b].ch[arr[b].len]=a; }
void preorder(int now){
if(now==0)
return;
printf("%d ",now);
preorder(arr[now].lc);
preorder(arr[now].rc);
}
中序遍历
void inorder(int now){
if(now==0)
return;
inorder(arr[now].lc);
printf("%d ",now);
inorder(arr[now].rc);
}
后序遍历
void postorder(int now){
if(now==0)
return;
postorder(arr[now].lc);
postorder(arr[now].rc);
printf("%d ",now);
}
已知中,后,求先
void dfs(int l1,int r1,int l2,int r2)
{
int m;
for(int i=0;il1) dfs(l1,m-1,l2,r2-r1+m-1);
if(m
以先序字符串方式建立二叉树
#include
using namespace std;
struct node{
char v;
int lc,rc;
}arr[85];
int root,cnt;
int newnode(char x){
cnt++;
arr[cnt].v=x,arr[cnt].lc=arr[cnt].rc=0;
return cnt;
}
void build(int &rt){
char c=getchar();
if(c==' ') return;
rt=newnode(c);
build(arr[rt].lc),build(arr[rt].rc);
}
void postorder(int x){
if(x==0) return;
postorder(arr[x].lc),postorder(arr[x].rc);
printf("%c",arr[x].v);
}
int main(){
build(root),postorder(root);
return 0;
}



