“Tree planting specialist”

Time:2021-3-4


Perception:

Taste: ants on the tree

Cooking time: 10min

This article has been included inGithub github.com/GeekhytThank you, star.

Mr. Zhou Shuren once said: if you learn tree well, you will master half of data structure and algorithm!

Canteen owner (Tong oba): even if we are the leaf nodes in the Internet wave, we also need to have the spirit of shaking the tree, even if it is too much to shake the tree. Because even if you are just an ordinary person all your life, you can’t end your life just to be an ordinary person.

Today’s menu, ants on the tree, let’s introduce the actors.

Popular science of tree related terms

  • Root node
  • Leaf node
  • Parent node
  • Child node
  • Brother node
  • height
  • depth
  • layer

A yesRoot node. C. D, F, G areLeaf node. A is the sum of B and EParent node. B and E are a’sChild node. B. Between E and eBrother nodeHeight, depth, layerAs shown in the figure above. For the convenience of understanding memory,heightJust look up,depthJust look down. Different from height and depth,layerCompared with the buildings in inception, the buildings are calculated from the first floor. The buildings in inception are upside down, from top to bottom.

Binary tree

Each node has at most two child nodes, namelyLeft child nodeandRight child node

Full binary tree

All the leaf nodes are at the bottom, except the leaf node, each node has two left and right sub nodes. The graph above is a full binary tree.

Complete binary tree

The leaf nodes are in the bottom two layers, and the leaf nodes of the last layer are arranged on the left. Besides the last layer, the number of nodes of other layers should reach the maximum.

Heap is actually a kind of complete binary tree, and the general storage method is array.

By using array to store complete binary tree, there is no need to store the pointer of left and right child nodes as chain storage, which can save memory.

Traversal of binary tree

  • Preorder traversal: first print the current node, then print the left subtree of the current node, and finally print the right subtree of the current node(ABCDEFG)
  • Middle order traversal: first print the left subtree of the current node, then print the current node, and finally print the right subtree of the current node(CBDAFEG)
  • After traversal: first print the left subtree of the current node, then print the right subtree of the current node, and finally print the current node(CDBFGEA)
//Preorder traversal
const preorderTraversal = function(root) {
    const result = [];
    function pushRoot(node) {
        if (node != null) {
            result.push(node.val);
            if (node.left != null) {
                pushRoot(node.left);
            }
            if (node.right != null){ 
                pushRoot(node.right);
            } 
        }
    }
    pushRoot(root);
    return result;
};
//Middle order traversal
const inorderTraversal = function(root) {
    const result = [];
    function pushRoot(node) {
        if (node != null) {
            if (node.left != null) {
                pushRoot(node.left);
            }
            result.push(node.val);
            if (node.right != null){ 
                pushRoot(node.right);
            } 
        }
    }
    pushRoot(root);
    return result;
};
//Postorder traversal
const postorderTraversal = function(root) {
    const result = [];
    function pushRoot(node) {
        if (node != null) {
            if (node.left != null) {
                pushRoot(node.left);
            }
            if (node.right != null){ 
                pushRoot(node.right);
            }
            result.push(node.val);
        }
    }
    pushRoot(root);
    return result;
};

Complexity analysis

  • Time complexity: O (n)
  • Spatial complexity: the worst case is O (n), the average is O (logn)

Binary search tree

Also known as binary sort tree, binary search tree.

Traversing binary search tree in middle order can output ordered data sequence, and the time complexity is highO(n)Very efficient.

Any node in the binary search tree,The value of every node in the left subtree is less than that of this node, while the value of every node in the right subtree is greater than that of this node.

In binary search tree, the time complexity of many operations such as search, insert and delete is proportional to the height of the tree. What is the time complexity of the two extreme casesO(n)andO(logn)Corresponding to the case that the binary tree degenerates into a linked list and the case of a complete binary tree.

In extreme cases, complexity degradation is not what we want, so we need to design a balanced binary search tree. Height approximation of balanced binary search treelognTherefore, the time complexity of search, insert and delete operations is relatively stable, which is rightO(logn)

Adelson velsky Landis tree

AVL treeIt defines that the height difference between the left and right subtrees of any node is not more than 1, and both the left and right subtrees are a balanced binary tree.

AVL treeIt is a highly balanced binary search tree. Although the search efficiency is high, in order to maintain the height balance, the insertion and deletion operations need to be adjusted (left-hand, right-hand), which requires a lot of maintenance costs.

Red black tree

The red black tree can be called “net mangrove”, and its appearance rate is one skyline higher than other trees. Different from AVL tree, red black tree only achieves approximate balance, not strict balance. Therefore, the cost of maintaining the balance is lower than that of AVL tree, the performance of search, insert, delete and other operations are relatively stable, and the time complexity is lowO(logn)

A qualified red black tree should meet the following requirements:

  • Each node is either red or black
  • The root node is black
  • Each leaf node is a black empty node (the leaf node does not store data)
  • Adjacent nodes cannot be red at the same time, and red and black are separated
  • Each node contains the same number of black nodes in all paths from the node to its reachable leaf node

Red black tree has a large number of applications in engineering, because the stability of performance is very high. Because the performance of red black tree is relatively stable, it carries the banner of engineering application.

Trie tree

Behind the powerful keyword prompt function of search engines such as Google and Baidu, the most basic principle isTrie treeBy changing space for time and using the common prefix of string, the query time is reduced to improve the efficiency. In addition, there are many applications, such as: IP routing using the longest prefix matching algorithm of trie tree, using forwarding table to select the path and intelligent tips in IDE.

Trie treeIs an atypical multi tree model, it is different from the general multi tree, we can compare their node data structure design. A general multi tree node contains node values and pointers to child nodes. The node of trie tree contains whether the node is the end of a string and the alphabet mapping table. Through the alphabet, we can get the values of all its children through a parent node.

What is the time complexity of finding a string in the trie treeO(k), K is the length of the string to find.

The trie tree in the figure above is made up of five wordscolor、coat、city、hi、hot. The value of the root node is null.

Leetcode 208. Implementation of trie tree

class Trie {
  constructor() {
    this.root = {};
  }
  insert(word) {
    let curr = this.root;
    word.split('').forEach(ch => (curr = curr[ch] = curr[ch] || {}));
    curr.isWord = true;
  }
  traverse(word) {
    let curr = this.root;
    for (let i = 0; i < word.length; i++) {
      if (!curr) return null;
      curr = curr[word[i]];
    }
    return curr;
  }
  search(word) {
    let node = this.traverse(word);
    return !!node && !!node.isWord;
  }
  startsWith(word) {
    return !!this.traverse(word);
  }
}

B + tree

We know that the query speed of storing index in content is faster than that of storing index in disk. However, when the amount of data is large, the index also increases. Memory is limited and we have to store the index on disk. Then, how to improve the efficiency of reading from the disk has become one of the key engineering.

Most indexes of relational databases, such as MySQL and Oracle, are implemented with B + trees.B + tree is more suitable for building indexes stored on disk than red black tree. B + tree is a multi tree, and its height is lower than that of red black tree when building index with the same number of data. When querying data with index, reading B + tree index requires less disk IO times.

A B-tree of order m satisfies the following characteristics:

  • The number of sub nodes K of each node satisfies m > k > m / 2, and the number of sub nodes of root node can not exceed M / 2
  • Through the two-way linked list, the leaf nodes are connected in series to facilitate searching by interval
  • M-ary tree only stores indexes, not data
  • In general, the root node is stored in memory, and other nodes are stored in disk

reference resources

  • The beauty of data structure and algorithm

❤️ Three strokes of love

1. If you see this, please support it. YourfabulousIt’s my motivation.

2. pay attention to the front desk of the official account.Your front-end canteen, remember to eat on time

3. This article has been included in the front canteenGithub github.com/GeekhytThank you, star.