Binary tree traversal

Source: Internet
Author: User

Binary tree traversal

/* Various traversal of Binary Trees, including first, middle, and later layers, recursion and non-recursion. */# Include <iostream> # include <stack> # include <queue> using namespace std; typedef struct Bi_Node {char data; struct Bi_Node * lchild, * rchild;} Bit_Node, * Bit_Tree; /* because your BiTree type itself is the pointer type pointing to the struct, it will not be deeply copied during parameter passing. If you pass an object parameter and there is a pointer in this object, if it is not added the reference will cause a shortest copy, but this will cause the two pointers to point to the same memory. Therefore, you must write and copy the constructor to implement deep copy. However, if you add a reference to the constructor, it will not be called. copy the constructor to construct a temporary object, but directly reference your real parameter object. Therefore, pointers in the class will not collapse */void creat_node (Bit_Tree & T) // without adding &, the pointer is indeed not assigned a value, thus becoming a wild pointer {c Har data; cin> data; if (data = '#') {T = NULL;} else {T = (Bit_Tree) malloc (sizeof (Bi_Node )); t-> data = data; creat_node (T-> lchild); creat_node (T-> rchild) ;}} void print_node (Bit_Tree T) {if (T-> data! = '#') {Cout <T-> data <"" ;}} void pref_order (Bit_Tree T) // first traverse the recursive root-left-right {if (T! = NULL) {print_node (T); pref_order (T-> lchild); pref_order (T-> rchild) ;}} void _ pref_order (Bit_Tree T) // first traverse non-recursive {stack <Bit_Tree> s; Bit_Tree p = T; while (p |! S. empty () {if (p! = NULL) {s. push (p); cout <p-> data <""; p = p-> lchild;} else {p = s. top (); s. pop (); p = p-> rchild ;}} void in_order (Bit_Tree T) // recursively Traverse left-root-right {if (T! = NULL) {in_order (T-> lchild); print_node (T); in_order (T-> rchild) ;}} void _ in_order (Bit_Tree T) // traverse non-recursion in the middle order {stack <Bit_Tree> s; Bit_Tree p = T; while (p |! S. empty () {if (p! = NULL) {s. push (p); // cout <p-> data <""; p = p-> lchild;} else {p = s. top (); cout <p-> data <""; s. pop (); p = p-> rchild ;}} void post_order (Bit_Tree T) // recursively Traverse left-right-root {if (T! = NULL) {post_order (T-> lchild); post_order (T-> rchild); print_node (T) ;}} typedef struct Bit_Node_Post {char tag; Bit_Tree bit_tree ;} * _ Bit_Node_Post; void _ post_order (Bit_Tree T) // post-order traversal of non-recursive {stack <_ Bit_Node_Post> s; Bit_Tree p = T; _ Bit_Node_Post bt; while (p |! S. empty () {while (p! = NULL) {bt = (_ Bit_Node_Post) malloc (sizeof (Bit_Node_Post); bt-> tag = 'l'; bt-> bit_tree = p; s. push (bt); p = p-> lchild;} while (! S. empty () & (s. top ()-> tag = 'R') {bt = s. top (); s. pop (); cout <bt-> bit_tree-> data <"";} if (! S. empty () {bt = s. top (); bt-> tag = 'R'; p = bt-> bit_tree; p = p-> rchild ;}} void level_order (Bit_Tree T) // level traversal (good) layer by layer, from left to right {queue <Bit_Tree> q; Bit_Tree p = T; q. push (p); while (! Q. empty () {p = q. front (); cout <p-> data <""; q. pop (); if (p-> lchild! = NULL) {q. push (p-> lchild);} if (p-> rchild! = NULL) {q. push (p-> rchild) ;}} int main () {Bit_Tree T; // the pointer to the root node is initialized, and the subsequent T will not be referenced. Creat_node (T); cout <"sequential traversal (recursion)" <endl; pref_order (T); cout <endl; cout <"first-order traversal (non-recursive)" <endl; _ pref_order (T); cout <endl; cout <"middle-order traversal (recursion) "<endl; in_order (T); cout <endl; cout <" central order traversal (non-recursive) "<endl; _ in_order (T ); cout <endl; cout <"post-order traversal (recursion)" <endl; post_order (T); cout <endl; cout <"post-order traversal (non-recursive)" <endl; _ post_order (T); cout <endl; cout <"hierarchical traversal" <endl; level_order (T); cout <endl; return 0 ;}


Bitree T-> defines a Bitree Instance Object: T; Bitree & T-> defines the reference of the Bitree instance object, which is the alias of a defined object and needs to be initialized; /* Reference is a concept in C ++. It is easy for beginners to confuse references with pointers. In the following program, n is a reference of m, and m is a referent ). Int m; int & n = m; n is equivalent to the alias (nickname) of m. Any operation on n is an operation on m. For example, someone named Wang xiaomao, whose nickname is "San Mao ". The reason for saying "three hairs" is actually to say three things to Wang xiaomao. Therefore, n is neither a copy of m nor a pointer to m. In fact, n is m itself. */Bitree * T-> defines the Instance Object Pointer of Bitree and points to an instance object. For the code, see Bitree T; Bitree & T = T; Bitree * T = & T; // & is the address.

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.