Left Fortune law foundation classes 4_4_3 find the successor of a node in a binary tree

Problem:
  find the binary tree successor of a node
  [Title] There is a new binary tree node types is as follows:
  public class the Node {
  public int value;
  public the Node left;
  public the Node right;
  public the Node parent;
  public the Node (int Data) Data this.value = {;}
  }
  the structure of a plurality of parent pointer pointing to a parent node than conventional binary tree node structure.
  Suppose a binary Node type nodes,
  the tree parent pointer for each node are properly directed their parent node,
  parent node to the first null. Only one node to node in a binary tree,
  please realize the function returns the node's successor nodes. In the sequence preorder traversal of the binary tree,
  the next node node is called the successor node section


Solution:
  In order traversal sequence, after each successor node of a node is the node of the node,
  a pioneer in the node is the node in the foregoing sequence order.

  Successor node x is:
    [] if the right subtree is the right subtree of the leftmost node of node
    [if not the right subtree]:
      by pointer precursor, parent back up, and determines whether the node that left subtree of the parent node,
      and if so, the successor node of the parent node for node. No, the parent continues to back up his father.
      If has been found, the successor of this node is empty.

 

Code:

  

  1 #pragma once
  2 #include <iostream>
  3 #include <vector>
  4 #include <queue>
  5 
  6 using namespace std;
  7 
  8 struct Node
  9 {
 10     int val;
 11     Node* lchild;
 12     Node* rchild;
 13     Node* parent;
 14     Node(int a = -1) :val(a), lchild(NULL), rchild(NULL),parent(NULL) {}
 15 };
 16 
 17 void Create(Node*& root, vector<int>num)//层序构造树
 18 {
 19     queue<Node*>q;
 20     root = new Node(num[0]);
 21     q.push(root);
 22     int i = 1;
 23     while (i < num.size() && !q.empty())
 24     {
 25         Node* p = q.front();
 26         q.pop();
 27         if (!p)//空节点p
 28             break;
 29         if (num[i] > 0)
 30         {
 31             p->lchild = new Node(num[i++]);
 32             p->lchild->parent = p;
 33         }
 34         if (num[i] > 0)
 35         {
 36             p->rchild = new Node(num[i++]);
 37             p->rchild->parent = p;
 38         }
 39         q.push(p->lchild);
 40         q.push(p->rchild);
 41     }
 42 is  }
 43 is  
44 is  void MidTravel (the Node * the root)
 45  {
 46 is      IF (! The root)
 47          return ;
 48      MidTravel (directory root-> lchild);
 49      COUT << directory root-> Val << "   " ;
 50      MidTravel (directory root- > rchild);
 51 is  }
 52 is  
53 is the node FindNode * (* the node node)
 54 is  {
 55      IF (node-> rchild) // a right subtree, so the leftmost successor node is the right child of node 
56      {
 57         * P = node- the Node> rchild;
 58          the while (p-> lchild)
 59              P = p-> lchild;
 60          return P;        
 61 is      }
 62 is      // no right subtree
 63      @ successor node for the node is a parent the left subtree of a node 
64      the node F *; // parent pointer 
65      F = node-> parent;
 66      the while (! F)
 67      {
 68          IF (node == F-> lchild)
 69              return F;
 70          node = F;
 71         f = node->parent;
 72     }
 73     return NULL;
 74 }
 75 
 76 void Test()
 77 {
 78     Node* root = NULL;
 79     vector<int>num = { 1,2,3,4,5,6,7,8,9,10,11,12,-1,-1,-1 };
80      the Create (the root, NUM); // sequence structure tree 
81  
82      COUT << " ============= ============= view preorder = " << endl;
 83      MidTravel (the root);
 84      COUT << endl << endl;
 85  
86      the node * P = NULL;
 87      P = directory root-> rchild;         
 88      COUT << " node " << p-> Val << " successor nodes: " ;
 89      P = FindNode (P);
 90      IF (P)
 91 is         << p-COUT> Val << endl;
 92      the else 
93          COUT << " . Empty " << endl;
 94  
95      P = directory root-> lchild-> rchild;
 96      COUT << " node " << p-> Val << " successor nodes: " ;
 97      P = FindNode (P);
 98      IF (P)
 99          COUT p-<<> Val << endl;
 100      the else 
101          COUT << " empty. " << endl;
102 
103      P = directory root-> rchild-> rchild;
 104      COUT << " node " << p-> Val << " successor nodes: " ;
 105      P = FindNode (P);
 106      IF (P)
 107          COUT < <p-> Val << endl;
 108      the else 
109          COUT << " . empty " << endl;
 110  
111  }
 112         

 

Guess you like

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