{"id":242,"date":"2011-11-01T00:41:00","date_gmt":"2011-10-31T16:41:00","guid":{"rendered":"http:\/\/wangkaixuan.tech\/?p=242"},"modified":"2020-06-06T00:46:54","modified_gmt":"2020-06-05T16:46:54","slug":"%e6%95%b0%e6%8d%ae%e7%bb%93%e6%9e%84_%e6%9f%a5%e6%89%be_%e9%9d%99%e6%80%81%e6%9f%a5%e6%89%be%e6%95%b0%e8%a1%a8_%e5%b9%b3%e8%a1%a1%e4%ba%8c%e5%8f%89%e6%a0%91","status":"publish","type":"post","link":"http:\/\/www.wangkaixuan.tech\/?p=242","title":{"rendered":"\u6570\u636e\u7ed3\u6784_\u67e5\u627e_\u9759\u6001\u67e5\u627e\u6570\u8868_\u5e73\u8861\u4e8c\u53c9\u6811"},"content":{"rendered":"\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"704\" height=\"348\" src=\"http:\/\/wangkaixuan.tech\/wp-content\/uploads\/2020\/06\/0_1320113149Y5f2.gif.jpg\" alt=\"\" class=\"wp-image-243\" srcset=\"http:\/\/www.wangkaixuan.tech\/wp-content\/uploads\/2020\/06\/0_1320113149Y5f2.gif.jpg 704w, http:\/\/www.wangkaixuan.tech\/wp-content\/uploads\/2020\/06\/0_1320113149Y5f2.gif-300x148.jpg 300w, http:\/\/www.wangkaixuan.tech\/wp-content\/uploads\/2020\/06\/0_1320113149Y5f2.gif-546x270.jpg 546w\" sizes=\"auto, (max-width: 704px) 100vw, 704px\" \/><\/figure>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"516\" height=\"497\" src=\"http:\/\/wangkaixuan.tech\/wp-content\/uploads\/2020\/06\/0_13201131558H0p.gif.jpg\" alt=\"\" class=\"wp-image-244\" srcset=\"http:\/\/www.wangkaixuan.tech\/wp-content\/uploads\/2020\/06\/0_13201131558H0p.gif.jpg 516w, http:\/\/www.wangkaixuan.tech\/wp-content\/uploads\/2020\/06\/0_13201131558H0p.gif-300x289.jpg 300w, http:\/\/www.wangkaixuan.tech\/wp-content\/uploads\/2020\/06\/0_13201131558H0p.gif-280x270.jpg 280w\" sizes=\"auto, (max-width: 516px) 100vw, 516px\" \/><\/figure>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"698\" height=\"540\" src=\"http:\/\/wangkaixuan.tech\/wp-content\/uploads\/2020\/06\/0_1320113161lL8P.gif.jpg\" alt=\"\" class=\"wp-image-245\" srcset=\"http:\/\/www.wangkaixuan.tech\/wp-content\/uploads\/2020\/06\/0_1320113161lL8P.gif.jpg 698w, http:\/\/www.wangkaixuan.tech\/wp-content\/uploads\/2020\/06\/0_1320113161lL8P.gif-300x232.jpg 300w, http:\/\/www.wangkaixuan.tech\/wp-content\/uploads\/2020\/06\/0_1320113161lL8P.gif-349x270.jpg 349w\" sizes=\"auto, (max-width: 698px) 100vw, 698px\" \/><\/figure>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"564\" height=\"470\" src=\"http:\/\/wangkaixuan.tech\/wp-content\/uploads\/2020\/06\/0_13201131919McI.gif.jpg\" alt=\"\" class=\"wp-image-246\" srcset=\"http:\/\/www.wangkaixuan.tech\/wp-content\/uploads\/2020\/06\/0_13201131919McI.gif.jpg 564w, http:\/\/www.wangkaixuan.tech\/wp-content\/uploads\/2020\/06\/0_13201131919McI.gif-300x250.jpg 300w, http:\/\/www.wangkaixuan.tech\/wp-content\/uploads\/2020\/06\/0_13201131919McI.gif-324x270.jpg 324w\" sizes=\"auto, (max-width: 564px) 100vw, 564px\" \/><\/figure>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"705\" height=\"545\" src=\"http:\/\/wangkaixuan.tech\/wp-content\/uploads\/2020\/06\/0_13201132046x6u.gif.jpg\" alt=\"\" class=\"wp-image-247\" srcset=\"http:\/\/www.wangkaixuan.tech\/wp-content\/uploads\/2020\/06\/0_13201132046x6u.gif.jpg 705w, http:\/\/www.wangkaixuan.tech\/wp-content\/uploads\/2020\/06\/0_13201132046x6u.gif-300x232.jpg 300w, http:\/\/www.wangkaixuan.tech\/wp-content\/uploads\/2020\/06\/0_13201132046x6u.gif-349x270.jpg 349w\" sizes=\"auto, (max-width: 705px) 100vw, 705px\" \/><\/figure>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"524\" height=\"622\" src=\"http:\/\/wangkaixuan.tech\/wp-content\/uploads\/2020\/06\/0_1320113211SuHR.gif.jpg\" alt=\"\" class=\"wp-image-248\" srcset=\"http:\/\/www.wangkaixuan.tech\/wp-content\/uploads\/2020\/06\/0_1320113211SuHR.gif.jpg 524w, http:\/\/www.wangkaixuan.tech\/wp-content\/uploads\/2020\/06\/0_1320113211SuHR.gif-253x300.jpg 253w, http:\/\/www.wangkaixuan.tech\/wp-content\/uploads\/2020\/06\/0_1320113211SuHR.gif-227x270.jpg 227w\" sizes=\"auto, (max-width: 524px) 100vw, 524px\" \/><\/figure>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"699\" height=\"307\" src=\"http:\/\/wangkaixuan.tech\/wp-content\/uploads\/2020\/06\/0_132011321866Y8.gif.jpg\" alt=\"\" class=\"wp-image-249\" srcset=\"http:\/\/www.wangkaixuan.tech\/wp-content\/uploads\/2020\/06\/0_132011321866Y8.gif.jpg 699w, http:\/\/www.wangkaixuan.tech\/wp-content\/uploads\/2020\/06\/0_132011321866Y8.gif-300x132.jpg 300w, http:\/\/www.wangkaixuan.tech\/wp-content\/uploads\/2020\/06\/0_132011321866Y8.gif-604x265.jpg 604w\" sizes=\"auto, (max-width: 699px) 100vw, 699px\" \/><\/figure>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"704\" height=\"483\" src=\"http:\/\/wangkaixuan.tech\/wp-content\/uploads\/2020\/06\/0_1320113227ES1Y.gif.jpg\" alt=\"\" class=\"wp-image-250\" srcset=\"http:\/\/www.wangkaixuan.tech\/wp-content\/uploads\/2020\/06\/0_1320113227ES1Y.gif.jpg 704w, http:\/\/www.wangkaixuan.tech\/wp-content\/uploads\/2020\/06\/0_1320113227ES1Y.gif-300x206.jpg 300w, http:\/\/www.wangkaixuan.tech\/wp-content\/uploads\/2020\/06\/0_1320113227ES1Y.gif-394x270.jpg 394w\" sizes=\"auto, (max-width: 704px) 100vw, 704px\" \/><\/figure>\n\n\n\n<p>&#8220;BalancedBinaryTree.h&#8221;<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#define LH 1\n#define EH 0\n#define RH -1\n#define OK 0\n#define FAILED 1\n \ntypedef int Status;\n \ntypedef int ElemType;\n \ntypedef struct BSTNode\/\/\u5e73\u8861\u4e8c\u53c9\u6811\u7684\u5b58\u50a8\u7ed3\u6784\n{\n\tElemType data;\n\tint bf;\n\tBSTNode *lchild,*rchild;\n\tBSTNode()\n\t{\n\t\tbf=EH;\n\t\tlchild=rchild=NULL;\n\t}\n}BSTNode,*BSTree;\n \nvoid R_Rotate(BSTree &amp;p)\/\/\u5355\u6b21\u53f3\u65cb\n{\n\tBSTree lc;\n\tlc=p->lchild;\n\tp->lchild=lc->rchild;\n\tlc->rchild=p;\n\tp=lc;\n}\n \nvoid L_Rotate(BSTree &amp;p)\/\/\u5355\u6b21\u5de6\u65cb\n{\n\tBSTree rc;\n\trc=p->rchild;\n\tp->rchild=rc->lchild;\n\trc->lchild=p;\n\tp=rc;\n}\n \nvoid LeftBalance(BSTree &amp;T)\/\/\u5de6\u5e73\u8861\u5904\u7406\n{\n\tBSTree lc=T->lchild,rd;\n\tswitch(lc->bf)\/\/\u5f85\u65cb\u8f6c\u6811\u7684\u5de6\u5b50\u6811\u7684\u5e73\u8861\u56e0\u5b50\u60c5\u51b5\n\t{\n\tcase LH:\/\/\u5982\u679c\u662f\u5de6\u5b50\u6811\u9ad8\u8fdb\u884c\u4e00\u6b21\u5355\u6b21\u53f3\u65cb\n\t\t\/\/\u8282\u70b9\u63d2\u5728\u4e86\u5de6\u5b50\u6811\u7684\u5de6\u5b50\u6811\u4e0a\n\t\tT->bf=lc->bf=EH;\/\/\u66f4\u6539\u5e73\u8861\u56e0\u5b50\n\t\tR_Rotate(T);\/\/\u5355\u6b21\u53f3\u65cb\n\t\tbreak;\n\tcase RH:\/\/\u5982\u679c\u662f\u53f3\u5b50\u6811\u9ad8\n\t\t\/\/\u8bf4\u660e\u8282\u70b9\u63d2\u5165\u5728\u4e86\u5de6\u5b50\u6811\u7684\u53f3\u5b50\u6811\u4e0a\u8981\u8fdb\u884c\u5148\u5411\u5de6\u65cb\u8f6c\u540e\u5411\u53f3\u65cb\u8f6c\u4e24\u6b21\u65cb\u8f6c\n\t\trd=lc->rchild;\n\t\tswitch(rd->bf)\/\/\u66f4\u6539\u76f8\u5e94\u8282\u70b9\u7684\u5e73\u8861\u56e0\u5b50\n\t\t{\n\t\tcase LH:\n\t\t\tT->bf=RH;\n\t\t\tlc->bf=EH;\n\t\t\tbreak;\n\t\tcase EH:\n\t\t\tT->bf=lc->bf=EH;\n\t\t\tbreak;\n\t\tcase RH:\n\t\t\tT->bf=EH;\n\t\t\tlc->bf=LH;\n\t\t\tbreak;\n\t\t}\n\t\trd->bf=EH;\n\t\tL_Rotate(T->lchild);\n\t\tR_Rotate(T);\n\t\tbreak;\n\t}\n}\n \nStatus RightBalance(BSTree &amp;T)\/\/\u53f3\u5e73\u8861\u5904\u7406\n{\n\tBSTree rc=T->rchild,ld;\n\tswitch(rc->bf)\n\t{\n\tcase LH:\n\t\tld=rc->lchild;\n\t\tswitch(ld->bf)\n\t\t{\n\t\tcase LH:\n\t\t\tT->bf=EH;\n\t\t\trc->bf=RH;\n\t\t\tbreak;\n\t\tcase EH:\n\t\t\tT->bf=rc->bf=EH;\n\t\t\tbreak;\n\t\tcase RH:\n\t\t\tT->bf=LH;\n\t\t\trc->bf=EH;\n\t\t\tbreak;\n\t\t}\n\t\tld->bf=EH;\n\t\tR_Rotate(T->rchild);\n\t\tL_Rotate(T);\n\t\tbreak;\n\tcase RH:\n\t\tT->bf=rc->bf=EH;\n\t\tL_Rotate(T);\n\t\tbreak;\n\t}\n\treturn OK;\n}\n \nStatus InsertAVL(BSTree &amp;T,ElemType e,bool &amp;taller)\/\/\u8282\u70b9\u63d2\u5165\n{\n\tif(T==NULL)\n\/\/\u82e5\u4e3a\u7a7a\u6811\uff0c\u63d2\u5165\u4e00\u4e2a\u6570\u636e\u5143\u7d20\u4e3ae\u7684\u65b0\u8282\u70b9\u4f5c\u4e3aBBST\u7684\u6839\u8282\u70b9\u6570\u7684\u6df1\u5ea6\u589e1\n\t{\n\t\tT=new BSTNode;\n\t\tT->data=e;\n\t\ttaller=true;\n\t}\n\telse\n\/\/\u4e0d\u4e3a\u7a7a\u6811\n\t{\n\t\tif(T->data==e)\/\/\u5f85\u63d2\u5173\u952e\u5b57\u548cBBST\u6839\u8282\u70b9\u5173\u952e\u5b57\u76f8\u540c\u4e0d\u8fdb\u884c\u63d2\u5165\n\t\t{\n\t\t\ttaller=false;\n\t\t\treturn FAILED;\n\t\t}\n\t\telse if(e&lt;T->data)\/\/\u5c0f\u4e8e\u4e14\u5de6\u5b50\u6811\u4e2d\u4e0d\u5b58\u5728\u5173\u952e\u5b57\u548c\u5f85\u63d2\u5173\u952e\u5b57\u76f8\u540c\u7684\u7684\u8282\u70b9\u5c31\u63d2\u5165\n\t\t{\n\t\t\tif(InsertAVL(T->lchild,e,taller)==FAILED) return FAILED;\/\/\u5982\u63d2\u5165\u4e0d\u6210\u529f\u8fd4\u56deFAILED\n\t\t\tif(taller)\/\/\u5982\u679c\u63d2\u5165\u6210\u529f\u5e76\u4e14\u5de6\u5b50\u6811\u957f\u9ad8\u4e86\n\t\t\t{\n\t\t\t\tswitch(T->bf)\/\/BBST\u6839\u8282\u70b9\u7684\u73b0\u72b6\n\t\t\t\t{\n\t\t\t\tcase LH:\/\/\u5de6\u5b50\u6811\u9ad8\n\t\t\t\t\tLeftBalance(T);\/\/\u5de6\u5e73\u8861\u5904\u7406\n\t\t\t\t\ttaller=false;\/\/\u6807\u8bb0\u4e3a\u672a\u957f\u9ad8\n\t\t\t\t\tbreak;\n\t\t\t\tcase EH:\/\/\u5e73\u8861\u7684\n\t\t\t\t\tT->bf=LH;\/\/\u6807\u8bb0\u4e3a\u5de6\u9ad8\n\t\t\t\t\ttaller=true;\/\/\u6807\u8bb0\u4e3a\u957f\u9ad8\n\t\t\t\t\tbreak;\n\t\t\t\tcase RH:\/\/\u53f3\u5b50\u6811\u9ad8\n\t\t\t\t\tT->bf=EH;\/\/\u6807\u8bb0\u4e3a\u5e73\u8861\n\t\t\t\t\ttaller=false;\/\/\u6807\u8bb0\u4e3a\u672a\u957f\u9ad8\n\t\t\t\t\tbreak;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\telse\/\/\u5927\u4e8e\u4e14\u53f3\u5b50\u6811\u4e2d\u4e0d\u5b58\u5728\u5173\u952e\u5b57\u548c\u5f85\u63d2\u5173\u952e\u5b57\u76f8\u540c\u7684\u7684\u8282\u70b9\u5c31\u63d2\u5165\n\t\t{\n\t\t\tif(InsertAVL(T->rchild,e,taller)==FAILED) return FAILED;\/\/\u5982\u679c\u518d\u53f3\u5b50\u6811\u4e2d\u63d2\u5165\u4e0d\u6210\u529f\u8fd4\u56de\u4e0d\u6210\u529f\u4fe1\u606f\n\t\t\tif(taller)\/\/\u63d2\u5165\u6210\u529f\u5c31\u5224\u65ad\u53f3\u5b50\u6811\u662f\u5426\u957f\u9ad8\n\t\t\t{\n\t\t\t\tswitch(T->bf)\/\/BBST\u6839\u7684\u73b0\u72b6\n\t\t\t\t{\n\t\t\t\tcase LH:\/\/\u5de6\u5b50\u6811\u9ad8\n\t\t\t\t\tT->bf=EH;\n\t\t\t\t\ttaller=false;\n\t\t\t\t\tbreak;\n\t\t\t\tcase EH:\/\/\u5e73\u8861\n\t\t\t\t\tT->bf=RH;\n\t\t\t\t\ttaller=true;\n\t\t\t\t\tbreak;\n\t\t\t\tcase RH:\/\/\u53f3\u5b50\u6811\u9ad8\n\t\t\t\t\tRightBalance(T);\n\t\t\t\t\ttaller=false;\n\t\t\t\t\tbreak;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n\treturn OK;\n}\n \nvoid TraverseBSTree(BSTree &amp;T)\/\/\u904d\u5386BBST\n{\n\tif(T!=NULL)\n\t{\n\t\tcout&lt;&lt;T->data&lt;&lt;endl;\n\t\tTraverseBSTree(T->lchild);\n\t\tTraverseBSTree(T->rchild);\n\t}\t\n}<\/code><\/pre>\n\n\n\n<p>&#8220;main.cpp&#8221;<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#include&lt;iostream>\nusing namespace std;\n \n#include\"BalancedBinaryTree.h\"\n \nint main()\n{\n\tBSTree T=NULL;\n\tint choice;\n\tElemType key;\n\tbool taller=false;\n\twhile(1)\n\t{\n\t\tsystem(\"cls\");\n\t\tcout&lt;&lt;\"1 . InsertAVL\"&lt;&lt;endl\n\t\t\t&lt;&lt;\"2 . TraverseBSTree\"&lt;&lt;endl;\n\t\tcin>>choice;\n\t\tsystem(\"cls\");\n\t\tswitch(choice)\n\t\t{\n\t\tcase 1:\n\t\t\tcin>>key;\n\t\t\tInsertAVL(T,key,taller);\n\t\t\tbreak;\n\t\tcase 2:\n\t\t\tTraverseBSTree(T);\n\t\t\tsystem(\"pause\");\n\t\t\tbreak;\n\t\t}\n\t}\n}<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>&#8220;BalancedBinaryTree.h&#8221; &#8220;main.cpp&#8221;<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[9],"tags":[],"class_list":["post-242","post","type-post","status-publish","format-standard","hentry","category-06-02-"],"_links":{"self":[{"href":"http:\/\/www.wangkaixuan.tech\/index.php?rest_route=\/wp\/v2\/posts\/242","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/www.wangkaixuan.tech\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.wangkaixuan.tech\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.wangkaixuan.tech\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"http:\/\/www.wangkaixuan.tech\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=242"}],"version-history":[{"count":0,"href":"http:\/\/www.wangkaixuan.tech\/index.php?rest_route=\/wp\/v2\/posts\/242\/revisions"}],"wp:attachment":[{"href":"http:\/\/www.wangkaixuan.tech\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=242"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.wangkaixuan.tech\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=242"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.wangkaixuan.tech\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=242"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}