Left Fortune law books "Programmer Code Interview Guide" --3_01 respectively, to achieve binary tree preorder using recursive and non-recursive manner, in order and post-order traversal [★★★]

[Title]
recursive and non-recursive manner, respectively, in accordance with the first binary sequence, sequence, and print all subsequent nodes. We agreed: preorder order to root, left and right; preorder order is left, root, right-; post-order traversal order of left, right, root.

  1 #include <iostream>
  2 #include <queue>
  3 #include <vector>
  4 #include <stack>
  5 using namespace std;
  6 struct Node
  7 {
  8     int v;
  9     Node *r, *l;
 10     Node(int a = -1) :v(a), r(nullptr), l(nullptr) {}
 11 };
 12 Node* root = nullptr;
 13 vector<int>res;
 14 void creatTree()
 15 {
 16     root = new Node(1);
 17     root->l = new Node(2);
 18     root->r = new Node(3);
 19     root->l->l = new Node(4);
 20     root->l->r = new Node(5);
 21     root->r->l = new Node(6);
 22     root->r->r = new Node(7);
 23 }
 24 ///////////// Recursively ///////////////
 25  void preOrder (the Node * the root) // preorder traversal 
26 is  {
 27      IF (the root == nullptr a)
 28          return ;
 29      res.push_back (the root -> V);
 30      preOrder (directory root-> L);
 31 is      preOrder (directory root-> R & lt);
 32  }
 33 is  void inOrder (the Node * the root) // preorder 
34 is  {
 35      IF (the root == nullptr a)
 36          return ;
 37 [      preOrder (directory root-> L);
38 is      res.push_back (directory root-> V);
 39      preOrder (directory root-> R & lt);
 40  }
 41 is  void lastOrder (the Node * the root) // postorder 
42 is  {
 43 is      IF (the root == nullptr a)
 44 is          return ;
 45      lastOrder (directory root-> L);
 46 is      lastOrder (directory root-> R & lt);
 47      res.push_back (directory root-> V);
 48  }
 49  //////////// / nonrecursive mode //// ///// // 
50  void NpreOrder (the Node * root) // preorder traversal
51  // 1. Application of a new stack, referred to as a stack. The head is then pressed into the head node in the stack.
52  // 2. ejected from the stack in the stack node, referred to as cur, then print the value of cur node, then the node cur right child (not empty then) be pressed into the stack, and finally left the cur children ( is not empty) is pressed into the stack.
53  // 3. Step 2 is repeated until the stack is empty, the entire process ends. 
54 is  {
 55      IF (the root == nullptr a)
 56 is          return ;
 57 is      Stack <* the Node> S;
 58      s.push (the root);
 59      the while (! S.empty ())
 60      {
 61 is          the Node * P = s.top ( );
 62 is          s.pop ();
 63 is          res.push_back (p-> V);
64          IF (! P-> = R & lt nullptr a)
 65              s.push (p-> R & lt);
 66          IF (! P-> L = nullptr a)
 67              s.push (p-> L);
 68      }
 69  }
 70  
71 is  void NinOrder (the Node * the root) // preorder
 72  // 1. application of a new stack, referred to as a stack. Initially, so that the variable cur = head.
73  // 2. cur first node pushed onto the stack, the entire subtree of nodes to cur the head, the left margin are sequentially pushed onto the stack, i.e. kept so cur = cur.left, then repeat steps 2.
74  // 3. Step 2 is repeated, until the discovery of cur is empty, then pop a node from the stack, referred to as a node.
75  // print the value node, and let cur = node.right, and then continue to repeat step 2.
76 // 4. When the stack is empty and cur is empty, the process stops. 
77  {
 78      IF (the root == nullptr a)
 79          return ;
 80      Stack <* the Node> S;
 81      the Node * P = the root;
 82      the while (!! P = nullptr a || s.empty ())
 83      {
 84          IF (P ! = nullptr a)
 85          {
 86              s.push (P);
 87              P = p-> L;
 88          }
 89          the else 
90          {
 91 is              P =s.top ();
 92              s.pop ();
 93              res.push_back (p-> V);
 94              P = p-> R & lt;
 95          }
 96      }
 97  }
 98  void NlastOrder1 (the Node * the root) // subsequent traverse
 99  // introduce implementation postorder with two stacks, the specific process is as follows:
 100  // 1. application a stack, denoted as sl, then the head node sl is pressed into the head.
101  // 2. ejected from sl nodes recorded as cur, cur then turn left child and right child sl pushed in.
102  // 3. In the process, each node is ejected from s1 and s2 are put.
103  // 4. repeating steps 2 and 3 until s1 is empty, the process stops.
104  //5. sequentially ejected from the print node and the s2, is the order in postorder printing. 
105  {
 106      IF (the root == nullptr a)
 107          return ;
 108      Stack <* the Node> S1, S2;
 109      s1.push (the root);
 110      the Node * P;
 111      the while (! S1.empty ())
 112      {
 113          P = s1.top ();
 114          s1.pop ();
 115          s2.push (P);
 1 16          IF (! p-> L = nullptr a)
 117              s1.push (p-> L);
 1 18          IF (p-> r! =nullptr a)
 119              s1.push (p-> R & lt);
 120      }
 121      the while ! ( s2.empty ())
 122      {
 123          P = s2.top ();
 124          s2.pop ();
 125          res.push_back (p- > V);
 126      }
 127  }
 128  void NlastOrder2 (the Node * the root) // postorder
 129  // Finally, only after the process of traversing a stack implementation procedure is as follows:
 130  // 1. application a stack, referred to as a stack, the head node pressed into the stack, and set two variables h and c. Throughout the process, and h representative of the last pop-up print node, the node C on behalf of the stack the stack, initially h is head node, c is null.
131  //2. Let c each equal to the current stack does not pop the top of the stack, but this time the node divided into the following three cases from the stack.
132  // ① left child if c is not null, and not equal to c h left child, right child does not mean c, c left put the children pressed into the stack in. Specifically explain the reason for this, first of h is the meaning of the latest pop and print nodes, so if h is equal to c left child or right child, explained c left subtree and the right subtree has been printed, not at this time c should then left the child in the stack. Otherwise, indicating the left subtree not treated, then the time will be pressed into the left child c of the stack.
133  // ② ① If the condition is not satisfied, and c is the right child is not null, h c is not equal to the right child, put the child right c pressed into the stack. Meaning that if h is equal to c the right child, explained c right subtree has been printed, this time should not c then right into the stack in children. Otherwise, the explanation has not treated the right subtree, then the right child c pressed into the stack.
134  // ③ If the condition ① and ② conditions are not established, indicating c left subtree and right subtree have been printed, then pop up from the stack and prints c, and then make h = c.
135  // 3. Step 2 is repeated until the stack is empty, the process stops. 
136  {
 137      IF (the root == nullptr a)
 138          return ;
 139      Stack <* the Node> S;
 140     s.push(root);
141     Node* p;//root==h  p==c
142     while (!s.empty())
143     {
144         p = s.top();
145         if (p->l != nullptr && root != p->l && root != p->r)
146             s.push(p->l);
147         else if (p->r != nullptr && root != p->r)
148             s.push(p->r);
149         else
150         {
151             res.push_back(p->v);
152             s.pop();
153             root = p;
154         }
155     }
156 }
157 
158 int main()
159 {
160 
161 }

 

Guess you like

Origin www.cnblogs.com/zzw1024/p/11445161.html