数据结构与算法分析—树的概念和有关二叉树基本算法的实现(C语言)

 

 一 :树的概念和一些术语

  以下来自陈越姥姥DS课的PPT,英文比较短小精悍

Definition】A tree is a collection of nodes.  The collection can be empty; otherwise, a tree consists of

  (1)  a distinguished node r, called the root;

  (2)  and zero or more nonempty (sub)trees T1...Tk, each of whose roots are connected by a directed edge from r.

 Note:

 1  Subtrees must not connect together.  Therefore every node in the tree is the root of some subtree.

 2  There are   N   —   1  edges in a tree with N nodes.

 3  Normally the root is drawn at the top.

 degree of a node : number of subtrees of the node.  For example, degree(A) = 3, degree(F) = 0

 degree of a tree : max{degree(node)} node belong tree For example, degree of this tree = 3.

 parent : a node that has subtrees.

 children : the roots of the subtrees of a parent.

 siblings : children of the same parent.

 leaf ( terminal node ) : a node with degree 0 (no children).

 path from n1 to nk a (unique) sequence of nodes n1, n2, …, nk  such that ni is the parent of ni+1 for 1 <=i < k.

 length of path : number of edges on the path. 

 depth of nilength of the unique path from the root to ni.   Depth(root) = 0.

 height of nilength of the longest path from ni to a leaf.  Height(leaf) = 0, and height(D) = 2.

 height (depth) of a tree : height(root) = depth(deepest leaf).

 ancestors of a node : all the nodes along the path from the node up to the root.

 descendants of a node : all the nodes in its subtrees.

二:有关二叉树基本算法的实现

二叉树是一颗棵树,不存在有多于两个的儿子的节点

有一些比较特殊的二叉树

满二叉树:一个二叉树,如果每一个层的结点数都达到最大值,则这个二叉树就是满二叉树。也就是说,如果一个二叉树的层数为K,且结点总数是(2^k) -1 ,则它就是满二叉树。

完全二叉树:若设二叉树的深度为h,除第 h 层外,其它各层 (1~h-1) 的结点数都达到最大个数,第 h 层所有的结点都连续集中在最左边,这就是完全二叉树。

以下是一些关于二叉树的基本算法,比如前序 后序 中序 层序遍历 求树的高度

/*
        2
       / \  
      3   1
     /   / \  
    5   6   4
*/
#include <stdio.h>
#include <stdlib.h>

#define MAXN 1000
typedef struct TNode *Position;
typedef Position BinTree;
struct TNode{
    int Data;
    BinTree Left;
    BinTree Right;
};

void preorder(BinTree T)
{
    if(T != NULL) {
        printf("%d\n",T -> Data);
        preorder(T -> Left);
        preorder(T -> Right);
    }
}
void inorder(BinTree T)
{
     if(T != NULL) {
        inorder(T -> Left);
        printf("%d\n",T -> Data);
        inorder(T -> Right);
    }
}
void postorder(BinTree T)
{
    if(T != NULL) {
        postorder(T -> Left);
        postorder(T -> Right);
        printf("%d\n",T -> Data);
    }
}
int getHeight(BinTree T)
{
    if(T == NULL) return 0;
    else {
        int Left_Height = getHeight(T -> Left);
        int Right_Height = getHeight(T -> Right);

        if(Left_Height >= Right_Height) return Left_Height + 1;
        return Right_Height + 1;
    }
}
void levelorder(BinTree T)
{
    if(T == NULL) return;
    BinTree Que[MAXN];
    int Front = 0,Rear = 0;
    Que[Rear++] = T;
    while(Front < Rear) {
        BinTree _T = Que[Front++];
        printf("%d\n",_T -> Data);
        if(_T -> Left != NULL) Que[Rear++] = _T -> Left;
        if(_T -> Right != NULL) Que[Rear++] = _T -> Right;
    }
}
BinTree create()
{
    return (Position)malloc(sizeof(struct TNode));
}
int main(void)
{
    BinTree T1,T2,T3,T4,T5,T6;
    //分配内存
    T1 = create();
    T2 = create();
    T3 = create();
    T4 = create();
    T5 = create();
    T6 = create();
    //赋值
    T1 -> Data = 1;
    T2 -> Data = 2;
    T3 -> Data = 3;
    T4 -> Data = 4;
    T5 -> Data = 5;
    T6 -> Data = 6;
    //建树
    T2 -> Left = T3;
    T2 -> Right = T1;
    T3 -> Left = T5;
    T3 -> Right = NULL;
    T1 -> Left = T6;
    T1 -> Right = T4;
    T5 -> Left = NULL;
    T5 -> Right = NULL;
    T6 -> Left = NULL;
    T6 -> Right = NULL;
    T4 -> Left = NULL;
    T4 -> Right = NULL;
    printf("--------preorder-----------\n");
    preorder(T2);
    printf("--------inorder------------\n");
    inorder(T2);
    printf("--------postorder------------\n");
    postorder(T2);
    printf("--------levelorder---------\n");
    levelorder(T2);
    printf("Height is %d\n",getHeight(T2));
    return 0;
}

 下面分别是根据前序遍历和中序遍历找后序遍历,根据后序遍历和中序遍历找前序遍历

#include <stdio.h>
#include <stdlib.h>

#define MAXN 1000
typedef struct TNode *Position;
typedef Position BinTree;
struct TNode{
    char Data;
    BinTree Left;
    BinTree Right;
};
BinTree build(char *pre,char *mid,int size)
{
    if(size <= 0) return NULL;
    int i = 0;
    for(i = 0; i < size; i++) {
        if(*(mid + i) == *pre) {
            break;
        }
    }
    BinTree T;
    T = (Position)malloc(sizeof(struct TNode));
    T -> Data = mid[i];
    T -> Left = build(pre + 1,mid,i);
    T -> Right = build(pre + 1 + i,mid + i + 1,size - i - 1);
    return T;
}
void postorder(BinTree T)
{
    if(T != NULL) {
        postorder(T -> Left);
        postorder(T -> Right);
        printf("%c\n",T -> Data);
    }
}
int main(void)
{
    char pre[55],mid[55];
    scanf("%s %s",pre,mid);
    BinTree T = build(pre,mid,strlen(pre));
    postorder(T);
    return 0;
}

 

#include <stdio.h>
#include <stdlib.h>

#define MAXN 1000

typedef struct TNode *Position;
typedef Position BinTree;
struct TNode{
    char Data;
    BinTree Left;
    BinTree Right;
};
BinTree build(char *post,char *mid,int size)
{
    if(size <= 0) return NULL;
    int i = 0;
    for(i = 0; i < size; i++) {
        if(*(mid + i) == *(post + size - 1)){
            break;
        }
    }
    BinTree T;
    T = (Position)malloc(sizeof(struct TNode));
    T -> Data = mid[i];
    T -> Left = build(post,mid,i);
    T -> Right = build(post + i,mid + i + 1,size - i - 1);
    return T;
}
void preorder(BinTree T)
{
    if(T != NULL) {
        printf("%c\n",T -> Data);
        preorder(T -> Left);
        preorder(T -> Right);
    }
}
int main(void)
{
    char post[55],mid[55];
    scanf("%s %s",post,mid);
    BinTree T = build(post,mid,strlen(post));
    preorder(T);
    return 0;
}

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值