void BT_InOrderNoRec(pTreeT root){ stack<treeT *> s; while ((NULL != root) || !s.empty()) { if (NULL != root) { s.push(root); root = root->left; } else { root = s.top(); visit(root); s.pop(); root = root->right; } }}自己實現代碼(比較粗糙):
順序訪問每個節點,然后將右節點插入棧中。然后將當前節點變換為左節點。知道當前節點為空,才會作出棧操作。偽代碼如下: void BT_PreOrderNoRec(pTreeT root){ stack<treeT *> s; while ((NULL != root) || !s.empty()) { if (NULL != root) { visit(root); s.push(root); root = root->left; } else { root = s.top(); s.pop(); root = root->right; } }}自己實現的代碼:
posted on 2011-04-10 10:42 kahn 閱讀(306) 評論(0) 編輯 收藏 引用 所屬分類: 算法相關
Powered by: C++博客 Copyright © kahn