1. 程式人生 > >資料結構與演算法 (十) 二叉樹 前序遍歷 中序遍歷 後序遍歷

資料結構與演算法 (十) 二叉樹 前序遍歷 中序遍歷 後序遍歷

名詞解釋

 度數(degree) 一個結點的子樹個數

 樹葉(leaf) 沒有子樹的結點稱為樹葉或終端結點

 分支結點(branch node) 非終端結點

 子女(child)和兒子(son)非終端結點

 父母(parent)若結點s是結點p的兒子 則稱p是x的父母或者父親

 有序樹(ordered tree) 樹中各個結點的兒子都是有序的

 層數(level) 定義樹根的層數為1.其他結點的層數等於其父母結點的層數加1

 高度(height) 樹中的結點的最大層數 規定空樹的高度為 0

 

1.二叉樹概念

      二叉樹由結點的有限集合構成,這個有限集合或者為空或者由一個根結點以及兩顆不想交的分別稱為這個根的左子樹和右子樹的二叉樹組成

2.二叉樹的遍歷

binarytreenode.h

struct binaryTreeNode
{
    ElementType data;
    struct binaryTreeNode *LeftChild,*RightChild;
};

typedef struct binaryTreeNode BinaryTreeNode;

//初始化樹節點
void InitBinaryTreeNode(BinaryTreeNode *btree,ElementType e,BinaryTreeNode *l,BinaryTreeNode *r)
{
    btree->LeftChild=l;
    btree->RightChild=r;
    btree->data=e;
}
//生成樹節點
BinaryTreeNode *CreateBTreeNode(ElementType item,BinaryTreeNode *lptr,BinaryTreeNode *rptr)
{
    BinaryTreeNode *p;
    p=(BinaryTreeNode *)malloc(sizeof(BinaryTreeNode));
    if(p==NULL)
    {
        printf("memory allocation failure\n");
    }
    else
    {
        InitBinaryTreeNode(p,item,lptr,rptr);
    }
}

binarytree.h


//此處宣告要放在頭部
enum boolean {FALSE,TRUE};
typedef enum boolean Bool;

#ifndef FALSE
#define FALSE 0
#endif

#ifndef TRUE
#define TRUE 1
#endif


struct binaryTree
{
    BinaryTreeNode *root;
};
typedef struct binaryTree BinaryTree;
//初始化
void InitBinaryTree(BinaryTree * bt)
{
    bt->root=NULL;
}
Bool IsEmpty(BinaryTree *bt)
{
    return ((bt->root) ? FALSE : TRUE);
}
Bool getRoot(BinaryTree *bt,ElementType *x)
{
    if(bt->root)
    {
        *x=bt->root->data;
        return TRUE;
    }
    else
    {
        return FALSE;
    }
}

BinaryTreeNode *MakeTree(BinaryTree *bt,ElementType element,BinaryTreeNode *left,BinaryTreeNode *right)
{

    bt->root=(BinaryTreeNode *)malloc(sizeof(BinaryTreeNode));
    if(bt->root==NULL)
    {

        printf("Memory allocation failure!\n");
        exit(1);
    }
    InitBinaryTreeNode(bt->root,element,left,right);
    return bt->root;
}
//前序遍歷

void PreOrder(BinaryTreeNode *t)
{
    if(t)
    {
		//前序遍歷 的先後順序
        printf("%c",t->data);
        PreOrder(t->LeftChild);  // a b d e
        PreOrder(t->RightChild); // c f
    }
}

//中序遍歷

void InOrder(BinaryTreeNode *t)
{
    if(t)
    {

        InOrder(t->LeftChild);
        printf("%c",t->data);
        InOrder(t->RightChild);
    }
}

//後序遍歷

void PostOrder(BinaryTreeNode *t)
{
    if(t)
    {
        PostOrder(t->LeftChild);
        PostOrder(t->RightChild);
        printf("%c",t->data);
    }
}

main.c

typedef char ElementType;

#include <stdio.h>
#include <stdlib.h>
#include "binarytreenode.h"
#include "binarytree.h"

int main()
{
    BinaryTree *t=(BinaryTree *)malloc(sizeof(BinaryTree));

    BinaryTreeNode *a,*b,*c,*d,*e,*f;

    f=MakeTree(t,'f',NULL,NULL);
    e=MakeTree(t,'e',NULL,NULL);

    d=MakeTree(t,'d',NULL,NULL);

    c=MakeTree(t,'c',f,NULL);
    b=MakeTree(t,'b',d,e);
    a=MakeTree(t,'a',b,c);

    printf("The PreOrder is \n");

    PreOrder(t->root);

    printf("\n");

    printf("The InOrder is \n");

    InOrder(t->root);

    printf("\n");

    printf("The PostOrder is \n");

    PostOrder(t->root);

    printf("\n");

//getchar();

    return 1;
}