Balanced binary tree data structure _

// node class 
  class Node {
    construction (date) {
      this.data = data
      this.left = null
      this.right = null
    }
  }
  // balanced binary tree Binary Tree Balanced 
  class {BBT
    constructor() {
      this.root = null
    }

    insert(data) {
      // Create the node 
      the let the newNode = new new the Node (Data)                
       // insert node: this.root will change 
      the this .Root = the this .insertNode ( the this .Root, the newNode)
    }
    insertNode(node, newNode) {
      // currently empty tree 
      IF (Node == null ) {
        node = newNode
        return node
      } The else  IF (newNode.data < node.data) {
         // go to the left 
        IF (node.left == null ) {
          node.left = newNode
          return node
        } else {
          node.left = this.insertNode(node.left, newNode)
          // 判断平衡
          node = this.checkIsBalance(node)
        }
      } The else  IF (newNode.data> node.data) {
         // go to the right 
        IF (node.right == null ) {
          node.right = newNode
          return node
        } else {
          Node.Right = This .InsertNode (Node.Right, NewNode)
           // determine equilibrium 
          Node = This .CheckIsBalance (Node)
        }
      }
      return node
    }

    checkIsBalance(node) {
      // The height of the left and right subtrees 
      @ 1 left subtree - right subtree>. 1 
      IF (( the this .getHeight (node.left) - the this .getHeight (node.right))>. 1 ) {
         IF ( the this . getHeight (node.left.left)> the this .getHeight (node.left.right))
          Node = the this .rotateR (Node) // dextrorotatory 
        the else 
          Node = the this .rotateLR (Node) // first left and then right-handed 
      } // 2. right subtree - left subtree>. 1 
      the else  IF (( the this .getHeight ( node.right) - the this .getHeight (node.left))>. 1 ) {
         IF ( the this .getHeight (node.right.right)> the this .getHeight (node.right.left))
          Node = the this .rotateL (Node) // L 
        the else 
          Node = the this .rotateRL (Node) // first left and then right-handed 
      }
       return Node
    }
    // children maximum height + 1'd 
    getHeight (Node) {
       IF (Node == null ) return 0
       // returns the larger value in the current node in the subtree + 1'd 
      // Math.max () 
      return (Math.max ( the this .getHeight (node.left), the this .getHeight (node.right))) +. 1
    }
    // dextrose 
    rotateR (node) {
      let temp = node.left
      node.left = temp.right
      temp.right = node
      return temp
    }

    // L 
    rotateL (node) {
      let temp = node.right
      node.right = temp.left
      temp.left = node
      return temp
    }

    // spin around 
    rotateLR (node) {
      node.left = this.rotateL(node.left)
      return this.rotateR(node)
    }
    // right-left 
    rotateRL (node) {
      node.right = this.rotateR(node.right)
      return this.rotateL(node)
    }
  }

 

Guess you like

Origin www.cnblogs.com/JunLan/p/12359544.html