


Javascript data structure binary search tree implementation method_javascript skills
The example in this article describes the implementation method of binary search tree in javascript. Share it with everyone for your reference, the details are as follows:
Binary search tree: As the name suggests, each node in the tree has at most two forks; and the value of the left fork node is < the value of the right fork node is .
Features: Inserting nodes, finding the largest/minimum nodes, and sorting node values are very convenient
Binary search tree-javascript implementation
<script type="text/javascript"> // <![CDATA[ //打印输出 function println(msg) { document.write(msg + " "); } //节点类 var Node = function (v) { this.data = v; //节点值 this.left = null; //左节点 this.right = null; //右节点 } //二叉搜索树类 var BinarySearchTree = function () { this.root = null; //初始化时,根节点为空 //插入节点 //参数:v 为节点的值 this.insert = function (v) { var newNode = new Node(v); if (this.root == null) { //树为空时,新节点,直接成为根节点 this.root = newNode; return; } var currentNode = this.root; //工作“指针”节点(从根开始向下找) var parentNode = null; while (true) { parentNode = currentNode; if (v < currentNode.data) { //当前节点的值 > 目标节点的值 //应该向左插,工作节点移到左节点 currentNode = currentNode.left; if (currentNode == null) { //没有左节点,则新节点,直接成为左节点 parentNode.left = newNode; return; //退出循环 } } else { //否则向右插,工作节点移到右节点 currentNode = currentNode.right; if (currentNode == null) { parentNode.right = newNode; return; } } } } //查找最小节点 this.min = function () { var p = this.root; //工作节点 while (p != null && p.left != null) { p = p.left; } return p; } //查找最大节点 this.max = function () { var p = this.root; //工作节点 while (p != null && p.right != null) { p = p.right; } return p; } //中序遍历 this.inOrder = function (rootNode) { if (rootNode != null) { this.inOrder(rootNode.left); //先左节点 println(rootNode.data); //再根节点 this.inOrder(rootNode.right); //再右节点 } } //先序遍历 this.preOrder = function (rootNode) { if (rootNode != null) { println(rootNode.data); //先根 this.preOrder(rootNode.left); //再左节点 this.preOrder(rootNode.right); //再右节点 } } //后序遍历 this.postOrder = function (rootNode) { if (rootNode != null) { this.postOrder(rootNode.left); //先左节点 this.postOrder(rootNode.right); //再右节点 println(rootNode.data); //再根节点 } } } //以下是测试 var bTree = new BinarySearchTree(); //《沙特.算法设计技巧与分析》书上图3.9 左侧的树 bTree.insert(6); bTree.insert(3); bTree.insert(8); bTree.insert(1); bTree.insert(4); bTree.insert(9); println('中序遍历:') bTree.inOrder(bTree.root); println("<br/>"); println("先序遍历:"); bTree.preOrder(bTree.root); println("<br/>"); println("后序遍历:"); bTree.postOrder(bTree.root); println("<br/>"); var minNode = bTree.min(); println("最小节点:" + (minNode == null ? "不存在" : minNode.data)); println("<br/>"); var maxNode = bTree.max(); println("最大节点:" + (maxNode == null ? "不存在" : maxNode.data)); // ]]> </script> <!--中序遍历: 1 3 4 6 8 9 <br> 先序遍历: 6 3 1 4 8 9 <br> 后序遍历: 1 4 3 9 8 6 <br> 最小节点:1 <br> 最大节点:9-->
Output result:
中序遍历: 1 3 4 6 8 9 先序遍历: 6 3 1 4 8 9 后序遍历: 1 4 3 9 8 6 最小节点:1 最大节点:9
I hope this article will be helpful to everyone in JavaScript programming.

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



When using complex data structures in Java, Comparator is used to provide a flexible comparison mechanism. Specific steps include: defining the comparator class, rewriting the compare method to define the comparison logic. Create a comparator instance. Use the Collections.sort method, passing in the collection and comparator instances.

Data structures and algorithms are the basis of Java development. This article deeply explores the key data structures (such as arrays, linked lists, trees, etc.) and algorithms (such as sorting, search, graph algorithms, etc.) in Java. These structures are illustrated through practical examples, including using arrays to store scores, linked lists to manage shopping lists, stacks to implement recursion, queues to synchronize threads, and trees and hash tables for fast search and authentication. Understanding these concepts allows you to write efficient and maintainable Java code.

Reference types are a special data type in the Go language. Their values do not directly store the data itself, but the address of the stored data. In the Go language, reference types include slices, maps, channels, and pointers. A deep understanding of reference types is crucial to understanding the memory management and data transfer methods of the Go language. This article will combine specific code examples to introduce the characteristics and usage of reference types in Go language. 1. Slices Slices are one of the most commonly used reference types in the Go language.

AVL tree is a balanced binary search tree that ensures fast and efficient data operations. To achieve balance, it performs left- and right-turn operations, adjusting subtrees that violate balance. AVL trees utilize height balancing to ensure that the height of the tree is always small relative to the number of nodes, thereby achieving logarithmic time complexity (O(logn)) search operations and maintaining the efficiency of the data structure even on large data sets.

Overview of Java Collection Framework The Java collection framework is an important part of the Java programming language. It provides a series of container class libraries that can store and manage data. These container class libraries have different data structures to meet the data storage and processing needs in different scenarios. The advantage of the collection framework is that it provides a unified interface, allowing developers to operate different container class libraries in the same way, thereby reducing the difficulty of development. Data structures of the Java collection framework The Java collection framework contains a variety of data structures, each of which has its own unique characteristics and applicable scenarios. The following are several common Java collection framework data structures: 1. List: List is an ordered collection that allows elements to be repeated. Li

In-depth study of the mysteries of Go language data structure requires specific code examples. As a concise and efficient programming language, Go language also shows its unique charm in processing data structures. Data structure is a basic concept in computer science, which aims to organize and manage data so that it can be accessed and manipulated more efficiently. By in-depth learning the mysteries of Go language data structure, we can better understand how data is stored and operated, thereby improving programming efficiency and code quality. 1. Array Array is one of the simplest data structures

Overview of the PHPSPL Data Structure Library The PHPSPL (Standard PHP Library) data structure library contains a set of classes and interfaces for storing and manipulating various data structures. These data structures include arrays, linked lists, stacks, queues, and sets, each of which provides a specific set of methods and properties for manipulating data. Arrays In PHP, an array is an ordered collection that stores a sequence of elements. The SPL array class provides enhanced functions for native PHP arrays, including sorting, filtering, and mapping. Here is an example of using the SPL array class: useSplArrayObject;$array=newArrayObject(["foo","bar","baz"]);$array

B-tree is a balanced search tree used for fast storage and retrieval of data. The performance of B-tree indexes can be optimized using joint indexes, prefix indexes and the correct balancing strategy. Specifically, choosing the appropriate order, using union indexes, using prefix indexes, and choosing the right balancing strategy can significantly improve the performance of B-tree indexes.
