JS two fork tree, pre/middle order/Post order (maximum minimum, sort)

Source: Internet
Author: User

    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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.