树的非递归遍历(深度优先:前|中|后序遍历) & (广度优先:层次遍历)
2013-08-06 21:58
281 查看
Notes:( http://en.wikipedia.org/wiki/Tree_traversal) (看了几天简单总结下, 多多指教)
1. Because from a given node there is more than one possible next node(it is not a linear data structure),
then, assuming sequential computation(not parallel),some nodes must be deferred-stored in some way
for later visiting. (Stack || Queue)
2. The name given to a particular style of traversal comes from the order in which nodes are visited.Most
simply, does one go down first(DepthFirst: PreOrder | InOrder | PostOrder), or across first(Breadth-First: LevelOrder).
3. The trace of a traversal is called a sequentialization of the tree. Either pre-order or post-order paired
with in-order is sufficient to describe the tree uniquely,while pre-order with post-order leaves some
ambiguity in the tree structure.
Implementations:
PreOrder:
1. Because from a given node there is more than one possible next node(it is not a linear data structure),
then, assuming sequential computation(not parallel),some nodes must be deferred-stored in some way
for later visiting. (Stack || Queue)
2. The name given to a particular style of traversal comes from the order in which nodes are visited.Most
simply, does one go down first(DepthFirst: PreOrder | InOrder | PostOrder), or across first(Breadth-First: LevelOrder).
3. The trace of a traversal is called a sequentialization of the tree. Either pre-order or post-order paired
with in-order is sufficient to describe the tree uniquely,while pre-order with post-order leaves some
ambiguity in the tree structure.
Implementations:
PreOrder:
void PreOrder_Recur(struct node* root) { if (root == NULL) return ; printf("%d ", root->data); Recursive(root->left); Recursive(root->right); } // Iterative Implementation void PreOrder1(struct node* root) { if (root == NULL) return ; stack<BiTree> s; while (root) { printf("%d ", root->data); s.push(root); root = root->left; } while (!s.empty()) { struct node* temp = s.top()->right; s.pop(); while (temp) { printf("%d ", temp->data); s.push(temp); temp = temp->left; } } } void PreOrder2(struct node* root) { if (root == NULL) return ; stack<BiTree> s; s.push(root); while (!s.empty()) { struct node* node = s.top(); printf("%d ", node->data); s.pop(); if (node->right) s.push(node->right); if (node->left) s.push(node->left); } } void PreOrder3(struct node* node) { if (node == NULL) return ; stack<BiTree> s; while (!s.empty() || node != NULL) { if (node != NULL) { printf("%d ", node->data); s.push(node->right); node = node->left; } else { node = s.top(); s.pop(); } } }
void InOrder(struct node* node) { if (node == NULL) return ; stack<BiTree> s; while (node) { s.push(node); node = node->left; } while (!s.empty()) { struct node* temp = s.top()->right; printf("%d ", s.top()->data); s.pop(); while (temp) { s.push(temp); temp = temp->left; } } } void InOrder2(struct node* node) { if (node == NULL) return ; stack<BiTree> s; while (!s.empty() || node) { if (node != NULL) { s.push(node); node = node->left; } else { node = s.top(); printf("%d ", node->data); s.pop(); node = node->right; } } } //using one stack void PostOrder(struct node* node) { if (node == NULL) return ; stack<BiTree> s; s.push(node); BiTree prev = NULL; while (!s.empty()) { struct node* currNode = s.top(); if (prev == NULL || prev->left == currNode || prev->right == currNode) { if (currNode->left) s.push(currNode->left); else if (currNode->right) s.push(currNode->right); } else if (currNode->left == prev) { if (currNode->right) s.push(currNode->right); } else { printf("%d ", currNode->data); s.pop(); } prev = currNode; } } //using two stack. (NLR | NRL | LRN) //using a visited flag (above two to LRN) //For BFS, using queue is easy. //Above 3 questions if needed , I will add it next time.
相关文章推荐
- 二叉树遍历(前序、中序、后序、层次遍历、深度优先、广度优先)
- 二叉树基本操作的递归实现(二叉树建立,先序,中序,后序,深度的递归遍历。广度优先,高度优先的非递归遍历)
- 二叉树遍历(前序、中序、后序、层次、深度优先、广度优先遍历)
- 二叉树遍历(前序、中序、后序、层次、深度优先、广度优先遍历)
- 图的遍历-(深度优先&广度优先)
- Graph(3)--图的遍历(深度优先&广度优先)
- 二叉树的深度优先,广度优先,以及层次遍历算法
- 【数据结构】邻接矩阵表示法的图的深度广度优先遍历递归和非递归遍历
- 【概念】【二叉树】深度优先遍历、广度优先遍历和非递归遍历
- 第五讲 树-1(广度优先遍历二叉树&深度优先遍历二叉树)
- 笔试算法题(10):深度优先,广度优先以及层序遍历 & 第一个仅出现一次的字符
- 二叉树搜索树遍历--前序、中序、后序、深度优先、广度优先遍历的简单实现
- 图的遍历-(深度优先&广度优先)
- 二叉树遍历,深度有限遍历,广度优先遍历,前序中序后续优先遍历,层次遍历
- c语言编程 输出一个无向图的邻接表,邻接矩阵,进行深度和广度优先遍历
- 图——广度优先遍历和深度优先遍历——邻接矩阵表示法
- 深度优先与广度优先遍历文件
- 二叉树 前序、中序、后序、层次遍历及非递归实现 查找、统计个数、比较、求深度的递归实现
- 树之二叉树的建立与四种遍历(前序,中序, 后序, 层次)及树的深度
- 迷宫路径&广度优先求解&深度优先求解&迷宫游戏