• 简单二叉查找树


    #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 << "
    ";
        postPrintBSTree(node);
        cout << "
    ";
        prePrintBSTree(node);
        Node* rst = searchBSTree(node, 10);
        if (rst)
        {
            cout << rst->val << endl;
        }
        else
        {
    
            cout << "
    not found.
    ";
        }
        deleteBSTree(node, 6);
        midPrintBSTree(node);
        return 0;
    }
  • 相关阅读:
    管理之道
    散户炒股图
    Linux系统编程(37)—— socket编程之UDP服务器与客户端
    Linux系统编程(36)—— socket编程之UDP详解
    Linux系统编程(35)—— socket编程之TCP服务器的并发处理
    Linux系统编程(33)—— socket编程之TCP程序的错误处理
    Linux系统编程(34)—— socket编程之TCP服务器与客户端的交互
    Linux系统编程(32)—— socket编程之TCP服务器与客户端
    Linux系统编程(31)—— socket编程之TCP详解
    Linux系统编程(30)—— socket编程之TCP/IP协议
  • 原文地址:https://www.cnblogs.com/gardonkoo/p/7445079.html
Copyright © 2020-2023  润新知