二叉樹的創建、遍歷及搜索(C實現)

binTree.h

#ifndef BIN_TREE_H
#define BIN_TREE_H

typedef int DataType;

typedef struct node
{
    DataType    data;
    struct  node *lchild, *rchild;
}BinTNode;

typedef BinTNode* BinTree;

void CreateBinTree(BinTree* root);
BinTree BinTreeInsert(BinTree bt, DataType k);

void preorderTraversal(BinTree root);
void inorderTraversal(BinTree root);
void postorderTraversal(BinTree root);

void FindValue(BinTree root, DataType k, int level);

#endif

 

binTree.c

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

/* init a BST */
void CreateBinTree(BinTree* root)
{
    BinTree t;  /* t will be the bintree root pointer */
    DataType tmp;
    
    t = NULL;
    printf("please input the new node value:");
    scanf("%d", &tmp);
    while (tmp != 0)
    {
        t = BinTreeInsert(t, tmp);
        printf("please input the new node value:");
        scanf("%d", &tmp);
    }

    *root = t;
}

/* insert a node into bintree */
/* during this procedure, we can't lose the root pointer(bt) */
BinTree BinTreeInsert(BinTree bt, DataType k)
{
    BinTNode *p;
    
    /* if the tree is empty, first create the root node */
    if (bt == NULL)
    {
        p = (BinTNode *)malloc(sizeof(BinTNode));
        p->data = k;
        p->lchild = p->rchild = NULL;
        return p; /* 調用返回時,函數無法返回執行過程中在棧上分配的指針變量,但可以返回在堆上分配的^_^ */
    }
    else if (bt->data == k)
    {
        return bt;
    }
    else if (bt->data > k)
    {
        bt->lchild = BinTreeInsert(bt->lchild, k);
    }
    else
    {
        bt->rchild = BinTreeInsert(bt->rchild, k);
    }

    /* 從建立根結點開始,以後的每次調用都不會改變bt的值,只是爲了實現鏈式表達式 */
    return bt;
}

/* 中序遍歷 */
void inorderTraversal(BinTree root)
{
    if (root)
    {
        inorderTraversal(root->lchild);
        printf("%d ", root->data);
        inorderTraversal(root->rchild);
    }
}

/* 前序遍歷 */
void preorderTraversal(BinTree root)
{
    if (root)
    {
        printf("%d ", root->data);
        preorderTraversal(root->lchild);
        preorderTraversal(root->rchild);        
    }
}

/* 後序遍歷 */
void postorderTraversal(BinTree root)
{
    if (root)
    {
        postorderTraversal(root->lchild);
        postorderTraversal(root->rchild);
        printf("%d ", root->data);    
    }    
}

/* print out the times of call when you find out this value */
void FindValue(BinTree root, DataType k, int level)
{
    level++;
    if (root == NULL)
    {
        printf("sorry, after %d times call, I can't find this value\n", level);
        return;        
    }
    
    if (root->data == k)
    {
        printf("after %d times call, I find it!!\n", level);    
    }
    else if (k < root->data)
    {
        FindValue(root->lchild, k, level);    
    }
    else if (k > root->data)
    {
        FindValue(root->rchild, k, level);    
    }
}

main.c

#include "binTree.h"
#include <stdio.h>

int main()
{
    BinTree root;
    int v;
    
    CreateBinTree(&root);
    printf("inorderTraversal is:\n");
    inorderTraversal(root);
    printf("\n");
    
    printf("preorderTraversal is:\n");
    preorderTraversal(root);
    printf("\n");
    
    printf("postorderTraversal is:\n");
    postorderTraversal(root);
    printf("\n");
    
    printf("root->data is %d\n", root->data);
    
    while (v >= 0)
    {
        printf("please input the value you want to find:");
        scanf("%d", &v);
        FindValue(root, v, 0);
    }
    
    return 0;
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章