查找二叉树

/*
 *典型的二叉树,此二叉树的构建方法是:
 *第一个输入的数字为根结点
 *下一个数字若比第一个数字小则为其左孩子,比第一个数字大则为其右孩子
 *
 *所以使用中序遍历能够将树中的数字按从小到大的顺序排列。
 *三种遍历方法针对不同的树的构造方法
 *
 *代码中包含以下功能模块:
 *  1.向二叉树中插入元素
 *      2.先序遍历二叉树
 *  3.中序遍历二叉树
 *  4.后序遍历二叉树
 *  5.查询二叉树
 *  6.获取树中最大元素和最小元素
 *  7.获取某个数字的前驱和后继
 *  8.删除二叉树中的某个元素
 *          Author: StoryMonster
 *Last change Date: 2016/6/22
 */

#include <iostream>
#include <stdlib.h>

typedef struct aa
{
    struct aa * left;
    struct aa * right;
    struct aa * father;
    int value;
} TreeNode;

TreeNode *root = (TreeNode *)malloc(sizeof(TreeNode));
int NodeCount = 0;

/*
 *在以node为根结点的树中查找值为value的节点
 *返回该节点,若无该节点返回NULL
 */
TreeNode *Search(TreeNode *node,int value)
{
    if(node->value == value || node == NULL)
    {
        return node;
    }
    if(node->value < value)
    {
        Search(node->right,value);
    }
    else
    {
        Search(node->left,value);
    }
}
/*
 *获取以node为根结点的树中值最大的节点
 */
TreeNode *Maximum(TreeNode *node)
{
    if(node->right == NULL)
    {
        return node;
    }
    Maximum(node->right);
}
/*
 *获取以node为根结点的树中值最小的节点
 */
TreeNode *Minimum(TreeNode *node)
{
    if(node->left == NULL)
    {
        return node;
    }
    Minimum(node->left);
}
/*
 *获取node节点在中序遍历下的后继节点
 */
TreeNode *Successor(TreeNode *node)
{
    if(node->right != NULL)
    {
        return Minimum(node->right);
    }
    TreeNode *p = node->father; 
    while(p != NULL && node == p->right)
    {
        node = p;
        p = node->father;
    }
    return p;
}
/*
 *获取node节点在中序遍历下的前驱节点
 */
TreeNode *Precursor(TreeNode *node)
{
    if(node->left != NULL)
    {
        return Maximum(node->left);
    }
    TreeNode *p = node->father;
    while(p->left == node && p!=NULL)
    {
        node = p;
        p = node->father;
    }
    return p;
}
/*
 *删除树中一个节点
 */
void DeleteNode(TreeNode *node)
{
    if(node->left == NULL&&node->right == NULL)
    {
        TreeNode *p = node->father;
        if(p->left == node)
        {
            p->left = NULL;
        }
        else
        {
            p->right = NULL;
        }
        free(node);
        NodeCount--;
    }
    else if(node->left == NULL)
    {
        TreeNode *child = node->right;
        TreeNode *p = node->father;
        if(p->left == node)
        {
            p->left = child;
        }
        else
        {
            p->right = child;
        }
        free(node);
        NodeCount--;
    }
    else if(node->right == NULL)
    {
        TreeNode *child = node->left;
        TreeNode *p = node->father;
        if(p->left == node)
        {
            p->left = child;
        }
        else
        {
            p->right = child;
        }
        free(node);
        NodeCount--;
    }
    else 
    {
        TreeNode *p = Successor(node);
        DeleteNode(p);
        node->value = p->value;
    }
}
/*
 *插入元素的递归
 */
void InsertNode(TreeNode *node,TreeNode *p)
{
    if(p->left == NULL && (p->value >= node->value))
    {
        p->left = node;
        node->father = p;
        return ;
    }
    if(p->right == NULL && (p->value < node->value))
    {
        p->right = node;
        node->father = p;
        return;
    }
    if(p->value >= node->value)
    {
        InsertNode(node,p->left);
    }
    else
    {
        InsertNode(node,p->right);
    }
}
/*
 *插入节点
 */
void Insert(TreeNode *node)
{
    if(root == NULL)
    {
        root = node;
        NodeCount++;
        return ;
    }
    TreeNode *p = root;
    InsertNode(node,p);
    NodeCount++;
}
/*
 *先序遍历递归体
 */
void preorderTraversal(TreeNode *p)
{
    if(p == NULL)
    {
        return ;
    }
    std::cout << p->value << "  ";
    preorderTraversal(p->left);
    preorderTraversal(p->right);
}
/*
 *先序遍历
 */
void PreorderTraversal()
{
    TreeNode *p = root;
    preorderTraversal(root);
    std::cout << std::endl;
}
/*
 *中序遍历递归体
 */
void inorderTraversal(TreeNode *p)
{
    if(p == NULL)
    {
        return ;
    }
    inorderTraversal(p->left);
    std::cout << p->value << "  ";
    inorderTraversal(p->right);
}
/*
 *中序遍历
 */
void InorderTraversal()
{
    TreeNode *p = root;
    inorderTraversal(p);
    std::cout << std::endl;
}
void postorderTraversal(TreeNode *p)
{
    if(p == NULL)
    {
        return ;
    }
    postorderTraversal(p->left);
    postorderTraversal(p->right);
    std::cout << p->value << "  ";
}
/*
 *后序遍历
 */
void PostorderTraversal()
{
    TreeNode *p = root;
    postorderTraversal(p);
    std::cout << std::endl;
}
int main()
{
    root = NULL;
    short choice = 0;
    int value = 0;
    while(1)
    {
        TreeNode *node = (TreeNode *)malloc(sizeof(TreeNode));
        std::cout << "1:insert  2:preorder tranversal  3:inorder tranversal  4:postorder tranversal  5:delete node" << std::endl;
        std::cin >> choice;
        switch(choice)
        {
            case 1: std::cout << "input value:";
                    std::cin >>node->value;
                    node->father = NULL;
                    node->left   = NULL;
                    node->right  = NULL;
                    Insert(node);
                    std::cout << "there are " << NodeCount << " nodes in this tree!"<< std::endl;
                    break;
            case 2: std::cout << "preorder traversal:";
                    PreorderTraversal();
                    break;
            case 3: std::cout << "inorder traversal:";
                    InorderTraversal();
                    break;
            case 4: std::cout << "postorder traversal:";
                    PostorderTraversal();
                    break;
            case 5: std::cout << "input node value:";
                    std::cin >> value;
                    TreeNode *p = Search(root,value);
                    DeleteNode(p);
                    std::cout << "there are " << NodeCount << " nodes left!"<< std::endl;
                    break;
        }
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值