Definition and use of the JavaScript Binary Search Tree

Source: Internet
Author: User
This article mainly introduces the definition and representation of the Binary Search Tree of the JavaScript data structure, this section briefly describes the concept and features of the Binary Search Tree and the implementation skills related to the creation, insertion, and traversal of the Binary Search Tree by javascript, if you need it, refer to the next article to introduce the definition and representation of the Binary Search Tree in the JavaScript data structure, describes the concept and features of the Binary Search Tree and the implementation skills related to the creation, insertion, and traversal of the Binary Search Tree in javascript. For more information, see

This article describes the definition and representation of the Binary Search Tree of the JavaScript data structure. We will share this with you for your reference. The details are as follows:

A tree is a non-linear data structure that stores data in layers. The tree is used to store hierarchical data, such as files in the file system. The tree is also used to store ordered lists. Here we will study a special tree: Binary Tree. Select the tree instead of the basic data structure because the search on the binary tree is very fast (but not on the linked list ), adding or deleting elements to or from a binary tree is also very fast (this is not the case for adding or deleting an array ).

A tree is a finite set of n nodes. The top is the root, and the bottom is the root subtree. The node of the tree contains a Data Element and several branches pointing to its subtree. The subtree owned by a node is called the degree of the node. A node with 0 degrees is called a leaf or terminal node. A node with a degree of less than 0 is called a non-terminal node or a branch node. The tree degree is the maximum degree of each node in the tree. The hierarchy of a node is defined from the root, and the root layer is 0th. The maximum hierarchy of nodes in the tree is called the depth or height of the tree.

A binary tree is a special tree with no more than two subnodes. Binary Trees have some special computing properties, making some operations on them extremely efficient. By limiting the number of subnodes to 2, you can write efficient programs to insert, query, and delete data in the tree.

Before using JavaScript to build a binary tree, we need to add two new terms to the tree dictionary. The two subnodes of a parent node are called the left node and the right node respectively. In the implementation of some binary trees, the left node contains a group of specific values, and the right node contains another group of specific values.Binary Search TreeIt is a special binary tree. Relatively small values are stored in the left node, and large values are stored in the right node. This feature makes searching highly efficient. This is true for both numeric and non-numeric data, such as words and strings.

The binary search tree consists of nodes. Therefore, we need to define a Node object. The Code is as follows:


Function Node (data, left, right) {// Node class this. data = data; this. left = left; this. right = right; this. show = show;} function show () {// return this. data ;}

Left and right are respectively used to point to left and right subnodes.

Next, you need to create a binary search tree class. The Code is as follows:


Function BST () {// tree class this. root = null; this. insert = insert; this. inOrder = inOrder; this. preOrder = preOrder; this. postOrder = postOrder ;}

Next, insert the code of the node. Traverse the left side of a small plug, and the right side of a large plug. The Code is as follows:


Function insert (data) {// insert var n = new Node (data, null, null); if (this. root = null) {// The first element this. root = n;} else {var current = this. root; // always point to the root node var parent; while (true) {// always run until the Left or Right node is found, parent = current; if (data
 
  

The above is the definition of the JavaScript Binary Search Tree and the details of using instances. For more information, see other related articles in the first PHP community!

Related Article

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.