hello head

#include <iostream>

using namespace std;
typedef struct Node
{
    int val;
    Node* left;
    Node* right;
}Node;


Node* createNode(int val)
{
    Node* node = new Node;
    node->val = val;
    node->left = NULL;
    node->right = NULL;
    return node;
}

//插入二叉搜索树
Node* insertBSTree(Node* &node, int val)
{
    
    if (NULL == node)
    {
        node = createNode(val);
    }
    
    else if (node->val > val)
    {
        node->left = insertBSTree(node->left, val);
    }
    else
    {
        node->right = insertBSTree(node->right, val);
    }
    return node;
}

//创建二叉查找树
Node* createBSTree(Node* &node, int* array, int size)
{
    for (int i = 0; i < size; ++i)
    {
        insertBSTree(node, array[i]);
    }
    return node;
}

Node* deleteBSTree(Node*& node, int val)
{
    if (NULL == node){return NULL;}

    Node head;
    head.left = node;

    if (node->val > val)
    {
        deleteBSTree(node->left, val);
    }
    else if (node->val < val)
    {
        deleteBSTree(node->right, val);
    }
    else
    {
        //case 1:左右子树都是NULL
        if (NULL == node->left && NULL == node->right)
        {
            delete node;
            node = NULL;
        }
        //case 2:左子树是NULL
        else if (NULL == node->left)
        {
            head.left = node->right;
            delete node;
            node = NULL;
        }
        //case 3:右子树是NULL
        else if (NULL == node->right)
        {
            head.left = node->left;
            delete node;
            node = NULL;
        }
        //case4 :左右子树都不是NULL
        else 
        {
            head.left = node->left;
            Node* tmp = node->left->right;
            while(tmp)
            {
                tmp = tmp->right;
            }
            tmp = node->right;
            delete node;
            node = NULL;
        }
    }
    node = head.left;
    return head.left;
    
}

Node* searchBSTree(Node* node, int val)
{
    if (NULL == node)
    {
        return NULL;
    }
    if (node->val == val)
    {
        return node;
    }
    else if (node->val > val)
    {
        return searchBSTree(node->left, val);
    }
    else
    {
        return searchBSTree(node->right, val);
    }
}

void midPrintBSTree(Node* node)//中序遍历
{
    if (NULL != node)
    {
        if (node->left)
        {
            midPrintBSTree(node->left);
        }
        cout << node->val << " ";
        if (node->right)
        {
            midPrintBSTree(node->right);
        }
    }
}

void prePrintBSTree(Node* node)//先序遍历
{
    if (NULL != node)
    {
        cout << node->val << " ";
        if (node->left)
        {
            prePrintBSTree(node->left);
        }
        if (node->right)
        {
            prePrintBSTree(node->right);
        }
    }
}

void postPrintBSTree(Node* node)//后续遍历
{
    if (NULL != node)
    {
        if (node->left)
        {
            postPrintBSTree(node->left);
        }
        if (node->right)
        {
            postPrintBSTree(node->right);
        }
        cout << node->val << " ";
    }
}

int main()
{

    int arr[] = {6,5,4,3,2,1,9,8,7};
    Node *node = NULL;
    createBSTree(node, arr, 9);
    midPrintBSTree(node);
    cout << "\n";
    postPrintBSTree(node);
    cout << "\n";
    prePrintBSTree(node);
    Node* rst = searchBSTree(node, 10);
    if (rst)
    {
        cout << rst->val << endl;
    }
    else
    {

        cout << "\nnot found.\n";
    }
    deleteBSTree(node, 6);
    midPrintBSTree(node);
    return 0;
}

 

posted on 2017-08-28 18:10  gardonkoo  阅读(121)  评论(0编辑  收藏  举报

hello foot