The beauty of programming -- layered traversal of Binary Trees

Source: Internet
Author: User

Method 1: Start from the root node and press each layer of nodes into an array. cur indicates the current access node, and last indicates the first node in the next layer. You can traverse the array to obtain a hierarchical traversal;

Code:

1 # include <iostream> 2 # include <queue> 3 # include <vector> 4 using namespace STD; 5 6 template <class T> 7 class binarytree 8 {9 struct node 10 {11 t ELEM; 12 node * left; 13 node * right; 14 15 node (const T & X, node * lt = NULL, node * RT = NULL) 16: ELEM (x), left (LT), right (RT) {} 17 ~ Node () {} 18}; 19 20 private: 21 node * root; 22 void makeempty (node * & T); 23 void leveltraverse (node * & T); 24 public: 25 binarytree (node * t = NULL) 26 {27 root = T; 28} 29 void createbinarytree (); 30 void leveltraverse () 31 {32 leveltraverse (Root ); 33} 34 ~ Binarytree () 35 {36 makeempty (Root); 37} 38}; 39 40 // use the queue to construct a binary search tree 41 template <class T> 42 void binarytree <t>:: createbinarytree () 43 {44 queue <node *> que; 45 node * t; 46 t value; 47 48 cout <"Enter root :(-1 represent null) "<": "; 49 CIN> value; 50 51 root = new node (value); 52 que. push (Root); 53 54 t ldata, RDATA; 55 56 while (! Que. empty () 57 {58 T = que. front (); 59 que. pop (); 60 61 cout <"Enter left son of the node" <t-> ELEM <":"; 62 CIN> ldata; 63 cout <"Enter right son of the node" <t-> ELEM <":"; 64 CIN> RDATA; 65 66 If (ldata! =-1) 67 que. Push (t-> left = new node (ldata); 68 if (RDATA! =-1) 69 que. Push (t-> right = new node (RDATA); 70} 71 cout <"construct complement! "<Endl; 72} 73 74 template <class T> 75 void binarytree <t>: makeempty (node * & T) 76 {77 If (t-> left! = NULL) 78 makeempty (t-> left); 79 if (t-> right! = NULL) 80 makeempty (t-> right); 81 Delete t; 82} 83 84 template <class T> 85 void binarytree <t>: leveltraverse (node * & T) 86 {87 If (t = NULL) 88 return; 89 90 vector <node *> arr; 91 int cur = 0; 92 int last = 1; 93 94 arr. push_back (t); 95 while (cur <last) 96 {97 while (cur <last) 98 {99 cout <arr [cur]-> ELEM <'\ T'; 100 if (ARR [cur]-> left! = NULL) 101 arr. push_back (ARR [cur]-> left); 102 If (ARR [cur]-> right! = NULL) 103 arr. push_back (ARR [cur]-> right); 104 + + cur; 105} 106 107 last = arr. size (); 108 cout <Endl; 109} 110} 111 112 int main () 113 {114 binarytree <int> tree; 115 tree. createbinarytree (); 116 tree. leveltraverse (); 117 return 0; 118}
View code

Method 2: this problem can be solved directly using the queue data structure:

 1 template<class T> 2 void binaryTree<T>::levelTraverse(node *&t) 3 { 4     queue<node *>que; 5     que.push(t); 6     while(!que.empty()) 7     { 8         node *q=que.front(); 9         cout<<q->elem<<ends;10         if(q->left!=NULL)11             que.push(q->left);12         if(q->right!=NULL)13             que.push(q->right);14         que.pop();15     }16 }
View code

However, in this case, the hierarchy of the tree cannot be clearly displayed, and a line break must be displayed between the layers;

We can design a tag value to mark a layer that has been traversed, right, into the queue, output a line feed, and then start a new round of traversal;

1 template <class T> 2 void binarytree <t>: leveltraverse (node * & T) 3 {4 queue <node *> que; 5 Node * sign; 6 // tag value 7 sign-> ELEM =-1; 8 que. push (t); 9 que. push (sign); 10 while (que. size ()> 1) 11 {12 node * q = que. front (); 13 cout <q-> ELEM <ends; 14 if (Q-> left! = NULL) 15 que. Push (Q-> left); 16 if (Q-> right! = NULL) 17 que. push (Q-> right); 18 que. pop (); 19 20 // If a tag value is displayed, the line breaks, and the next layer traverses 21 if (que. front ()-> ELEM =-1) 22 {23 que. pop (); 24 que. push (sign); 25 cout <Endl; 26} 27} 28}
View code

If you traverse from bottom to top by depth, you can press the previous result into the stack, press a tag value between each layer, and finally pop the element in the stack;

 

The beauty of programming -- layered traversal of Binary Trees

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.