您的位置:首页 > 理论基础 > 数据结构算法

树的非递归遍历(深度优先:前|中|后序遍历) & (广度优先:层次遍历)

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: 

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.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
相关文章推荐