Binary tree based on data structure


Binary tree is an important type of tree structure. The data structure abstracted from many practical problems is often in the form of a binary tree. Even a general tree can be easily converted into a binary tree, and the storage structure and algorithm of the binary tree are relatively simple, so the binary tree is particularly important. The characteristic of a binary tree is that each node can only have at most two subtrees, and there are left and right points.
A binary tree is a collection of n finite elements, which is either empty or consists of an element called the root and two disjoint binary trees called the left subtree and the right subtree, respectively. sequence tree. When the set is empty, the binary tree is called an empty binary tree. In a binary tree, an element is also called a node

Binary tree properties

A binary tree is a special tree structure with some important properties. These properties are very helpful for understanding and manipulating binary trees. Following are some main properties of binary trees:

  1. Each node has at most two children: Each node can contain at most two children, often referred to as the left child and the right child. This feature is the most basic feature of a binary tree.

  2. Left subtree and right subtree: Each node of the binary tree can be regarded as the root node, and they have their own left subtree and right subtree respectively. This makes the binary tree have a recursive structure.

  3. Depth of the tree (Depth): The depth of the tree is the number of nodes in the longest path from the root node to the furthest leaf node. The depth of the tree can be used to evaluate the height of the tree.

  • The i-th layer has at most 2^(i-1) nodes (i>=1)
  • A binary tree of depth k has at most 2^k - 1 nodes (k>=1).
  • A binary tree with n nodes has a depth of at least log2(n+1)
  1. Height of the tree (Height): The height of the tree is the number of edges of the longest path from the root node to the furthest leaf node. The height of the tree can be used to evaluate the complexity and performance of the tree.
  • A binary tree of height h has at least h nodes and at most 2^h - 1 nodes.
  • For any binary tree, its maximum width is 2^h - 1, and h is its height.
  1. Leaf node (Leaf): A node without child nodes is called a leaf node, also known as a terminal node. Leaf nodes are at the end of the tree.

  2. Internal Node: In addition to the root node and leaf nodes, other nodes are internal nodes, that is, nodes with at least one child node.

  3. Sibling nodes: Nodes with the same parent node are called sibling nodes.

  4. Ancestor node (Ancestor): All nodes on the path from a node to the root node are the ancestor nodes of this node.

  5. Descendant: All nodes in the subtree of a node are descendants of the node.

  6. Binary search tree properties: For binary search tree (BST), it is a special binary tree with the following properties:

  • The value of all nodes in the left subtree is less than or equal to the value of the parent node.
  • All nodes in the right subtree have a value greater than or equal to the value of the parent node.
  • The left and right subtrees are also binary search trees, respectively.
  1. Complete Binary Tree Properties: A complete binary tree is a special kind of binary tree with the following properties:
  • Except for the last layer, the nodes of other layers are full, and the nodes of the last layer are arranged from left to right.
  1. Full binary tree properties: A full binary tree is a special binary tree with the following properties:
  • Except for leaf nodes, each node has two child nodes.

Binary tree classification

  1. Full binary tree: the number of nodes on each level is the maximum number of nodes.
    insert image description here

  2. Complete binary tree: Except for the last layer, the other layers are full, and the nodes in the last layer are arranged to the left.
    insert image description here

  3. Binary Search Tree (BST): The value of the left subtree is less than the root node, and the value of the right subtree is greater than the root node.
    insert image description here

  4. Balanced binary tree: The height difference between the left and right subtrees does not exceed 1, which is usually used to improve search performance.

  5. Red-black tree: Each node is marked with red and black, and self-balancing is maintained through color. It is a balanced binary tree.

  6. B-tree and B+ tree: Extended multi-fork tree, which can be used for database indexing.

traverse binary tree

  1. Preorder traversal: root node -> left subtree -> right subtree
  2. Inorder traversal: left subtree -> root node -> right subtree
  3. Post-order traversal: left subtree —> right subtree —> root node
  4. Layer order traversal: traverse nodes layer by layer
js
// 二叉树节点类
class Node {
    
    
  constructor(val) {
    
    
    this.val = val;
    this.left = null;
    this.right = null;
  }
}

// 前序遍历 
function preorder(root) {
    
    
  if (!root) return [];
  let result = [root.val];
  result = result.concat(preorder(root.left));
  result = result.concat(preorder(root.right));
  return result;
}

// 中序遍历
function inorder(root) {
    
    
  if (!root) return [];
  let result = [];
  result = result.concat(inorder(root.left));
  result.push(root.val);
  result = result.concat(inorder(root.right));
  return result;
} 

// 后序遍历
function postorder(root) {
    
    
  if (!root) return [];
  let result = [];
  result = result.concat(postorder(root.left));
  result = result.concat(postorder(root.right));
  result.push(root.val);
  return result;
}

// 层序遍历 
function levelorder(root) {
    
    
  let result = [];
  let queue = [root];
  while (queue.length) {
    
    
    let curr = queue.shift();
    result.push(curr.val);
    if (curr.left) queue.push(curr.left);
    if (curr.right) queue.push(curr.right);
  }
  return result;
}

let root = new Node(1);
root.left = new Node(2); 
root.right = new Node(3);
root.left.left = new Node(4);
root.left.right = new Node(5);

// 分别测试不同遍历函数
console.log(preorder(root)); // [1, 2, 4, 5, 3]
console.log(inorder(root)); // [4, 2, 5, 1, 3] 
console.log(postorder(root)); // [4, 5, 2, 3, 1]
console.log(levelorder(root)); // [1, 2, 3, 4, 5]

How to judge whether it is a complete binary tree

Judging whether a binary tree is a complete binary tree can be done by means of breadth-first search (BFS). In the process of BFS, we need to traverse each node in the tree and add the nodes to the queue in hierarchical order. For a complete binary tree, after encountering the first empty node, all subsequent nodes should be empty nodes, otherwise it is not a complete binary tree.

The JavaScript code for using BFS to determine whether it is a complete binary tree is given below:

class TreeNode {
    
    
  constructor(data) {
    
    
    this.data = data;
    this.left = null;
    this.right = null;
  }
}

function isCompleteBinaryTree(root) {
    
    
  if (!root) return true;

  const queue = [root];
  let flag = false; // 用来标记是否遇到第一个空节点

  while (queue.length > 0) {
    
    
    const node = queue.shift();

    // 如果遇到第一个空节点
    if (!node) {
    
    
      flag = true;
    } else {
    
    
      // 如果已经遇到空节点,但当前节点不是空节点,则不是完全二叉树
      if (flag) return false;
      
      queue.push(node.left);
      queue.push(node.right);
    }
  }

  return true;
}

// 示例用法:
const binaryTree = new TreeNode(1);
binaryTree.left = new TreeNode(2);
binaryTree.right = new TreeNode(3);
binaryTree.left.left = new TreeNode(4);
binaryTree.left.right = new TreeNode(5);
binaryTree.right.left = new TreeNode(6);
binaryTree.right.right = new TreeNode(7);

console.log(isCompleteBinaryTree(binaryTree)); // 打印: true

A binary tree is created, and isCompleteBinaryTreea function is used to determine whether the tree is a complete binary tree. According to the given binary tree, the result should be true.

Guess you like

Origin blog.csdn.net/study_way/article/details/131991504