Four strategies for non-recursive traversal of Binary Trees-first, middle, last, and Sequence

Source: Internet
Author: User

Recursive Algorithms that traverse Binary Trees are easy to understand, but non-recursive loop algorithms are not easy to see at a glance. The following five algorithms are written according to Yan Weimin's "Data Structure" and USTC's instructor Zhang Yu's handout. Some of them have been modified.

 

First traverse Binary Tree Algorithm 1

// Copyright (c) 2009, Alex Zhong. all rights reserved. </P> <p> Status preordertraverse (bitree T, status (visit *) (telemtype E) <br/>{< br/> initstack (s ); <br/> push (S, T); <br/> P = T; // or gettop (S, P); <br/> while (! Stackempty (s) & P) <br/>{< br/> while (p) <br/>{< br/> If (! Visit (p-> data) <br/> return error; <br/> push (p-> rchild); <br/> P = p-> lchild; <br/>}< br/> POP (S, P); <br/>}< br/> destroystack (s); <br/> Return OK; <br/>}

 

First traverse binary tree algorithm 2

// Copyright (c) 2009, Alex Zhong. all rights reserved. </P> <p> Status preordertraverse (bitree T, status (visit *) (telemtype) <br/>{< br/> initstack (s ); <br/> push (S, T); <br/> P = T; <br/> while (! Stackempty (s) & P) <br/>{< br/> while (p) <br/>{< br/> If (! Visit (p-> data) <br/> return error; <br/> push (S, P); <br/> P = p-> lchild; <br/>}< br/> If (! Stackempty (s) <br/>{< br/> POP (S, P); <br/> P = p-> rchild; <br/>}< br/> destroystack (s); <br/> Return OK; <br/>}< br/>

 

Ordinal traversal Binary Tree Algorithm

// Copyright (c) 2009, Alex Zhong. all rights reserved. </P> <p> Status inordertraverse (bitree T, status (visit *) (telemtype E) <br/>{< br/> initstack (s ); <br/> push (S, T) '<br/> P = T; <br/> while (! Stackempty (s) & P) <br/>{< br/> while (p) <br/>{< br/> push (S, P ); <br/> P = p-> lchild; <br/>}< br/> POP (S, P); <br/> If (! Visit (p-> data) <br/> return error; <br/> P = p-> rchild; <br/>}< br/> destroystack (s ); <br/> Return OK; <br/>}

 

Post-order traversal Binary Tree Algorithm

// Copyright (c) 2009, Alex Zhong. all rights reserved. </P> <p> Status postordertraverse (bitree T, status (visit *) (telemtype E) <br/>{< br/> initstack (s ); <br/> push (S, T); <br/> P = T; <br/> while (! Stackempty (s) & P) <br/>{< br/> If (P) <br/> {// always go to the leftmost node <br/> push (S, P); <br/> P = p-> lchild; <br/>}< br/> else <br/> {// If P is null <br/> gettop (S, P ); <br/> If (p-> rchild & P-> rchild! = R) <br/> {// P: The right child with the right child or P is not just accessed <br/> P = p-> rchild; <br/> push (S, P); <br/> P = p-> lchild; <br/>}< br/> else <br/> {// P no right child or P right child has just been accessed <br/> POP (S, P ); <br/> visit (p-> data); <br/> r = P; <br/> P = NULL; <br/>}< br/> destroystack (s); <br/> Return OK; <br/>}

 

Sequence traversal Binary Tree Algorithm

// Copyright (c) 2009, Alex Zhong. all rights reserved. </P> <p> Status levelordertraverse (bitree T, status (visit *) (telemtype E) <br/>{< br/> initqueue (Q ); <br/> P = T; <br/> If (p) <br/> {<br/> If (! Visit (p-> data) <br/> return error; <br/> enqueue (Q, P-> lchild); <br/> enqueue (Q, p-> rchild); <br/> while (q) <br/>{< br/> dequeue (Q, P); <br/> If (! Visit (p-> data) <br/> return error; <br/> If (p-> lchild) <br/> enqueue (Q, P-> lchild ); <br/> If (p-> rchild) <br/> enqueue (Q, P-> rchild ); <br/>}< br/> Return OK; <br/>}

 

Welcome to discussion, criticism, and correction!

Alex Zhong

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.