Home Web Front-end JS Tutorial Sample code of javascript algorithm binary search tree

Sample code of javascript algorithm binary search tree

Jan 23, 2018 am 11:12 AM
javascript js Example

This article mainly introduces the example code of the binary search tree of the javascript algorithm. It has certain reference and value for learning JavaScript. Friends who are interested in JavaScript can refer to this article.

What is a binary tree

A binary tree means that each node of the tree can only have at most two child nodes

What is a binary search tree

Based on the binary tree, the binary search tree has one more condition, that is, when inserting a value in the binary tree, if the inserted value is smaller than the current node, it is inserted into the left node, otherwise it is inserted into the right node. ; If during the insertion process, the left node or the right node already exists, then continue to compare according to the above rules until a new node is encountered.

Characteristics of binary search trees

Due to its unique data structure, the binary search tree has a time complexity of O whether it is adding, deleting or searching. (h), h is the height of the binary tree. Therefore, the binary tree should be as short as possible, that is, the left and right nodes should be as balanced as possible.

Construction of binary search tree

To construct a binary search tree, you must first construct the node class of the binary tree. It can be seen from the characteristics of binary trees that each node class has a left node, a right node and the value itself, so the node class is as follows:

class Node {
 constructor(key) {
  this.key = key;
  this.left = null;
  this.right = null;
 }
}
Copy after login

Then construct a binary search tree

class Tree{
 constructor(param = null) {
  if (param) {
   this.root = new Node(param);
  } else {
   this.root = null;
  }
 }
}
Copy after login

here this. The root is the tree of the current object.

New addition of binary search tree

The left subtree of the binary search tree is smaller than the node, and the right subtree is larger than the node. Features, you can easily write the algorithm for adding a binary search tree, as follows:

insert(key) {
 if (this.root === null) {
  this.root = new Node(key);
 } else {
  this._insertNode(this.root, key);
 }
}
_insertNode(node, key) {
 if (key < node.key) {
  if (node.left === null) {
   node.left = new Node(key);{1}
  } else {
   this._insertNode(node.left, key);{2}
  }
 } else if (key > node.key) {
  if (node.right === null) {
   node.right = new Node(key);{3}
  } else {
   this._insertNode(node.right, key);{4}
  }
 }
}
Copy after login

The above code first determines the size of the new key and the key of the current node. If it is small, it recursively traverses the left child node. , until a left child node that is null is found; the same applies if it is larger than the current node. The reason why the above code {1}{2}{3}{4} can change the value of this.root is because the JavaScript function is passed by value, and when the parameter is a non-basic type, such as the object here, the value of the object is memory, so the content of this.root will be directly changed every time.

Traversal of binary search trees

Binary search trees are divided into three traversal methods: pre-order, mid-order, and post-order.

inOrderTraverse(callback) {
 this._inOrderTraverse(this.root, callback);
}
_inOrderTraverse(node, callback) {
 if (node) {
  this._inOrderTraverse(node.left, callback);
  callback(node.key);
  this._inOrderTraverse(node.right, callback);
 }
}
Copy after login

The above is an in-order traversal.

The thing to understand here is recursion. You know, the execution of a function can be abstracted into a data structure - a stack. For the execution of the function, a stack is maintained to store the execution of the function. Each time the function recurses, it will push the current execution environment onto the stack and record the execution location. Taking the above code as an example, there is the following data

It will start from 11, execute {1} to push into the stack, then enter 7, then execute {1} to push into the stack, and then go to 5, execute {1} to insert stack, and then to 3, execute {1} to push into the stack. At this time, it is found that the left child node of node 3 is null, so it starts to pop up. At this time, the execution environment of node 3 pops up, execute {2}, {3}, and find 3 The right child node of is also null, the recursive execution of {3} is completed, then pop up node 5, execute {2}{3}, then pop up 7, execute {2}{3} and push it onto the stack, when {3} is executed, It is found that node 7 has a right node, so we continue to execute {1}, go to node 8, and then execute {1}. 8 has no left child node. After {1} is executed, {2}{3} is executed, and so on.

The difference between preorder and midorder is that it first accesses the node itself, that is, the execution order of the code is 2 1 3.

The same is true for post-order, the execution order is 1 3 2

It is not difficult to find that no matter the front, middle or post-order, the left node is always recursed first, and when the left node When the traversal is completed, pop the stack and traverse the nodes. The only difference between them is the timing of accessing the node itself.

Binary search tree search

The search is very simple. Based on the principle that the left child node is smaller than the node and the right child node is larger than the node, the loop judgment is made. Can.

search(value) {
 if (this.root) {
  if (value === this.root.key) {
   return true;
  } else {
   return this._searchNode(value, this.root);
  }
 }
 throw new Error(&#39;this.root 不存在&#39;);
}
_searchNode(value, node) {
 if (!node) {
  return false;
 }
 if (value === node.key) {
  return true;
 }
 if (value > node.key) {
  return this._searchNode(value, node.right);
 } else if (value < node.key) {
  return this._searchNode(value, node.left);
 }
}
Copy after login

Deletion of binary search tree

Deletion is more complicated and needs to be judged according to different situations

First determine whether the node has a left Subtree, if there is no left subtree, directly replace the deleted node with the root node of the right subtree;

If there is, replace the deleted node with the smallest node of the right subtree;

remove(key) {
 this._removeNode(this.root, key);
}
_removeNode(node, value) {
 if (!node) {
  return null;
 }
 if (value > node.key) {
  node.right = this._removeNode(node.right, value);
 } else if (value < node.key) {
  node.left = this._removeNode(node.left, value);
 } else {
  // 如果没有左子树,那么将右子树根节点作为替换节点
  if (!node.left) {
   return node.right;
   // 如果存在左子树,那么取右子树最小节点作为替换节点
  } else if (node.left) {
   return this._minNode(node.right);
  }
 }
}
Copy after login

Summary

In general, through this simple study of binary search trees, I learned about recursion again. My previous understanding of recursion was only a little bit A simple theoretical concept, this in-depth practice has deepened my understanding of recursion a lot.

This reminds me of the study of mathematics. The theoretical formulas of mathematics are easy to remember and master. If the full score for mastering a knowledge point is ten points, then until you actually practice it, Merely looking at the mastery of the formula can only give you 2 points, because the formula is very simple, just a few sentences and a few principles, but the problems encountered are ever-changing. Only by truly putting the theory into practice and polishing it through various practices can we Really understand the mystery of it.

Related recommendations:

Sharing the differences between three JavaScript simulation implementation encapsulation methods and writing methods

Detailed explanation of JavaScript self-executing functions and jQuery extension methods

Explanation of Require calling js examples in JavaScript

The above is the detailed content of Sample code of javascript algorithm binary search tree. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Recommended: Excellent JS open source face detection and recognition project Recommended: Excellent JS open source face detection and recognition project Apr 03, 2024 am 11:55 AM

Face detection and recognition technology is already a relatively mature and widely used technology. Currently, the most widely used Internet application language is JS. Implementing face detection and recognition on the Web front-end has advantages and disadvantages compared to back-end face recognition. Advantages include reducing network interaction and real-time recognition, which greatly shortens user waiting time and improves user experience; disadvantages include: being limited by model size, the accuracy is also limited. How to use js to implement face detection on the web? In order to implement face recognition on the Web, you need to be familiar with related programming languages ​​and technologies, such as JavaScript, HTML, CSS, WebRTC, etc. At the same time, you also need to master relevant computer vision and artificial intelligence technologies. It is worth noting that due to the design of the Web side

Oracle DECODE function detailed explanation and usage examples Oracle DECODE function detailed explanation and usage examples Mar 08, 2024 pm 03:51 PM

The DECODE function in Oracle is a conditional expression that is often used to return different results based on different conditions in query statements. This article will introduce the syntax, usage and sample code of the DECODE function in detail. 1. DECODE function syntax DECODE(expr,search1,result1[,search2,result2,...,default]) expr: the expression or field to be compared. search1,

Go language indentation specifications and examples Go language indentation specifications and examples Mar 22, 2024 pm 09:33 PM

Indentation specifications and examples of Go language Go language is a programming language developed by Google. It is known for its concise and clear syntax, in which indentation specifications play a crucial role in the readability and beauty of the code. effect. This article will introduce the indentation specifications of the Go language and explain in detail through specific code examples. Indentation specifications In the Go language, tabs are used for indentation instead of spaces. Each level of indentation is one tab, usually set to a width of 4 spaces. Such specifications unify the coding style and enable teams to work together to compile

PHP and JS Development Tips: Master the Method of Drawing Stock Candle Charts PHP and JS Development Tips: Master the Method of Drawing Stock Candle Charts Dec 18, 2023 pm 03:39 PM

With the rapid development of Internet finance, stock investment has become the choice of more and more people. In stock trading, candle charts are a commonly used technical analysis method. It can show the changing trend of stock prices and help investors make more accurate decisions. This article will introduce the development skills of PHP and JS, lead readers to understand how to draw stock candle charts, and provide specific code examples. 1. Understanding Stock Candle Charts Before introducing how to draw stock candle charts, we first need to understand what a candle chart is. Candlestick charts were developed by the Japanese

Simple JavaScript Tutorial: How to Get HTTP Status Code Simple JavaScript Tutorial: How to Get HTTP Status Code Jan 05, 2024 pm 06:08 PM

JavaScript tutorial: How to get HTTP status code, specific code examples are required. Preface: In web development, data interaction with the server is often involved. When communicating with the server, we often need to obtain the returned HTTP status code to determine whether the operation is successful, and perform corresponding processing based on different status codes. This article will teach you how to use JavaScript to obtain HTTP status codes and provide some practical code examples. Using XMLHttpRequest

The relationship between js and vue The relationship between js and vue Mar 11, 2024 pm 05:21 PM

The relationship between js and vue: 1. JS as the cornerstone of Web development; 2. The rise of Vue.js as a front-end framework; 3. The complementary relationship between JS and Vue; 4. The practical application of JS and Vue.

Application and example analysis of PHP dot operator Application and example analysis of PHP dot operator Mar 28, 2024 pm 12:06 PM

Application and example analysis of PHP dot operator In PHP, the dot operator (".") is an operator used to connect two strings. It is very commonly used and very flexible when concatenating strings. By using the dot operator, we can easily concatenate multiple strings to form a new string. The following will introduce the use of PHP dot operators through example analysis. 1. Basic usage First, let’s look at a basic usage example. Suppose there are two variables $str1 and $str2, which store two words respectively.

How to get HTTP status code in JavaScript the easy way How to get HTTP status code in JavaScript the easy way Jan 05, 2024 pm 01:37 PM

Introduction to the method of obtaining HTTP status code in JavaScript: In front-end development, we often need to deal with the interaction with the back-end interface, and HTTP status code is a very important part of it. Understanding and obtaining HTTP status codes helps us better handle the data returned by the interface. This article will introduce how to use JavaScript to obtain HTTP status codes and provide specific code examples. 1. What is HTTP status code? HTTP status code means that when the browser initiates a request to the server, the service

See all articles