【问题标题】:convert a not-full binary search tree to full binary search tree将不完整的二叉搜索树转换为完整的二叉搜索树
【发布时间】:2017-04-27 15:50:46
【问题描述】:

我在完全二叉搜索树中遇到问题 如何将不完整的二叉搜索树转换为完整的二叉搜索树,例如这个例子:
我在树中有一个节点结构:

结构节点 {
整数键;
节点*左,*右;
};

我有一棵二叉树
--------18
--------/----\
-----16---19
-----/-\----\
----8--17--20
---/------------
--7------------
这不是一个完整的二叉搜索树,它只是一棵二叉树。 我想将此树转换为完整的二叉搜索树,例如:
---------17
-------/-----\
------8-----19
-----/-\-----/---\
----7-16--18---20
请帮我解决,非常感谢!

【问题讨论】:

    标签: c++ binary-search-tree


    【解决方案1】:

    如果二叉树中的节点数有足够的节点来构造一棵完整的树,则一种方法有效,即节点数等于 1 或 3 或 7 或 15 或 ... 2^n -1,一个正整数n,就是将树读入向量并排序。然后可以将排序后的向量传递给描述为here 的众所周知的算法。这个想法是 BST 的根在向量的中间,并且作为左子树的根的根节点的左子节点将在排序向量的左半部分的中间(同样适用于右节点和向量的右半部分)。

    请注意:如果二叉树中的节点数(您的输入)不等于 2^n -1 则无法构造完整 二元(搜索)树,因为您没有正确数量的可用节点。但是,如果您被允许添加额外的节点,即不在二叉树中的节点(您在问题中没有提到这一点),下面的代码也可以工作,但您需要向排序后的节点添加额外的节点向量(例如,最后递增的数字)。

    #include <vector>
    #include <iostream>
    #include <algorithm>
    
    using namespace std;
    
    struct Node {
        int key;
        Node* left,*right;
    };
    
    
    /*
     * Construct a tree like this and return a pointer
     * to the root node
     */
    //--------18
    //------/----\
    //-----16---19
    //-----/-\----\
    //----8--17--20
    //---/-----------
    //--7------------
    Node* constructExampleTree();
    
    /*
     * Place all nodes which are part of the tree
     * mounted under 'node' (including 'node') into 'vec'.
     */
    void placeIntoVector(vector<int>& vec, Node* node);
    
    /*
     * Print the tree rooted under 'node' in order
     */
    void printInOder(Node* node);
    
    /*
     * Delete all nodes rooted under 'node' in post order
     */
    void deleteInPostOrder(Node* node);
    
    /* Helper function that allocates a new node with the
       given data and NULL left and right pointers. */
    Node* newNode(int data);
    
    /* A function that constructs Balanced Binary Search Tree from a sorted array
     *
     * Algorithm taken from: http://www.geeksforgeeks.org/sorted-array-to-balanced-bst/
     * */
    Node* sortedArrayToBST(vector<int>& arr, int start, int end)
    {
        /* Base Case */
        if (start > end)
          return NULL;
    
        /* Get the middle element and make it root */
        int mid = (start + end)/2;
        Node *root = newNode(arr[mid]);
    
        /* Recursively construct the left subtree and make it
           left child of root */
        root->left =  sortedArrayToBST(arr, start, mid-1);
    
        /* Recursively construct the right subtree and make it
           right child of root */
        root->right = sortedArrayToBST(arr, mid+1, end);
    
        return root;
    }
    
    int main (){
    
        Node* root = constructExampleTree();
    
        vector<int> vec;
        placeIntoVector(vec, root);
    
        sort(vec.begin(),vec.end());
    
        int counter = static_cast<int>( vec.size());
        Node *bstRoot = sortedArrayToBST(vec, 0, counter -1);
    
        // Printing a BST in order will give us a sorted output
        printInOder(bstRoot);
    
        deleteInPostOrder(root);
        deleteInPostOrder(bstRoot);
    
        return 0;
    }
    
    Node* constructExampleTree() {
    
        /*
         * Level 1 and 2
         */
        Node* eighteen = new Node();
        eighteen->key = 18;
        eighteen->left = NULL;
        eighteen->right= NULL;
    
        Node* sixteen = new Node();
        sixteen->key = 16;
        sixteen->left = NULL;
        sixteen->right= NULL;
    
        Node* nineteen = new Node();
        nineteen->key = 19;
        nineteen->left = NULL;
        nineteen->right= NULL;
    
        eighteen->left = sixteen;
        eighteen->right = nineteen;
    
        /*
         * Level 3
         */
        Node* eight = new Node();
        eight->key = 8;
        eight->left = NULL;
        eight->right= NULL;
    
        Node* seventeen = new Node();
        seventeen->key = 17;
        seventeen->left = NULL;
        seventeen->right= NULL;
    
        Node* twenty = new Node();
        twenty->key = 20;
        twenty->left = NULL;
        twenty->right= NULL;
    
        sixteen->left = eight;
        sixteen->right = seventeen;
    
        nineteen->right = twenty;
    
        /*
         * Level 4
         */
        Node* seven = new Node();
        seven->key = 7;
        seven->left = NULL;
        seven->right= NULL;
    
        eight->left = seven;
    
        return eighteen;
    }
    
    void placeIntoVector(vector<int>& vec, Node* node){
        if (node == NULL)
            return;
    
        placeIntoVector(vec, node->left);
        vec.push_back(node->key);
        placeIntoVector(vec, node->right);
    }
    
    void printInOder(Node* node){
        if (node == NULL)
            return;
    
        printInOder(node->left);
        cout << node->key << " ";
        printInOder(node->right);
    }
    
    void deleteInPostOrder(Node* node){
        if (node == NULL)
            return;
    
        deleteInPostOrder(node->left);
        deleteInPostOrder(node->right);
        delete node;
    }
    
    
    /* Helper function that allocates a new node with the
       given data and NULL left and right pointers. */
    Node* newNode(int data)
    {
        Node* node = new Node();
        node->key = data;
        node->left = NULL;
        node->right = NULL;
    
        return node;
    }
    

    【讨论】:

      猜你喜欢
      • 2019-03-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-08-24
      • 2023-03-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多