We don’t allow questions seeking recommendations for software libraries, tutorials, tools, books, or other off-site resources. You can edit the question so it can be answered with facts and citations.
Closed 4 years ago.
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
接受
或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
发布评论
评论(5)
如果重要的话,我发现将此类数据存储为文字树的效率低于将其存储为已排序数组并在数组上进行二分搜索以拼接/插入元素的效率。显然,JavaScript 对象的创建并不是免费的。
还有“在数组中编码树”技巧:
相同
与“children(N[i]) = N[2i+1], N[2i+2]” 。我不知道这是否真的能让你在 JavaScript 中获胜。
如果您尝试一些二叉树的替代方案,您可以在这里发布您的发现吗? :)
If it matters, I found that storing this kind of data as a literal tree was less efficient than storing it as an already-sorted array and doing binary search on the array to splice/insert elements. JavaScript object creation is not free, apparently.
There's also the ol' encode-a-tree-in-an-array trick:
is the same as
i.e. children(N[i]) = N[2i+1], N[2i+2] . I don't know if that really gives you any win in JavaScript.
If you try out some alternatives to the binary tree, could you post your findings here? :)
https://gist.github.com/alexhawkins/f993569424789f3be5db
https://gist.github.com/alexhawkins/f993569424789f3be5db
这有帮助吗? - JavaScript 中的计算机科学:二叉搜索树,第 1 部分
Does this help? - Computer science in JavaScript: Binary search tree, Part 1
你可以尝试 buckets,一个 JavaScript 库,它拥有你需要的一切。
You can try buckets, a javascript library, it has everything you need.
二叉搜索树通常称为 BST,是一种特殊类型的树,本质上是排序的。在二叉搜索树中,每个节点都大于其左子节点,小于其右子节点。此功能可以轻松地从二叉搜索树中搜索、插入和删除节点。
Algo
//检查根节点是否为空
// 如果是则将新节点分配给根
// 如果不是则迭代。 Iterate 方法将从当前处理节点的左子节点和右子节点检查要添加的节点值。
// 如果要添加的节点值小于该节点,则移动左子节点
// 如果左子节点为空,则将新节点分配给该左子节点
// 否则调用迭代方法
// 如果要添加的节点值大于节点值移动到右子节点
// 如果右子节点为空,则将新节点分配给该右子节点
// 否则调用迭代方法
如果这有帮助,您可以在此处查找完整文章 Javascript 中的二叉搜索树插入节点实现
Binary search trees commonly knows as BST are a special type of trees which are sorted in nature. In binary search tree every node is larger than its left child and smaller than its right child. This feature makes it easy to search, insert and delete a node from binary search tree.
Algo
//Check if root node is empty or not
// If yes then assign new node to root
// If not than iterate. Iterate method will check the node value to add from left and right child of the currently processing node.
// if node value to add is lesser than the node than move tho left child
// If left child is empty than assign new node to this left child node
// else call iterate method
// IF node value to add is greater than the node value than move to the right child
// If right child is empty than assign the new node to this right child node
// else call iterate method
If this helps you can look for full article here Binary Search Tree Insert node Implementation in Javascript