二叉树(数据结构)

介绍

  • 树(Tree)是n(n >= 0)个结点的有限集合,n = 0 时称为空树。在任意一棵非空树中:
    (1)有且仅有一个特定的称为根(Root)的结点;
    (2)当n > 1 时,其余结点可分为m(m > 0)个互不相交的有限集T1,T2,……Tm,其中每个集合本身又是一棵树,并且称为根的子树(SubTree);
  • 注意两点:(1) 根节点是唯一的;(2) 子树互不相交。

二叉树

  • 二叉树是一种特殊的树,它的特点是每个结点最多有两个子树(即二叉树的度不能大于2),并且二叉树的子树有左右之分,其次序不能颠倒。
  • 一棵深度为k 且有2^k -1 个结点的二叉树称为满二叉树。
  • 如果有深度为k 的,有n 个结点的二叉树,如果其每一个结点都与深度为k 的满二叉树中编号从1 至n 的结点一一对应,则称之为完全二叉树。
  • 二叉树性质:
    性质1:在二叉树的第i 层上最多有2^(i – 1)个结点。
    性质2:深度为k 的二叉树至多有2^i – 1 个结点。
/*************************************************************************
    > File Name: tree.c
    > Author: mrhjlong
    > Mail: [email protected] 
    > Created Time: 2016年05月07日 星期六 09时57分04秒
 ************************************************************************/

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

typedef int type_t;

typedef struct node
{
    type_t data;

    struct node *left;
    struct node *right;
}Node;

//创建结点
Node *node_create(type_t data)
{
    Node *p = (Node *)malloc(sizeof(Node));

    p->data = data;
    p->left = NULL;
    p->right = NULL;

    return p;
}

//中序遍历
void tree_in_order(Node *tree)
{
    if(tree == NULL)
        return;
    else
    {
        tree_in_order(tree->left);
        printf("in: %d\n", tree->data);
        tree_in_order(tree->right);
    }
}

//前序遍历
void tree_pre_order(Node *tree)
{
    if(tree == NULL)
        return;
    else
    {
        printf("pre: %d\n", tree->data);

        tree_pre_order(tree->left);
        tree_pre_order(tree->right);
    }
}

//后序遍历
void tree_post_order(Node *tree)
{
    if(tree == NULL)
        return;
    else
    {
        tree_post_order(tree->left);
        tree_post_order(tree->right);
        printf("next: %d\n", tree->data);
    }
}

void node_insert_by_recursion(Node **pTree, Node *pNode)
{
    if(*pTree == NULL)
    {
        *pTree = pNode;
    }
    else if((*pTree)->data >= pNode->data)
    {
        node_insert_by_recursion(&((*pTree)->left), pNode);
    }
    else
    {
        node_insert_by_recursion(&((*pTree)->right), pNode);
    }
}

void node_insert(Node **pTree, Node *pNode)
{
    if(*pTree == NULL)
    {
        *pTree = pNode;
    }
    else
    {
        Node *p = *pTree;
        Node *pre = p;
        while(p != NULL)
        {
            pre = p;
            if(pNode->data <= p->data)
                p = p->left;
            else
                p = p->right;
        }

        if(pNode->data <= pre->data)
        {
            pre->left = pNode;
        }
        else
        {
            pre->right = pNode;
        }
    }
}

//查找结点
Node *tree_search_by_recursion(Node *tree, type_t data)
{
    if(tree == NULL)
    {
        printf("%d is not found!\n", data);
        return NULL;
    }
    else if(tree->data == data)
    {
        return tree;
    }
    else if(data <= tree->data)
    {
        tree_search_by_recursion(tree->left, data);
    }
    else
    {
        tree_search_by_recursion(tree->right, data);
    }
}

Node *tree_search(Node *tree, type_t data)
{
    Node *p = tree;
    while(p != NULL)
    {
        if(p->data == data)
            return p;
        else if(data <= p->data)
            p = p->left;
        else
            p = p->right;
    }

    return p;
}

//求二叉树高度
int tree_height(Node *tree)
{
    int left = 0;
    int right = 0;

    if(tree == NULL)
        return 0;
    else
    {
        left = 1 + tree_height(tree->left);
        right = 1 + tree_height(tree->right);
        return (left > right ? left : right);
    }
}

//销毁二叉树
void tree_destroy(Node **pTree)
{
    if(*pTree == NULL)
        return;
    else
    {
        tree_destroy(&((*pTree)->left));
        tree_destroy(&((*pTree)->right));
        free(*pTree);
        *pTree = NULL;
    }
}

//删除结点
void node_delete(Node **pTree,type_t data)
{
    Node *pFind = *pTree;
    Node *pParent = NULL;

    //查找要删除的结点及其父结点
    while(pFind != NULL)
    {
        if(pFind->data == data)
            break;
        else if(data < pFind->data)
        {
            pParent = pFind;
            pFind = pFind->left;
        }
        else
        {
            pParent = pFind;
            pFind = pFind->right;
        }
    }

    if(pFind == NULL)
        return;
    else if(pFind->left == NULL || pFind->left->right == NULL)
    {
        if(pParent == NULL)
        {
            *pTree = pFind->right;
        }
        else
        {
            if(pParent->left == pFind)
                pParent->left = pFind->right;
            else
                pParent->right = pFind->right;
        }
        free(pFind);
        return;
    }
    else
    {
        pParent = pFind->left;
        Node *pos = pParent->right;
        while(pos->right != NULL)
        {
            pParent = pos;
            pos = pos->right;
        }
        pParent->right = pos->left;
        pFind->data = pos->data;
        free(pos);
        return;
    }


}


int main()
{
    Node *tree = NULL;
    int a[] = {45, 23, 65, 100, 29, 55, 89, 10, 3, 43, 36};
    int i = 0;
    Node *p = NULL;

    for(i = 0; i < sizeof(a) / sizeof(a[0]); i++)
    {
        p = node_create(a[i]);
        node_insert(&tree, p);
    }

//  tree_destroy(&tree);
    Node *pSea = tree_search(tree, 36);
    if(pSea != NULL)
    {
        printf("search: %d\n", pSea->data);
        printf("pSea: %p\n", pSea);
        printf("pNode: %p\n", p);
    }
    printf("height: %d\n", tree_height(tree));

    node_delete(&tree, 29); 

    tree_in_order(tree);
    printf("\n");
    tree_pre_order(tree);
    printf("\n");
    tree_post_order(tree);

    return 0;
}

你可能感兴趣的:(数据结构)