简体   繁体   English

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

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

I am trying to write a binary tree in c++ , linked list as subtrees and unique pointers as connections between those lists.Whole tree is divided on two parts : right and left. 我试图在c ++中编写一个二叉树,将链表作为子树和唯一指针编写为这些列表之间的连接。整个树分为两部分:右和左。 There are two pointers that reference going from head to the left and to the right leafes. 有两个指针指向从头部到左侧和右侧叶子。 Then every leaf in subtree is a structure , that stores next information : 然后子树中的每个叶子都是一个存储下一个信息的结构:

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;
}

Where leaf_value is a pointer to memory that will store all numbers on that level .(because it is a binary tree we can ,know the exact size ( 2 ^ current_level ) that should be allocated for objects ). 其中leaf_value是一个指向内存的指针,它将存储该级别上的所有数字。(因为它是一个二叉树,我们可以知道应该为对象分配的确切大小( 2 ^ current_level ))。 So we will fill that free space with numbers until occupancy is lesser than 2 ^ current_level .And after that we will add a new , deeper level for next row of elements. 因此,我们将用数字填充该空闲空间,直到占用率小于2 ^ current_level 。然后我们将为下一行元素添加一个新的更深层次。 The structure will look like this: 结构将如下所示:

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

Where elements in square bruckets are single leafs. 方形挂钩中的元素是单叶。 . I thought that it can be a good idea , to connect them all with uniwue pointers,because every node of list reference only to the next one , so in fact all pointers are unique.Here is simplified code of what i am trying to do. 我认为用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; 
}

But problem is that i can't find the right way to go through all leasts to the bottom one , because of uniquness of unique pointers .I don't sure that i can do that with move(pointer) function , because as i understood that functions work it lend ownership from TreeHead to CurrentLeafe , value stored in could be lost. 但问题是我无法找到正确的方法来通过所有最小的到底,因为独特的指针的单一性。我不确定我可以用move(pointer)功能做到这一点,因为我理解该函数可以将TreeHead所有权借给CurrentLeafe ,存储的值可能会丢失。 So the question is : Is there is a way to go through the unique pointers or should i use different kind of pointers to complete that task? 所以问题是:是否有办法通过独特的指针或我应该使用不同类型的指针来完成该任务? Thank you a lot ! 非常感谢 !

I changed my methods for a little by changing an array leafs [ 1 2 3 ] on normal . 我通过更改正常的数组叶子[ 1 2 3 ]来改变我的方法。 That means that now every leaf is an object that contains one element , so tree looks now not like : 这意味着现在每个叶子都是一个包含一个元素的对象,所以树看起来不像:

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

but like : 但是喜欢:

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

New tree structure : 新树结构:

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

But i am sure that will works with previous method as good too.So i found , not without a help that a good solution in this case is recursion , and now function that going through the tree and adding element to the end looks like so : 但我相信这也适用于以前的方法。所以我发现,并非没有帮助,在这种情况下一个好的解决方案是递归,现在功能通过树和添加元素到最后看起来像这样:

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);
};

This one is writed only for insertion of left childs . 这个只是为了插入左孩子而写的。 So the first and second if checks is it a root and is this root initialize . 所以第一个和第二个if检查是否为root,并且这个root是初始化的。 If it has childs , than recall function InsertLeftChilld . 如果它有孩子,则调用函数InsertLeftChilld Passinng aruments , to function with move will destroy previous pointer , so on the left side i return ownerships to the LeftChild : Passinng aruments,使用move功能会破坏前一个指针,所以在左侧我将所有权归还给LeftChild:

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

else if (tree -> LeftChild == nullptr) checks is it is the end and if it is - inserts new leaf else if (tree -> LeftChild == nullptr)检查是否结束,如果是 - 插入新叶子

else if (tree -> LeftChild -> childrens <= tree -> RightChild -> childrens) checks where you should direct your leaf next in case it isn't level where you can insert it .Hope this method can help someone in future .Thanks to everyone for answers ! else if (tree -> LeftChild -> childrens <= tree -> RightChild -> childrens)检查你应该在哪里引导你的叶子,以防它不是你可以插入它的级别。希望这种方法可以在将来帮助某人。感谢大家的回答!

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

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