[英]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.