在 C++ 中创建 n-tree

Posted

技术标签:

【中文标题】在 C++ 中创建 n-tree【英文标题】:Creating n-tree in C++ 【发布时间】:2014-05-26 12:42:56 【问题描述】:

我需要使用向量中可用元素的数量来计算可能组合的数量。例如,如果我有一个 [0]=1 & [1]=1 和 layer=2(树的长度)的向量,那么我应该得到 01 的答案。如果 [0]=2 &[1] =2 和 layer=2,那么答案是 00,01 或 10,11。问题是向量中可能有不同数量的元素,它们的数量也可能不同。我写了一个递归函数,它创建新节点并将对它们的引用推送到向量中,每个节点都有这样一个向量,但问题是我不知道如何编写一个 PRINT/SHOW 函数来显示结果我需要的方式。请给一些建议或推荐要阅读的主题。非常感谢代码推荐。

void addNode(vector<int>currentAmount, int layer, int positionNumber,  Node *&myTree)

    if (myTree==NULL)
    
        myTree = new Node;
        myTree->listOfNodes.push_back(new Node);
        myTree->currentAmount=currentAmount;
    
    if (myTree!=NULL&&layer!=0)
    
        myTree = new Node;
        myTree->positionNumber=positionNumber;
        myTree->layer=layer;
        currentAmount[positionNumber]-=1;
        for (int i=0; i<currentAmount.size();i++)
        
            if (currentAmount[i]!=0) myTree->listOfNodes.push_back(new Node); myTree->positionNumber.push_back(i);
        
        for (int i=0; i<myTree->listOfNodes.size(); i++)
            addNode(currentAmount, layer-1, myTree->positionNumber[i], myTree->listOfNodes[i]);
     


void showTree(Node *&Tree)

    for (int i=0; i<Tree->listOfNodes.size(); i++)
    
        showTree(Tree->listOfNodes[i]);
        cout<<Tree->listOfNodes[i]<<' '<<Tree->positionNumber[i]<<<' '<<Tree->layer<<'\t';
    

【问题讨论】:

【参考方案1】:

我将此代码用于 B-TREE。

/*

 * C++ Program to Implement B-Tree

 */

#include <iostream>

using namespace std;



// A BTree node

class BTreeNode



    private:

        int *keys;

        int t;

        BTreeNode **C;

        int n;

        bool leaf;

    public:

        BTreeNode(int t1, bool leaf1)

        

            t = t1;

            leaf = leaf1;

            keys = new int[2*t-1];

            C = new BTreeNode *[2*t];

            n = 0;

        

        // traverse all nodes in a subtree rooted with this node

        void traverse()

        

            int i;

            for (i = 0; i < n; i++)

            

                if (leaf == false)

                    C[i]->traverse();

                cout << " " << keys[i];

            

            if (leaf == false)

                C[i]->traverse();

        

        void insertNonFull(int k)

        

            int i = n-1;

            if (leaf == true)

            

                while (i >= 0 && keys[i] > k)

                

                    keys[i+1] = keys[i];

                    i--;

                

                keys[i+1] = k;

                n = n+1;

            

            else

            

                while (i >= 0 && keys[i] > k)

                    i--;

                if (C[i+1]->n == 2*t-1)

                

                    splitChild(i+1, C[i+1]);

                    if (keys[i+1] < k)

                        i++;

                

                C[i+1]->insertNonFull(k);

            

        

        void splitChild(int i, BTreeNode *y)

        

            BTreeNode *z = new BTreeNode(y->t, y->leaf);

            z->n = t - 1;

            for (int j = 0; j < t-1; j++)

                z->keys[j] = y->keys[j+t];

            if (y->leaf == false)

            

                for (int j = 0; j < t; j++)

                    z->C[j] = y->C[j+t];

            

            y->n = t - 1;

            for (int j = n; j >= i+1; j--)

                C[j+1] = C[j];

            C[i+1] = z;

            for (int j = n-1; j >= i; j--)

                keys[j+1] = keys[j];

            keys[i] = y->keys[t-1];

            n = n + 1;

        

        BTreeNode *search(int k)

        

            int i = 0;

            while (i < n && k > keys[i])

                i++;

            if (keys[i] == k)

                return this;

            if (leaf == true)

                return NULL;

            return C[i]->search(k);

        

        friend class BTree;

;



// Class BTree

class BTree



    private:

        BTreeNode *root;

        int t;

    public:

        BTree(int _t)

        

            root = NULL;

            t = _t;

        

        void traverse()

        

            if (root != NULL)

                root->traverse();

        

        BTreeNode* search(int k)

        

            return (root == NULL)? NULL : root->search(k);

        

        void insert(int k)

        

            if (root == NULL)

            

                root = new BTreeNode(t, true);

                root->keys[0] = k;

                root->n = 1;

            

            else

            

                if (root->n == 2*t-1)

                

                    BTreeNode *s = new BTreeNode(t, false);

                    s->C[0] = root;

                    s->splitChild(0, root);

                    int i = 0;

                    if (s->keys[0] < k)

                        i++;

                    s->C[i]->insertNonFull(k);

                    root = s;

                

                else

                    root->insertNonFull(k);

            

        

;

// Main

int main()



    BTree t(3);

    t.insert(10);

    t.insert(20);

    t.insert(5);

    t.insert(6);

    t.insert(12);

    t.insert(30);

    t.insert(7);

    t.insert(17);

    cout << "Traversal of the constucted tree is ";

    t.traverse();

    cout<<endl;

    int k = 6;

    cout<<k<<" is ";

    (t.search(k) != NULL)? cout << "Present\n" : cout << "Not Present\n";

    k = 15;

    cout<<k<<" is ";

    (t.search(k) != NULL)? cout << "Present\n" : cout << "Not Present\n";

    return 0;


【讨论】:

以上是关于在 C++ 中创建 n-tree的主要内容,如果未能解决你的问题,请参考以下文章

从 C++ 访问在 python 中创建的 C++ 类

如何使用 C++ 在 Windows 中创建守护线程?

在 C++ 中创建一个列表来保存对象

在 C++、QT 中创建 setup.exe [重复]

是否可以在 C++ 中创建“朋友类”?

试图在 C++ 中创建一个包含结构的数组?