当前位置: 首页 > 编程笔记 >

Javascript中的AVL树类

端木承业
2023-03-14
本文向大家介绍Javascript中的AVL树类,包括了Javascript中的AVL树类的使用技巧和注意事项,需要的朋友参考一下

这是AVL树类的完整实现-

示例

class AVLTree {
   constructor() {
      //将根元素初始化为null。
      this.root = null;
   }

   getBalanceFactor(root) {
      return this.getHeight(root.left) - this.getHeight(root.right);
   }

   getHeight(root) {
      let height = 0;
      if (root === null || typeof root == "undefined") {
         height = -1;
      } else {
         height = Math.max(this.getHeight(root.left), this.getHeight(root.right)) + 1;
      }
      return height;
   }

   insert(data) {
      let node = new this.Node(data);
      //检查树是否为空
      if (this.root === null) {
         //作为第一个元素插入this.root = node; 
      } else {
         insertHelper(this, this.root, node);
      }
   }
   inOrder() {
      inOrderHelper(this.root);
   }
}

AVLTree.prototype.Node = class {
   constructor(data, left = null, right = null) {
      this.data = data;
      this.left = left;
      this.right = right;
   }
};

function insertHelper(self, root, node) {
   (root === null) {
      root = node;
   } else if (node.data < root.data) {
      //向左走!
      root.left = insertHelper(self, root.left, node);
      //检查平衡系数并执行适当的旋转
      if (root.left !== null && self.getBalanceFactor(root) > 1) {
      if (node.data > root.left.data) {
         root = rotationLL(root);
      } else {
         root = rotationLR(root);
      }
   }
} else if (node.data > root.data) {
   //向右走!根。
   right = insertHelper(self, root.right, node);
   //检查平衡系数并执行适当的旋转
   if (root.right !== null && self.getBalanceFactor(root) < -1) {
      if (node.data > root.right.data) {
         root = rotationRR(root);
      } else {
         root = rotationRL(root);
      }
   }
}
return root;
}

function inOrderHelper(root) {
   if (root !== null) {
      inOrderHelper(root.left);
      console.log(root.data);
      inOrderHelper(root.right);
   }
}

function rotationLL(node) {
   let tmp = node.left;
   node.left = tmp.right;
   tmp.right = node;
   return tmp;
}

function rotationRR(node) {
   let tmp = node.right;
   node.right = tmp.left;
   tmp.left = node;
   return tmp;
}

function rotationLR(node) {
   node.left = rotationRR(node.left);
   return rotationLL(node);
}

function rotationRL(node) {
   node.right = rotationLL(node.right);
   return rotationRR(node);
}
 类似资料:
  • 本文向大家介绍Javascript中的AVL树,包括了Javascript中的AVL树的使用技巧和注意事项,需要的朋友参考一下 AVL树(以发明家Adelson-Velsky和Landis的名字命名)是一种自平衡二进制搜索树。自平衡树是一棵在其子树中执行一些旋转的树,以便可以在左右两侧进行平衡。 这些树木在插入物使树木一侧偏重的情况下特别有用。平衡树使查找时间接近O(log(n)),而完全不平衡的

  • 本文向大家介绍在Javascript AVL树中插入节点,包括了在Javascript AVL树中插入节点的使用技巧和注意事项,需要的朋友参考一下 我们可以学习如何在AVL树中插入节点。AVL树中的插入与BST相同,只要我们在树上向下移动,我们只需在插入过程中执行一个额外的步骤,称为平衡树。 这需要计算我们之前已经看到的平衡因子。并且根据配置,我们需要调用适当的旋转方法。在以上说明的帮助下,这些都

  • AVL树与自平衡二叉搜索树相同。AVL代表什么?这和发明者的名字有关吗?

  • 本文向大家介绍在Javascript AVL树中计算平衡因子,包括了在Javascript AVL树中计算平衡因子的使用技巧和注意事项,需要的朋友参考一下 AVL树检查左子树和右子树的高度,并确保差异不超过1。该差异称为“平衡因子”。 例如,在以下树中,第一棵树是平衡的,接下来的两棵树是不平衡的- 在第二棵树中,C的左子树的高度为2,而右边子树的高度为0,所以差为2。在第三棵树中,A的右子树的高度

  • 找到一个示例AVL树,从树中删除单个(特定)值会导致从两个不同的节点开始重新平衡。 这是我的家庭作业问题。我不知道上面的问题是什么。有人能解释吗? 在两个不同的节点上重新平衡是否意味着需要两次旋转来修复树?

  • 主要内容:二叉排序树转化为平衡二叉树,构建平衡二叉树的代码实现,总结上一节介绍如何使用二叉排序树实现动态 查找表,本节介绍另外一种实现方式—— 平衡二叉树。 平衡二叉树,又称为  AVL 树。实际上就是遵循以下两个特点的二叉树: 每棵子树中的左子树和右子树的深度差不能超过 1; 二叉树中每棵子树都要求是平衡二叉树; 其实就是在二叉树的基础上,若树中每棵子树都满足其左子树和右子树的深度差都不超过 1,则这棵二叉树就是平衡二叉树。 图 1 平衡与不平衡的二叉树及结点的