繁体   English   中英

有没有办法通过链表,而不是normall指针将是唯一的?

[英]Is there is a way to go through linked list , where instead of normall pointers will be unique?

我试图在c ++中编写一个二叉树,将链表作为子树和唯一指针编写为这些列表之间的连接。整个树分为两部分:右和左。 有两个指针指向从头部到左侧和右侧叶子。 然后子树中的每个叶子都是一个存储下一个信息的结构:

class Leaf{
private:
    int * leaf_value; 
    int occupancy;
    std::unique_ptr<Leaf> NextLeaf;

public:
    explicit Leaf(int);
    void AppendLeaf(int,std::unique_ptr<Leaf>);

};

Leaf::Leaf(int size) {
    leaf_value = new int (size);
    NextLeaf = nullptr;
    occupancy = 0;
}

其中leaf_value是一个指向内存的指针,它将存储该级别上的所有数字。(因为它是一个二叉树,我们可以知道应该为对象分配的确切大小( 2 ^ current_level ))。 因此,我们将用数字填充该空闲空间,直到占用率小于2 ^ current_level 。然后我们将为下一行元素添加一个新的更深层次。 结构将如下所示:

               1
            /        \
         [2]         [ 3 ]
        /              \
   [4 , 5 ,6 ,7]  [8 , 9 , 10 , 11 ]

方形挂钩中的元素是单叶。 我认为用allwue指针连接所有这些都是一个好主意,因为列表的每个节点仅引用下一个节点,所以实际上所有指针都是唯一的。这是我想要做的简化代码。


int main(){
     Node head;
     head.next = nullptr;
     int value;
     cin << value;
     AddNode(value , head); 
     return 0;
}

/*TreeHead - is another leaf structure that stores pointers to left and right branches 
struct SmartTree{
    int level;
    std::unique_ptr<Leaf> LeftChild = std::make_unique<Leaf>(1);
    std::unique_ptr<Leaf> RightChild = std::make_unique<Leaf>(1);

}
*/
void AddNode(int val , std::unique_ptr<SmartTree> TreeHead){
/*problem with implemantation of this part */
     Node * currentLeaf  = TreeHead;
     Node * previousLeaf = TreeHead;
     while(current != nullptr){
        previousLeaf = currentLeaf;
        currentLeaf = currentLeaf -> next ;
     }
     currentLeaf = new Leaf;
     previous -> next = currentLeaf;
     currentLeaf -> value = val; 
}

但问题是我无法找到正确的方法来通过所有最小的到底,因为独特的指针的单一性。我不确定我可以用move(pointer)功能做到这一点,因为我理解该函数可以将TreeHead所有权借给CurrentLeafe ,存储的值可能会丢失。 所以问题是:是否有办法通过独特的指针或我应该使用不同类型的指针来完成该任务? 非常感谢 !

我通过更改正常的数组叶子[ 1 2 3 ]来改变我的方法。 这意味着现在每个叶子都是一个包含一个元素的对象,所以树看起来不像:

                1
            /        \
         [2]         [ 3 ]
        /              \
   [4 , 5 ,6 ,7]  [8 , 9 , 10 , 11 ]

但是喜欢:

               1
           /       \
          2           3
        /    \      /  \ 
       5      6    7     8 

新树结构:

struct SmartTree {
    int value;
    int childrens;
    bool is_root = false;
    std::unique_ptr<SmartTree> LeftChild;
    std::unique_ptr<SmartTree> RightChild;
};

但我相信这也适用于以前的方法。所以我发现,并非没有帮助,在这种情况下一个好的解决方案是递归,现在功能通过树和添加元素到最后看起来像这样:

std::unique_ptr <SmartTree> InsertLeftChild(std::unique_ptr<SmartTree> tree, std::unique_ptr<SmartTree> left_subtree){
    // left_subtree - node to insert
    tree -> childrens += 1;
    if (tree -> is_root and  tree -> LeftChild == nullptr) tree -> LeftChild = std::move(left_subtree);
    else if (tree -> is_root) tree -> LeftChild = InsertLeftChild(std::move(tree -> LeftChild) , move(left_subtree));
    else if (tree -> LeftChild == nullptr)tree -> LeftChild = std::move(left_subtree);
    else if (tree -> RightChild == nullptr) tree -> RightChild = std::move(left_subtree);
    else if (tree -> LeftChild -> childrens <= tree -> RightChild -> childrens) tree->LeftChild = InsertLeftChild(std::move(tree -> LeftChild) , std::move(left_subtree));
    else if (tree -> LeftChild -> childrens > tree -> RightChild -> childrens)  tree->RightChild = InsertLeftChild(std::move(tree -> RightChild) , std::move(left_subtree));
    return move(tree);
};

这个只是为了插入左孩子而写的。 所以第一个和第二个if检查是否为root,并且这个root是初始化的。 如果它有孩子,则调用函数InsertLeftChilld Passinng aruments,使用move功能会破坏前一个指针,所以在左侧我将所有权归还给LeftChild:

tree -> LeftChild = std::move(left_subtree);

else if (tree -> LeftChild == nullptr)检查是否结束,如果是 - 插入新叶子

else if (tree -> LeftChild -> childrens <= tree -> RightChild -> childrens)检查你应该在哪里引导你的叶子,以防它不是你可以插入它的级别。希望这种方法可以在将来帮助某人。感谢大家的回答!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM