Reprint please indicate the source
2016.7.7 by Totooria Hyperion
http://demo.th-shr.com:9999/
The current implementation:
Pre-sequence traversal
Middle Sequence traversal
Post-post traversal
Hierarchical traversal
Find the number of leaf nodes
To find the height of a tree
Symmetric tree
Determine whether a node is in a tree
The nearest public parent node for the two node
Other algorithms are then slowly mended later
The first is to use the class you wrote:
Http://www.cnblogs.com/Totooria-Hyperion/p/5645096.html
The binary tree declaration is as follows:
Class ("Bintree",function(tl,tr,tdata) { This. TL = TL | |NULL; This. TR = TR | |NULL; This. Tdata = Tdata | |NULL;},{ "Createtree":function(preorder) {vart = {},tl = {},tr = {}; if(Preorder.str[0] = = "#") {Preorder.str= PREORDER.STR.SUBSTR (1); return NULL; } Else{T=NewBintree (); T.tdata= Preorder.str[0]; Preorder.str= PREORDER.STR.SUBSTR (1); TL=BinTree.prototype.createTree (preorder); T.setleft (TL); TR=BinTree.prototype.createTree (preorder); T.setright (TR); returnT; } }, "Setleft":function(PTR) { This. TL =ptr; }, "Setright":function(PTR) { This. TR =ptr; }, "GetLeft":function(PTR) {return This. tl; }, "GetRight":function(PTR) {return This. tr; }, "GetData":function() { return This. Tdata; }}); Bintree.extend ({"Getpreorder":function(obj) {(typeofObj.order = = "undefined")? Obj.order = "": obj; Obj.order+= This. Tdata; varTL = This. GetLeft (), tr = This. GetRight (); TL!=NULL?Tl.getpreorder (obj): tl; TR!=NULL?Tr.getpreorder (obj): TR; returnObj.order; }, "Getinorder":function(obj) {(typeofObj.order = = "undefined")? Obj.order = "": obj; varTL = This. GetLeft (), tr = This. GetRight (); TL!=NULL?Tl.getinorder (obj): tl; Obj.order+= This. Tdata; TR!=NULL?Tr.getinorder (obj): TR; returnObj.order; }, "Getpostorder":function(obj) {(typeofObj.order = = "undefined")? Obj.order = "": obj; varTL = This. GetLeft (), tr = This. GetRight (); TL!=NULL?Tl.getpostorder (obj): tl; TR!=NULL?Tr.getpostorder (obj): TR; Obj.order+= This. Tdata; returnObj.order; }, "Getlevelorder":function(obj) {(typeofObj.nodes = = "undefined")? Obj.nodes =[]: obj; varcur = [ This]; while(Cur.length! = 0) {Obj.nodes=obj.nodes.concat (cur); var_cur = []; for(vari=0;i<cur.length;i++) { varTL = Cur[i].getleft (), tr =cur[i].getright (); TL?_cur.push (TL): TL; TR?_cur.push (TR): TR; } cur=_cur; } varstr = ""; Obj.nodes.forEach (function(item) {str+=Item.tdata; }); returnstr; }, "Getleafsum":function(){ varTL = This. GetLeft (), tr = This. GetRight (); if(TL &&TR) { returnTl.getleafsum () +tr.getleafsum (); } Else if(!TL &&!)TR) { return1; } Else if(TL) {returntl.getleafsum (); } Else { returntr.getleafsum (); } }, "GetHeight":function() { varTL = This. GetLeft (), tr = This. GetRight (); if(!TL &&!)TR) { return1; } Else if(TL &&TR) { returnTl.getheight () >= tr.getheight ()? Tl.getheight () + 1:tr.getheight () + 1; } Else if(TL) {returnTl.getheight () + 1; } Else { returnTr.getheight () + 1; } }, "Swap":function() { varTL = This. GetLeft (), tr = This. GetRight (); This. Setleft (TR); This. Setright (TL); TL!=NULL?Tl.swap (): TL; TR!=NULL?Tr.swap (): TR; }, "Isintree":function(tree) {varBOOL =false; if( This==tree) { return true; } Else { varTL = Tree.getleft (), tr =tree.getright (); varTLB = TL! =NULL? This. Isintree (TL):false; varTRB = tr! =NULL? This. Isintree (TR):false; returnTLB | |TRB; } }, "Getnearestcommonfather":function(flag,node1,node2,tree) {if(flag) {if(Node1.isintree (Node2)) {returnNode2; } Else if(Node2.isintree (Node1)) {returnNode1; } Else { varTL = Tree.getleft (), tr =tree.getright (); varInleft = TL && node1.isintree (TL) &&Node2.isintree (TL); varInright = tr && node1.isintree (TR) &&Node2.isintree (TR); if(!inleft &&!)inright) { returnTree; } Else if(inleft) {return This. Getnearestcommonfather (1, NODE1,NODE2,TL); } Else { return This. Getnearestcommonfather (1, NODE1,NODE2,TR); } } } Else { if(Node1.isintree (tree) &&Node2.isintree (tree)) { return This. Getnearestcommonfather (1, Node1,node2,tree); } Else { return false; } } }});
The test code and test results are as follows:
varTree = BinTree.prototype.createTree ({"str": "6423### #51 # #7 # #"}); Console.log (Tree.getpreorder ({})); //6423517Console.log (Tree.getinorder ({}));//3246157Console.log (Tree.getpostorder ({}));//3241756Console.log (Tree.getlevelorder ({}));//6452173Console.log (Tree.getleafsum ());//3Console.log (Tree.getheight ());//4Tree.swap () Console.log (Exchange); Console.log (Tree.getpreorder ({})); //6571423Console.log (Tree.getpostorder ({}));//7153246Tree.swap () Console.log (Exchange); Console.log (Tree.tr.tl.isInTree (tree)) ;//trueConsole.log (BinTree.prototype.getNearestCommonFather (0,tree.tr.tl,tree.tr.tr,tree));
JavaScript-based two-fork tree for "original"