JS——二叉排序树

208 阅读1分钟
// 二叉树节点类
var Node = (function() {
  function Node(data, left, right, parent) {
    this.data = data;
    this.left = left;
    this.right = right;
    this.parent = parent;
  }
  Node.prototype = {
    constructor: Node,
    show: function() {
      return this.data
    }
  }
  return Node
})()

// 二叉排序树类
var OrderTree = (function() {
  function OrderTree() {
    this.root = null
  }
  OrderTree.prototype = {
    constructor: OrderTree,
    create: create, // 创建二叉排序树,params: Array[...Number]
    insert: insert, // 插入节点,params: data:(Number)
    remove: remove, // 删除节点,params: value:(Number) 返回被删除的节点
    disp: disp, // 展示这颗树
    sortMin: sortMin, // 从大到小排序返回有序序列
    sortMax: sortMax, // 从小到大排序返回有序序列
    search: search, // 查找,params: value:(Number)要查找的值,返回该节点
    getMin: getMin, // 获取最小值
    getMax: getMax // 获取最大值
  }

  function create(datalist) {
    var i = 0
    while(i < datalist.length) {
      this.insert(datalist[i++])
    }
    return this.root
  }

  function insert(data) {
    if(!this.root) {
      this.root = new Node(data, null, null, null)
      return
    }
    var _insert = function(node) {
      if(node.left && data < node.data) {
        return _insert(node.left)
      } else if(node.right && data >= node.data) {
        return _insert(node.right)
      } else if(!node.left && data < node.data) {
        node.left = new Node(data, null, null, node)
      } else {
        node.right = new Node(data, null, null, node)
      }
    }
    _insert(this.root)
  }
  
  function remove(value) {
    var re = this.search(value)
    if(re.constructor.name === '_Node') {
      var p = re.parent
      if(re === p.left) {
        delete p.left
        p.left = null
      } else {
        delete p.right
        p.right = null
      }
    }
  }
  
  function sortMin() {
    if(!this.root) {
      console.log('空树')
      return
    }
    var a = []
    var pre = function(n) {
      if(n) {
        pre(n.left)
        a.push(n.data)
        //         console.log(n.data)
        return pre(n.right)
      }
    }
    pre(this.root)
    return a
  }

  function sortMax() {
    return this.sortMin().reverse()
  }

  function search(value) {
    var n = '没有找到 ' + value
    var _search = function(node) {
      if(node) {
        if(value === node.data) {
          n = node
          return
        } else if(node.left && value < node.data) {
          return _search(node.left)
        } else if(value > node.data) {
          return _search(node.right)
        }
      }
    }
    _search(this.root)
//     console.log(n)
    return n
  }
  
  function getMin(){
    var current = this.root;
    while(!(current.left == null)){
      current = current.left;
    }
    return current.data;
  }

  function getMax(){
    var current = this.root;
    while(!(current.right == null)){
      current = current.right;
    }
    return current.data;
  }

  function disp() {
    if(!this.root) {
      console.log('空树')
    } else {
      console.log(this.root)
    }
  }
  return OrderTree
})()

var t = new OrderTree()
t.create([1,10,2,0,3,-100,200])
console.log(t.sortMin()); //[-100, 0, 1, 2, 3, 10, 200]
// console.log(t.search(1000))
t.remove(0)
t.insert(520)
// t.disp()
console.log(t.sortMin());