查找二叉树的实现与演示

最近重新学习了二叉树方面的知识.
写了一个查找二叉树的演示Demo

代码如下:
SearchTree.h

/* * SearchTree.h * * Created on: Jan 13, 2016 * Author: youngwan */

#ifndef SEARCHTREE_H_
#define SEARCHTREE_H_

typedef int ElementType;

typedef struct _TreeNode_ {
    ElementType Element;
    struct _TreeNode_ *Left;
    struct _TreeNode_ *Right;
}TreeNode, *Tree, *SearchTree, *Position;

SearchTree MakeEmpty(SearchTree T);
Position   Find(ElementType X, SearchTree T);
Position   FindMin(SearchTree T);
Position   FindMax(SearchTree T);
SearchTree Insert(ElementType X, SearchTree T);
SearchTree Delete(ElementType X, SearchTree T);
int        Height(SearchTree T);

#endif /* SEARCHTREE_H_ */

SearchTree.cpp

/*
 * SearchTree.c
 *
 *  Created on: Jan 13, 2016
 *      Author: youngwan
 */
#include <stdio.h>
#include <stdlib.h>
#include <vector>
#include "SearchTree.h"

using namespace std;
SearchTree MakeEmpty(SearchTree T)
{
    if (NULL != T)
    {
        MakeEmpty(T->Left);
        MakeEmpty(T->Right);
        free(T);
    }
    return NULL;
}

Position Find(ElementType X, SearchTree T)
{
    if (NULL == T)
        return NULL;

    if (X < T->Element)
        return Find(X, T->Left);
    else if (X > T->Element)
        return Find(X, T->Right);
    else
        return T;
}

Position FindMin(SearchTree T)
{
    if (NULL == T)
        return NULL;
    else if (NULL == T->Left)
        return T;
    else
        return FindMin(T->Left);
}

Position FindMax(SearchTree T)
{
    if (NULL == T)
        return NULL;
    else if (NULL == T->Right)
        return T;
    else
        return FindMax(T->Right);
}

/*非递归实现 查找最小*/
Position FindMin2(SearchTree T)
{
    if (NULL != T)
        while(NULL != T->Left)
            T = T->Left;

    return T;
}
/*非递归实现 查找最大*/
Position FindMax2(SearchTree T)
{
    if (NULL != T)
        while(NULL == T->Right)
            T = T->Right;

    return T;
}

SearchTree Insert(ElementType X, SearchTree T)
{
    if (NULL == T)
    {
        T = (SearchTree)malloc(sizeof(TreeNode));
        if (NULL == T)
        {
            fprintf(stderr, "malloc Error");
            return NULL;
        }
        else
        {
            T->Element = X;
            T->Left = T->Right = NULL;
        }
    }
    else if (X < T->Element)
    {
        T->Left = Insert(X, T->Left);
    }
    else if (X > T->Element)
    {
        T->Right = Insert(X, T->Right);
    }

    return T;
}

SearchTree Delete(ElementType X, SearchTree T)
{
    Position TmpCell;

    if (NULL == T)
    {
        fprintf(stderr, "Element not found");
        return NULL;
    }
    else if (X < T->Element)
    {
        T->Left = Delete(X, T->Left);
    }
    else if (X > T->Element)
    {
        T->Right = Delete(X, T->Right);
    }
    else if (T->Left && T->Right)    /*have two child*/
    {
        TmpCell = FindMin(T->Right);    /*find the smallest in right sub tree, set it be root node*/
        T->Element = TmpCell->Element;
        T->Right = Delete(T->Element, T->Right); /*right sub tree become it delete T*/
    }
    else                            /*have one or zero child*/
    {
        TmpCell = T;
        if (NULL == T->Left)
            T = T->Right;
        else if (NULL == T->Right)
            T = T->Left;
        free(TmpCell);
    }

    return T;
}

int Height(SearchTree T)
{
    int lDeep, rDeep;

    if (NULL == T)
        return -1;

    lDeep = Height(T->Left);
    rDeep = Height(T->Right);
    return 1 + (lDeep > rDeep ? lDeep : rDeep);
}

main.cpp

/*
 * main.cpp
 *
 *  Created on: Jan 16, 2016
 *      Author: youngwan
 */
#include <vector>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "SearchTree.h"

using namespace std;


static int power(int m, int n)
{
    int result = 1;

    while((n--) > 0)
    {
        result *= m;
    }
    return result;
}

void Print(Tree root)
{
    vector<Tree> vecTree[2];
    int current = 0;
    int next    = 1;
    int height, preSpace, mulSpace, i;

    if (root == NULL)
    {
        fprintf(stderr, "root is null\n");
        return ;
    }

    height = Height(root);
    vecTree[current].push_back(root);
    while (height >= 0)
    {
        preSpace = power(2, height) - 1;
        mulSpace = power(2, height + 1) - 1;
        for(i = 0; i < preSpace; i++)
            printf(" ");
        for(vector<Tree>::iterator iter = vecTree[current].begin(); iter < vecTree[current].end(); iter++)
        {
            if (NULL == (*iter))
            {
                printf(" ");
                vecTree[next].push_back(NULL);
                vecTree[next].push_back(NULL);
            }
            else
            {
                printf("%-2d", (*iter)->Element); vecTree[next].push_back((*iter)->Left); vecTree[next].push_back((*iter)->Right); } for(i = 0; i < mulSpace; i++) printf(" "); } printf("\n"); vecTree[current].clear(); current = 1 - current; next = 1 - next; height--; } } void Show(Tree root) { if (NULL == root) { fprintf(stderr, "root is empty!\n"); } else { Position pMax, pMin; int height; pMax = FindMax(root); pMin = FindMin(root); height = Height(root); printf("Max : %d\n", pMax->Element); printf("Min : %d\n", pMin->Element); printf("High : %d\n", height + 1); } } void FindElement(Tree root) { char buffer[1024] = {0}; int ret = 0; char *p = NULL; printf("Please input Digit you want find, split by ','!\n"); ret = scanf("%s", buffer); if (ret <= 0) { printf("Error, ret:%d\n", ret); return ; } p = strtok(buffer, ","); while(p) { if((*p) > '0' && (*p) <= '9') { ret = atoi(p); if (NULL == Find(ret, root)) printf("%d is not found\n", ret); else printf("%d is found\n", ret); } p = strtok(NULL, ","); } } Tree OperatorInsert(Tree root) { char buffer[1024] = {0}; int ret = 0; char *p = NULL; printf("Please Input Digit you want insert, split by ','!\n"); ret = scanf("%s", buffer); if (ret <= 0) { printf("Error, ret:%d\n", ret); return root; } p = strtok(buffer, ","); while(p) { if((*p) > '0' && (*p) <= '9') { ret = atoi(p); root = Insert(ret, root); } p = strtok(NULL, ","); } return root; } Tree OperatorDelete(Tree root) { char buffer[1024] = {0}; int ret = 0; char *p = NULL; printf("Please Delete Digit you want insert, split by ','!\n"); ret = scanf("%s", buffer); if (ret <= 0) { printf("Error, ret:%d\n", ret); return root; } p = strtok(buffer, ","); while(p) { if((*p) > '0' && (*p) <= '9') { ret = atoi(p); root = Delete(ret, root); } p = strtok(NULL, ","); } return root; } int main(void) { Tree root = NULL; char input[1024]; printf("Usage: insert only support digit(>0)\n"); while(1) { printf("(I:insert; D:delete; C:clear; P:print; S:Show; F:Find)\n"); printf("Please Input operator.\n"); scanf("%s", input); switch(input[0]) { case 'I': case 'i': root = OperatorInsert(root); break; case 'D': case 'd': root = OperatorDelete(root); break; case 'C': case 'c': root = MakeEmpty(root); break; case 'P': case 'p': Print(root); break; case 'S': case 's': Show(root); break; case 'F': case 'f': FindElement(root); break; default:break; } } return 1; } 

参考书籍: 数据结构与算法分析 c语言描述

    原文作者:二叉查找树
    原文地址: https://blog.csdn.net/wanlanjian/article/details/50544940
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞