


Detailed explanation of binary tree of JavaScript data structures and algorithms_Basic knowledge
The concept of binary tree
Binary Tree is a finite set of n (n>=0) nodes. The set is either an empty set (empty binary tree), or it consists of a root node and two mutually disjoint trees. It is a binary tree consisting of the left subtree and the right subtree of the root node.
Characteristics of binary trees
Each node has at most two subtrees, so there is no node with degree greater than 2 in the binary tree. Each node in the binary tree is an object, and each data node has three pointers, which are pointers to the parent, left child, and right child. Each node is connected to each other through pointers. The relationship between connected pointers is a parent-child relationship.
Definition of binary tree node
Binary tree nodes are defined as follows:
struct BinaryTreeNode
{
int m_nValue;
BinaryTreeNode* m_pLeft;
BinaryTreeNode* m_pRight;
};
Five basic forms of binary trees
Empty binary tree
There is only one root node
The root node has only the left subtree
The root node has only the right subtree
The root node has both left and right subtrees
There are only two situations for an ordinary tree with three nodes: two layers or three layers. But since the binary tree needs to distinguish left and right, it will evolve into the following five forms:
Special Binary Tree
Slanted Tree
As shown in the 2nd and 3rd pictures in the last picture above.
Full Binary Tree
In a binary tree, if all branch nodes have left subtrees and right subtrees, and all leaves are on the same level, such a binary tree is called a full binary tree. As shown below:
Complete Binary Tree
A complete binary tree means that the left side of the last level is full, the right side may be full or not, and then the remaining levels are full. A binary tree with depth k and number of nodes 2^k - 1 is a full binary tree (complete binary tree). It is a tree with depth k and no gaps.
The characteristics of a complete binary tree are:
Leaf nodes can only appear on the bottom two levels.
The lowest leaves must be concentrated on the left continuous position.
On the penultimate layer, if there are leaf nodes, they must be in continuous positions on the right.
If the node degree is 1, then the node has only left child.
A binary tree with the same node tree, a complete binary tree has the smallest depth.
Note: A full binary tree must be a complete binary tree, but a complete binary tree is not necessarily a full binary tree.
The algorithm is as follows:
bool is_complete(tree *root)
{
queue q;
tree *ptr;
// Perform breadth-first traversal (level traversal) and put NULL nodes into the queue
q.push(root);
While ((ptr = q.pop()) != NULL)
{
q.push(ptr->left);
q.push(ptr->right);
}
// Determine whether there are any unvisited nodes
While (!q.is_empty())
{
ptr = q.pop();
// If there are unvisited non-NULL nodes, the tree has holes and is a non-complete binary tree
If (NULL != ptr)
return false;
}
return true;
}
Properties of binary trees
Property 1 of a binary tree: There are at most 2^(i-1) nodes (i>=1) on the i-th level of the binary tree
Property 2 of binary trees: A binary tree with depth k has at most 2^k-1 nodes (k>=1)
Sequential storage structure of binary tree
The sequential storage structure of a binary tree uses a one-dimensional array to store each node in the binary tree, and the storage location of the nodes can reflect the logical relationship between the nodes.
Binary linked list
Since the applicability of sequential storage is not strong, we must consider the chain storage structure. According to international practice, the storage of binary trees generally uses a chain storage structure.
Each node of a binary tree has at most two children, so it is a natural idea to design a data field and two pointer fields for it. We call such a linked list a binary linked list.
Binary tree traversal
Traversing a binary tree means starting from the root node and visiting all the nodes in the binary tree in a certain order so that each node is visited once and only once.
There are three ways to traverse a binary tree, as follows:
(1) Preorder traversal (DLR), first visit the root node, then traverse the left subtree, and finally traverse the right subtree. The abbreviation root-left-right.
(2) In-order traversal (LDR), first traverses the left subtree, then visits the root node, and finally traverses the right subtree. Abbreviated as left-root-right.
(3) Post-order traversal (LRD), first traverses the left subtree, then traverses the right subtree, and finally visits the root node. Abbreviated as left-right-root.
Preorder traversal:
If the binary tree is empty, no operation returns, otherwise the root node is visited first, then the left subtree is traversed in preorder, and then the right subtree is traversed in preorder.
The order of traversal is: A B D H I E J C F K G
//Pre-order traversal
function preOrder(node){
If(!node == null){
putstr(node.show() " ");
preOrder(node.left);
preOrder(node.right);
}
}
In-order traversal:
If the tree is empty, no operation returns, otherwise starting from the root node (note that the root node is not visited first), traverse the left subtree of the root node in in-order, then visit the root node, and finally Traverse the right subtree in order.
The order of traversal is: H D I B E J A F K C G
//Use recursion to implement in-order traversal
function inOrder(node){
If(!(node == null)){
inOrder(node.left);//Visit the left subtree first
putstr(node.show() " ");//Visit the root node again
inOrder(node.right);//Last access to the right subtree
}
}
Post-order traversal:
If the tree is empty, the no-op operation returns, otherwise the left and right subtrees are traversed from left to right, first the leaves and then the nodes, and finally the root node is visited.
The order of traversal is: H I D J E B K F G C A
//Post-order traversal
function postOrder(node){
If(!node == null){
PostOrder(node.left);
postOrder(node.right);
putStr(node.show() " ");
}
}
Implement binary search tree
Binary search tree (BST) is composed of nodes, so we define a Node node object as follows:
function Node(data,left,right){
This.data = data;
This.left = left;//Save the left node link
This.right = right;
This.show = show;
}
function show(){
Return this.data;//Display the data saved in the node
}
Find the maximum and minimum values
Finding the minimum and maximum values on a BST is very simple because the smaller value is always on the left child node. To find the minimum value on a BST, just traverse the left subtree until the last node is found
Find the minimum value
function getMin(){
var current = this.root;
While(!(current.left == null)){
Current = current.left;
}
Return current.data;
}
This method traverses the left subtree of the BST one by one until it traverses to the leftmost node of the BST, which is defined as:
current.left = null;
At this time, the value saved on the current node is the minimum value
Find the maximum value
Finding the maximum value on BST only requires traversing the right subtree until the last node is found, and the value saved on this node is the maximum value.
function getMax(){
var current = this.root;
While(!(current.right == null)){
current = current.right;
}
Return current.data;
}

Whether to choose Python or JavaScript depends on the project type: 1) Choose Python for data science and automation tasks; 2) Choose JavaScript for front-end and full-stack development. Python is favored for its powerful library in data processing and automation, while JavaScript is indispensable for its advantages in web interaction and full-stack development.

Python and JavaScript each have their own advantages, and the choice depends on project needs and personal preferences. 1. Python is easy to learn, with concise syntax, suitable for data science and back-end development, but has a slow execution speed. 2. JavaScript is everywhere in front-end development and has strong asynchronous programming capabilities. Node.js makes it suitable for full-stack development, but the syntax may be complex and error-prone.

JavaScriptisnotbuiltonCorC ;it'saninterpretedlanguagethatrunsonenginesoftenwritteninC .1)JavaScriptwasdesignedasalightweight,interpretedlanguageforwebbrowsers.2)EnginesevolvedfromsimpleinterpreterstoJITcompilers,typicallyinC ,improvingperformance.

JavaScript can be used for front-end and back-end development. The front-end enhances the user experience through DOM operations, and the back-end handles server tasks through Node.js. 1. Front-end example: Change the content of the web page text. 2. Backend example: Create a Node.js server.

Choosing Python or JavaScript should be based on career development, learning curve and ecosystem: 1) Career development: Python is suitable for data science and back-end development, while JavaScript is suitable for front-end and full-stack development. 2) Learning curve: Python syntax is concise and suitable for beginners; JavaScript syntax is flexible. 3) Ecosystem: Python has rich scientific computing libraries, and JavaScript has a powerful front-end framework.

The power of the JavaScript framework lies in simplifying development, improving user experience and application performance. When choosing a framework, consider: 1. Project size and complexity, 2. Team experience, 3. Ecosystem and community support.

Introduction I know you may find it strange, what exactly does JavaScript, C and browser have to do? They seem to be unrelated, but in fact, they play a very important role in modern web development. Today we will discuss the close connection between these three. Through this article, you will learn how JavaScript runs in the browser, the role of C in the browser engine, and how they work together to drive rendering and interaction of web pages. We all know the relationship between JavaScript and browser. JavaScript is the core language of front-end development. It runs directly in the browser, making web pages vivid and interesting. Have you ever wondered why JavaScr

Node.js excels at efficient I/O, largely thanks to streams. Streams process data incrementally, avoiding memory overload—ideal for large files, network tasks, and real-time applications. Combining streams with TypeScript's type safety creates a powe


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

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

Hot Article

Hot Tools

WebStorm Mac version
Useful JavaScript development tools

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

SublimeText3 Linux new version
SublimeText3 Linux latest version

Zend Studio 13.0.1
Powerful PHP integrated development environment
