The binarysearchtree of JS data structure

Source: Internet
Author: User

Original link: http://www.gbtags.com/gb/share/5592.htm

Before Zhou--hua shared a sort method in detail, which introduced a variety of sort methods, I think it is very meaningful ~ a good development or need to know some important algorithms. So I studied two tree binarytree.

The advantage of Binarysearchtree is that it is easy to handle data, including inserting , deleting , sorting , and finding , which are excellent methods.

Let's compare the complexity of inserting , deleting , sorting , finding , and other algorithms:

1. insert , for each data insertion, his expectation complexity is O (log n), the worst-case complexity is O (n). In other words, he can achieve the complexity of O (log n) in general. Of course, this complexity is high for just inserting, because the complexity of just using Array.push is O (1). But the advantage of high insertion complexity here is that it reduces the complexity of other functions

2. find , if you want to find out whether a certain data exists, his expected complexity is O (log n), the worst-case complexity is O (n). For other algorithms, the optimal solution should be O (n) for unordered arrays, and the complexity of binary search for ordered arrays is O (log n). JS built-in method IndexOf, I did not find the complexity of this method, but I guess it should also be O (n).

3. sort , the first sort, in fact complex is generally considered to add and traverse together, then the complexity of the sorting is O (n (log n)) + O (n) = O (n (log n)). When add finishes, the complexity of each sort is O (n), which in fact is convenient for the tree. Comparing the other sorting algorithms, like the ones listed in the sort article previously shared by Zhou--hua, QuickSort, mergesort the complexity of these common algorithms is O (n (log n)), and the complexity of the previous two algorithms is higher relative to the tree.

4. Delete , his expected complexity is O (log n), and the worst-degree complexity is O (n). Compared to our usual method of JS deletion, with Arr.splice (item), 1), to compare, if according to my guess indexOf complexity is O (n), then the complexity of this thing is at least O (n). So, compared to the tree's deletion, the tree's method is indeed a little faster.

Advantages of BinarysearchtreeAs mentioned above, when the data is unordered, insert, delete, sort, find the complexity of the function is lower. It is also convenient to use. Disadvantages of Binarysearchtree

The disadvantage of Binarysearchtree is that:

1. All the data you have to add to the tree at the beginning of one, and the complexity of the process is O (n (log n)), because you need to traverse the entire data O (n), and then one by one add in O (log n).

2. Storing the tree at the same time definitely increases the amount of space that the original data occupies, because you also need to define left, right, root these things

3. There is no data duplication for Binarysearchtree.

4. The data is best out of order, if the data are ordered, then the method above is the worst complexity

However, his flaws, if you write code, you need to deal with a lot of data, but also need to insert, find, sort, delete these features, it is recommended to use Binarysearchtree. Or a further advanced tree, such as Avltree and Rbtree.

GitHub Address: https://github.com/nzakas/computer-science-in-javascript/blob/master/data-structures/ Binary-search-tree/binary-search-tree.js

Below the code comparison, in order to make time more obvious, where add, search, delete is processing 10,000 data. Sort is to export a 10,000 sorted array. According to Zhou-hua's reminder, add and sort should be compared together: (due to the large amount of data, the browser will be stuck for a period of time as soon as it is opened)

Original link: http://www.gbtags.com/gb/share/5592.htm

    1. Code Demo: http://www.gbtags.com/gb/rtreplayerpreview/1026.htm

The binarysearchtree of JS data structure

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.