function Node (data,left,right) {this.left=left this.right=right this.data=data} function Btr () {this.root = null; }//D: Root node L: Left Dial hand node R: Right child node btr.prototype.insert=function (data) {//Generate sorted two fork tree if (this.root==null) { This.root = new Node (data,null,null)}else {var current = This.root; while (true) {if (data<current.data) {if (current.left! = null) { Current = Current.left}else {current.left = new Node (data,null,null) Break}}else {if (current.right! = null) { Current = Current.right}else {current.right = new Node (data,null,nu ll) Break}}}} Btr.prototype.CenterS Can=functIon () {//middle order (LDR) var list=[],root = This.root,left,right while (root) {if (root.left) { left = Root.left List.unshift (root) root.left=null root = Left }else {Console.log (root.data) if (root.right) {right = Root.right Root.right=null root = right}else {root = List.s Hift ()}}}} btr.prototype.frontscan=function () {//Pre-order (DLR) var list=[], root = This.root,left,right while (root) {if (root.data) Console.log (root.data) left = ROOT.L EFT right = Root.right if (left) {root.left=null root.data=null List.unshift (root) root = left}else if (right) {root = right} else {ROot = List.shift ()}} btr.prototype.backscan=function () {//post-post (LRD) var list=[],root = This.root,left,right while (root) {left = Root.left right = Root.right if (left) { Root.left=null List.unshift (root) root = left}else { if (right) {root.right = null list.unshift (root) root = right }else {Console.log (root.data) root = List.shift ()} }}} btr.prototype.max=function () {var root = This.root,right while (root) { right = Root.right if (right) {root = right}else {Console.log (r oot.data) root = null}}} btr.prototype.min=function () {var root = this . Root,left while(Root) {left = Root.left if (left) {root = left}else {Console.lo G (root.data) root = null}}} var BTR = new BTR (); Btr.insert (6) Btr.insert (2) Btr.insert (1) Btr.insert (4) Btr.insert (3) Btr.insert (5) Btr.insert (9) BTR . Insert (8) Btr.insert (Ten) BTR. Centerscan ()
JS two fork tree, pre/middle order/Post order (maximum minimum, sort)