使用递归

时间:2016-10-28 13:06:50

标签: c++ recursion binary-tree preorder postorder

我在前后订单中制作二叉树形式。

预订:“vwbcyznamlp”

订购后:“cbznywmplav”

为此目的的逻辑

V W B C Y Z N A M L P
C B Z N Y W M P L A V

首先,注意根是V,因为它既是预购的第一个,也是后序的最后一个:

V W B C Y Z N A M L P
  C B Z N Y W M P L A V

然后看看W和A,他们分别是第一个留下孩子和根的最后一个孩子。预序中的A标记遍历从根的左子树转换到根的右子树的位置。后序中的W标记相同的位置。请注意,当您分割遍历时,A和W是相邻的位置:

V W B C Y Z N    A M L P
  C B Z N Y W    M P L A V

现在您只需要为序列解决同样的问题:

W B C Y Z N    
C B Z N Y W

A M L P
M P L A

例如,第一个序列的下一步是:

W B C    Y Z N    
  C B    Z N Y W

我正在通过递归制作树,但在递归中的一些条件我不认识。因此,当我通过preoder遍历这棵树时,答案是不正确的。

通过递归制作树后答案是

预订:“vwbccyznaml”

我所做的代码就在这里

首先我制作了Tree Struct

  #include <iostream>
  #include <string>
  #include <stack>
  using namespace std;
  struct TreeNode
  {
  public:
   string enteredData;
   struct TreeNode *left;
   struct TreeNode *right;
   struct TreeNode* newNode(string data)
    {
     struct TreeNode* temp = new TreeNode();
     temp->enteredData = data;
     temp->left = temp->right = NULL;
     return temp;
   }
  };
  struct TreeNode* Buildingtree(string preorder, string postorder);

附加功能

//for returning tree
struct TreeNode *constructTree(string preorder, string postorder)
{
int preIndex = 0;
return Buildingtree(preorder, postorder);
}


//for pre-order traversal
void printPreorder(struct TreeNode* node)
{
if (node == NULL)
    return;
cout << node->enteredData << " ";
printPreorder(node->left);
printPreorder(node->right);
}


//for building tree from given preorder and postorder
struct TreeNode* Buildingtree(string preorder, string postorder)
{
string newPreoderForFunction = "";
string newPostoderForFunction = "";
struct TreeNode* root = new TreeNode();
string rightpreorder;
string rightpostorder;
static stack<string> u;
int indexForPreOder = 1;
int indexForPostOrder = postorder.length() - 2;
if (indexForPostOrder <= -1)
{
    string g;
    if (preorder != "")
        g.assign(1, preorder[0]);
    else
        g.assign(1, postorder[0]);
    root = root->newNode(g);
    return root;
 }
 else
 {
    string findingCharFromPreorder;
    findingCharFromPreorder.assign(1, preorder[indexForPreOder]);
    string findingCharFromPostorder;
    findingCharFromPostorder.assign(1, postorder[indexForPostOrder]);
    size_t found;
    string h = "";
    if (preorder != "")
        h.assign(1, preorder[0]);
    else if (postorder != "")
        h.assign(1, postorder[0]);
    root = root->newNode(h);
    if ((found = preorder.find(findingCharFromPostorder)) != string::npos)
    {
        newPreoderForFunction = preorder.substr(1, found - 1);
        rightpreorder = preorder.substr(found, preorder.length() - 1);
        u.push(rightpreorder);
    }
    if ((found = postorder.find(findingCharFromPreorder)) != string::npos)
    {

        newPostoderForFunction = postorder.substr(0, found + 1);

        rightpostorder = postorder.substr(found + 1, indexForPostOrder);

        if (rightpostorder.length() > 1)
            rightpostorder.erase(rightpostorder.length() - 1);
        u.push(rightpostorder);

    }
  }
  root->left = Buildingtree(newPreoderForFunction, newPostoderForFunction);
  string a;
  string b;
  a = u.top();
  u.pop();
  b = u.top();
  u.pop();
  root->right = Buildingtree(b, a);
  return root;
}

主要功能

int main()
{
string a1 = "vwbcyznamlp";
string a2 = "cbznywmplav";
struct TreeNode *root = constructTree(a1,a2);
printPreorder(root);
cout << endl;
system("pause");
}

0 个答案:

没有答案