{"id":620,"date":"2013-09-13T12:45:26","date_gmt":"2013-09-13T04:45:26","guid":{"rendered":"http:\/\/blog.dayandcarrot.net\/?p=620"},"modified":"2013-09-13T12:45:26","modified_gmt":"2013-09-13T04:45:26","slug":"%e6%9f%90%e5%b9%b4%e7%a0%94%e7%a9%b6%e7%94%9f%e5%a4%8d%e8%af%95%e9%a2%98%e4%b9%8b%e4%ba%8c","status":"publish","type":"post","link":"https:\/\/dayandcarrot.space\/?p=620","title":{"rendered":"\u67d0\u5e74\u7814\u7a76\u751f\u590d\u8bd5\u9898\u4e4b\u4e8c"},"content":{"rendered":"<p>\u6839\u636e\u4e8c\u53c9\u6811\u7684\u5148\u5e8f\u548c\u4e2d\u5e8f\u5e8f\u5217\uff0c\u8bbe\u8ba1\u7b97\u6cd5\u91cd\u6784\u51fa\u8fd9\u68f5\u4e8c\u53c9\u6811\u3002\uff0840\u5206\uff09<br \/>\n\u5df2\u77e5\u4e00\u68f5\u4e8c\u53c9\u6811\u7684\u5148\u5e8f\u904d\u5386\u5e8f\u5217\u662fABECDFGHIJ\uff0c\u4e2d\u5e8f\u904d\u5386\u5e8f\u5217\u662fEBCDAFHIGJ\uff0c\u8bf7\u8bbe\u8ba1\u4e00\u4e2a\u7b97\u6cd5\uff0c\u5c06\u8fd9\u68f5\u4e8c\u53c9\u6811\u7684\u7ed3\u6784\u91cd\u6784\u51fa\u6765\uff0c\u5e76\u4e14\u8f93\u51fa\u5b83\u7684\u4e09\u79cd\u6df1\u5ea6\u4f18\u5148\u904d\u5386\u7684\u7ed3\u679c\uff08\u5373\u5148\u5e8f\u3001\u4e2d\u5e8f\u548c\u540e\u5e8f\u5e8f\u5217\uff09\u3002<br \/>\n====================================<br \/>\n\u90fd\u662f\u633a\u57fa\u7840\u7684\u9898\u76ee\u561b~<br \/>\n\u5148\u5e8f\u904d\u5386\u662fNLR\uff0c\u4e2d\u5e8f\u904d\u5386\u662fLRN<br \/>\n\u6240\u4ee5\u7ed9\u4e86\u4e00\u4e2a\u5148\u5e8f\u5e8f\u5217\uff0c\u6bd4\u5982\u9898\u76ee\u4e2d\u90a3\u4e2a\uff0c\u90a3\u7b2c\u4e00\u4e2a\u5143\u7d20\uff08A\uff09\u80af\u5b9a\u5c31\u662f\u90a3\u4e2aN\u5566<br \/>\n\u7136\u540e\u518d\u4e2d\u5e8f\u5e8f\u5217\u4e2d\u627e\u5230N\uff0c\u5c31\u80fd\u628a\u4e2d\u5e8f\u5e8f\u5217\u5288\u6210\u4e24\u6bb5\u4e86\uff0c(LEFT)N(RIGHT)<br \/>\n\u7136\u540e\u5c31\u9012\u5f52\uff0c\u5f04\u5230\u5230\u5e95\u4e86\u4e3a\u6b62\uff0c\u563f\u563f:)<br \/>\n\u4e0d\u8fc7\u81ea\u5df1\u5b9e\u73b0\u8d77\u6765\u8fd8\u662f\u633a\u86cb\u75bc\u7684\uff0c\u80fd\u529b\u6709\u9650\u5509\u5509\u5509<br \/>\n\u8f93\u51fa\u53ea\u641e\u4e86\u4e2a\u540e\u5e8f\u7684\uff0c\u5176\u4ed6\u7c7b\u4f3c\u53cd\u6b63\u3002<br \/>\n====================================<br \/>\n<code lang=\"c++\"><br \/>\n#include <queue><br \/>\n#include <algorithm><br \/>\n#include <iostream><br \/>\n#include <string><br \/>\nusing namespace std;<br \/>\nstruct Node<br \/>\n{<br \/>\n\tchar value;<br \/>\n\tNode *pLeft;<br \/>\n\tNode *pRight;<br \/>\n\tNode()<br \/>\n\t{<br \/>\n\t\tpLeft = NULL;<br \/>\n\t\tpRight = NULL;<br \/>\n\t}<br \/>\n};<br \/>\nvoid splitLNR(queue<char> input, queue<char> &left, queue<char> &right, char N)<br \/>\n{<br \/>\n\tbool flag = false;<br \/>\n\twhile(!input.empty())<br \/>\n\t{<br \/>\n\t\tchar currVal = input.front();<br \/>\n\t\tif(currVal == N)<br \/>\n\t\t{<br \/>\n\t\t\tflag = true;<br \/>\n\t\t\tinput.pop();<br \/>\n\t\t\tcontinue;<br \/>\n\t\t}<br \/>\n\t\tif(!flag)<br \/>\n\t\t{<br \/>\n\t\t\tleft.push(currVal);<br \/>\n\t\t}<br \/>\n\t\telse<br \/>\n\t\t{<br \/>\n\t\t\tright.push(currVal);<br \/>\n\t\t}<br \/>\n\t\tinput.pop();<br \/>\n\t}<br \/>\n}<br \/>\nvoid splitNLR(queue<char> input, queue<char> &left, queue<char> &right, int pos)<br \/>\n\/\/ RIght subseries starts at position = pos<br \/>\n{<br \/>\n\tint count = 0;<br \/>\n\twhile(!input.empty())<br \/>\n\t{<br \/>\n\t\tchar currVal = input.front();<br \/>\n\t\tif(count == 0)<br \/>\n\t\t{<br \/>\n\t\t\tinput.pop();<br \/>\n\t\t\tcount++;<br \/>\n\t\t\tcontinue;<br \/>\n\t\t}<br \/>\n\t\telse if(count < pos)\n\t\t{\n\t\t\tleft.push(currVal);\n\t\t}\n\t\telse\n\t\t{\n\t\t\tright.push(currVal);\n\t\t}\n\t\tinput.pop();\n\t\tcount++;\n\t}\n}\nvoid printLRN(Node *root)\n{\n\tif(root == NULL)\n\t\treturn;\n\tprintLRN(root->pLeft);<br \/>\n\tprintLRN(root->pRight);<br \/>\n\tcout << root->value << \" \";\n}\nvoid run( queue<char> A, queue<char> B, Node* root)<br \/>\n\/\/A: \u5148\u5e8f\uff0c B\uff1a\u4e2d\u5e8f<br \/>\n{<br \/>\n\tif(A.empty())<br \/>\n\t\treturn;<br \/>\n\tchar N = A.front();<br \/>\n\troot->value = N;<br \/>\n\tqueue<char> leftB, rightB, leftA, rightA;<br \/>\n\tsplitLNR(B, leftB, rightB, N);<br \/>\n\tsplitNLR(A, leftA, rightA, leftB.size() + 1);<br \/>\n\tif(leftB.empty() && rightB.empty())<br \/>\n\t{<br \/>\n\t\t\/\/N is the only element<br \/>\n\t\treturn;<br \/>\n\t}<br \/>\n\tif(!leftA.empty())<br \/>\n\t{<br \/>\n\t\tNode *newNode = new Node;<br \/>\n\t\troot->pLeft = newNode;<br \/>\n\t\trun(leftA, leftB, newNode);<br \/>\n\t}<br \/>\n\tif(!rightA.empty())<br \/>\n\t{<br \/>\n\t\tNode *newNode = new Node;<br \/>\n\t\troot->pRight = newNode;<br \/>\n\t\trun(rightA, rightB, newNode);<br \/>\n\t}<br \/>\n}<br \/>\nint main()<br \/>\n{<br \/>\n\tqueue<char> inputA, inputB;<br \/>\n\tchar *cNLR = \"ABECDFGHIJ\";<br \/>\n\tchar *cLNR = \"EBCDAFHIGJ\";<br \/>\n\tint i;<br \/>\n\tfor(i=0; i<strlen(cNLR) ; i++)\n\t{\n\t\tinputA.push(cNLR[i]);\n\t\tinputB.push(cLNR[i]);\n\t}\n\tNode rootNode;\n\tNode *ptr = &rootNode;\n\trun(inputA, inputB, ptr);\n\tptr = &rootNode;\n\tprintLRN(ptr);\n\treturn 0;\n}\n<\/code><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6839\u636e\u4e8c\u53c9\u6811\u7684\u5148\u5e8f\u548c\u4e2d\u5e8f\u5e8f\u5217\uff0c\u8bbe\u8ba1\u7b97\u6cd5\u91cd\u6784\u51fa\u8fd9\u68f5\u4e8c\u53c9\u6811\u3002\uff0840\u5206\uff09 \u5df2\u77e5\u4e00\u68f5\u4e8c\u53c9\u6811\u7684\u5148\u5e8f\u904d\u5386\u5e8f\u5217\u662fABECDFGHIJ\uff0c\u4e2d\u5e8f\u904d\u5386\u5e8f\u5217\u662fEBCDAFHIGJ\uff0c\u8bf7\u8bbe\u8ba1\u4e00\u4e2a\u7b97\u6cd5\uff0c\u5c06\u8fd9\u68f5\u4e8c\u53c9\u6811\u7684\u7ed3\u6784\u91cd\u6784\u51fa\u6765\uff0c\u5e76\u4e14\u8f93\u51fa\u5b83\u7684\u4e09\u79cd\u6df1\u5ea6\u4f18\u5148\u904d\u5386\u7684\u7ed3\u679c\uff08\u5373\u5148\u5e8f\u3001\u4e2d\u5e8f\u548c\u540e\u5e8f\u5e8f\u5217\uff09\u3002 ==================================== \u90fd\u662f\u633a\u57fa\u7840\u7684\u9898\u76ee\u561b~ \u5148\u5e8f\u904d\u5386\u662fNLR\uff0c\u4e2d\u5e8f\u904d\u5386\u662fLRN \u6240\u4ee5\u7ed9\u4e86\u4e00\u4e2a\u5148\u5e8f\u5e8f\u5217\uff0c\u6bd4\u5982\u9898\u76ee\u4e2d\u90a3\u4e2a\uff0c\u90a3\u7b2c\u4e00\u4e2a\u5143\u7d20\uff08A\uff09\u80af\u5b9a\u5c31\u662f\u90a3\u4e2aN\u5566 \u7136\u540e\u518d\u4e2d\u5e8f\u5e8f\u5217\u4e2d\u627e\u5230N\uff0c\u5c31\u80fd\u628a\u4e2d\u5e8f\u5e8f\u5217\u5288\u6210\u4e24\u6bb5\u4e86\uff0c(LEFT)N(RIGHT) \u7136\u540e\u5c31\u9012\u5f52\uff0c\u5f04\u5230\u5230\u5e95\u4e86\u4e3a\u6b62\uff0c\u563f\u563f:) \u4e0d\u8fc7\u81ea\u5df1\u5b9e\u73b0\u8d77\u6765\u8fd8\u662f\u633a\u86cb\u75bc\u7684\uff0c\u80fd\u529b\u6709\u9650\u5509\u5509\u5509 \u8f93\u51fa\u53ea\u641e\u4e86\u4e2a\u540e\u5e8f\u7684\uff0c\u5176\u4ed6\u7c7b\u4f3c\u53cd\u6b63\u3002 ==================================== #include #include #include #include using namespace std; struct Node { char value; Node *pLeft; Node *pRight; Node() { pLeft = NULL; pRight = NULL; } }; void splitLNR(queue input, queue &#038;left, queue &#038;right, char N) { bool flag = false; while(!input.empty()) { char currVal [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4],"tags":[],"class_list":["post-620","post","type-post","status-publish","format-standard","hentry","category-study"],"_links":{"self":[{"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=\/wp\/v2\/posts\/620","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=620"}],"version-history":[{"count":0,"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=\/wp\/v2\/posts\/620\/revisions"}],"wp:attachment":[{"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=620"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=620"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=620"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}