A node in a binary tree can have at most two children: one is the left child and the other is the right child. The binary search tree can also be referred to as BST, which is a type of binary tree, but only allows you to store values smaller (than the parent node) in the left node and store larger values (than the parent node) in the right node.
First we need to create a node class to store node data and its left and right child nodes:
class Node {
constructor(key) {
this.key = key;
this.left = null;
this.right = null;
}
}
Then you need to create a binary search tree class and implement the initial construction of the binary search tree:
class BinaryTree {
constructor() {
this.root = null;
}
// 插入
insert(key) {
const newNode = new Node(key);
if (this.root === null) {
this.root = newNode;
}
this.inertNode(this.root, newNode);
}
inertNode(node, newNode) {
// 判断是放在左边还是右边
// 如果插入的节点的值小于节点值则进入左子树
if (newNode.key < node.key) {
// 直到左子树为空
if (node.left === null) {
node.left = newNode;
}
// 如果没到左子树的底部
else {
this.inertNode(node.left, newNode);
}
}
// 如果插入的节点的值大于节点值则进入右子树
else if (newNode.key > node.key) {
if (node.right === null) {
node.right = newNode;
} else {
this.inertNode(node.right, newNode);
}
}
}
}
The initial construction of the binary search tree can be realized above. Next, let's understand the traversal of the tree, and you can understand it by looking at a few examples.
Inorder traversal example:
Preorder traversal example: Postorder traversal example:
Go directly to the complete code:
class Node {
constructor(key) {
this.key = key;
this.left = null;
this.right = null;
}
}
class BinaryTree {
constructor() {
this.root = null;
}
// 插入
insert(key) {
const newNode = new Node(key);
if (this.root === null) {
this.root = newNode;
}
this.inertNode(this.root, newNode);
}
inertNode(node, newNode) {
// 判断是放在左边还是右边
// 如果插入的节点的值小于节点值则进入左子树
if (newNode.key < node.key) {
// 直到左子树为空
if (node.left === null) {
node.left = newNode;
}
// 如果没到左子树的底部
else {
this.inertNode(node.left, newNode);
}
}
// 如果插入的节点的值大于节点值则进入右子树
else if (newNode.key > node.key) {
if (node.right === null) {
node.right = newNode;
} else {
this.inertNode(node.right, newNode);
}
}
}
// 中序遍历
middleorder() {
let info = [];
this.middleorderNode(this.root, ({ key }) => {
info.push(key);
})
return info;
}
middleorderNode(node, callback) {
if (node) {
this.middleorderNode(node.left, callback);
callback(node);
this.middleorderNode(node.right, callback);
}
}
// 先序遍历
preorder() {
let info = [];
this.preorderNode(this.root, ({ key }) => {
info.push(key);
})
return info;
}
preorderNode(node, callback) {
if (node) {
callback(node);
this.preorderNode(node.left, callback);
this.preorderNode(node.right, callback);
}
}
// 后序遍历
afterorder() {
let info = [];
this.afterorderNode(this.root, ({ key }) => {
info.push(key);
})
return info;
}
afterorderNode(node, callback) {
if (node) {
this.afterorderNode(node.left, callback);
this.afterorderNode(node.right, callback);
callback(node);
}
}
}
// 初始化一颗二叉排序树
const binerytree = new BinaryTree();
// key值
const keys = [19, 8, 15, 24, 45, 12, 5];
// 生成二叉排序树
keys.forEach(key => binerytree.insert(key));
console.log('先序遍历结果:\n');
console.log(binerytree.preorder());
console.log('中序遍历结果:\n');
console.log(binerytree.middleorder());
console.log('后序遍历结果:\n');
console.log(binerytree.afterorder());
Output result: