5
A
回答
1
int num=1;
struct node
{
int flag;
int val;
node *left,*right,*parent;
};
node* tree::InorderTraversalusingInBuiltstack()
{
stack<node *> p;
node *current=root;
while(!p.empty()|| current!=NULL)
{
while(current!=NULL)
{
p.push(current);
current=current->left;
}
current=p.top();
if(current->flag==num)
{
return current;
}
p.pop();
current=current->right;
}
}
void tree::StackUsingBST()
{
node *q;
while(root!=NULL)
{
num--;
q=InorderTraversalusingInBuiltqueue();
node *a=q->parent;
if(a!=NULL)
{
if(a->left==q)
a->left=NULL;
else
a->right=NULL;
q->parent=NULL;
delete q;
disp();
cout<<endl;
}
else
{
delete q;
root=NULL;
}
}
}
這裏我的方法是,我修改了數據結構的位,通過使用標誌變量 作爲全局變量;
假設第i個插入8則其相應的標誌值是1 然後插入12其標記值= 2 然後插入3其標記值= 3
現在序使用BST作爲堆棧我必須刪除最後插入的元素,根據我的算法具有最高的標誌值。
另請注意,插入的最後一個元素不會有任何子元素,因此它的刪除非常容易。
爲了找到可用於節點的最高標誌值,我使用了比其遞歸遍歷更好的棧來進行反轉。
找到與最高標誌值對應的節點後,刪除該節點。 重複此過程直到root = NULL。
相關問題
- 1. 使用堆棧實現C++
- 2. 堆棧實現
- 3. C++堆棧實現
- 4. 通用堆棧實現
- 5. 用Python實現堆棧
- 6. 使用鏈接列表實現堆棧
- 7. 使用指針實現堆棧
- 8. 使用2個隊列實現堆棧
- 9. 使用堆棧實現數組
- 10. 使用2堆棧實現隊列
- 11. 使用數組的C++實現堆棧
- 12. 使用堆棧快速排序實現
- 13. 在MySQL中實現堆棧
- 14. 在java中實現堆棧
- 15. 實現無堆棧的Python
- 16. 在C++中實現堆棧
- 17. 簡單堆棧實現
- 18. Python TCP堆棧實現
- 19. 在C++中實現堆棧
- 20. 在c中實現堆棧
- 21. java.lang.ArrayIndexOutOfBoundsException:0(堆棧實現)
- 22. 堆棧的數組實現
- 23. C中的堆棧實現
- 24. C堆棧數組實現
- 25. 使用鏈接列表實現堆棧實現
- 26. Monad變換器:用MaybeT(狀態堆棧)實現堆棧機
- 27. MVC:用戶消息堆棧實現
- 28. c中的實現通用堆棧
- 29. 如何用std :: vector實現堆棧?
- 30. 使用堆棧來獲得BST的高度