Literacy data structure – Introduction to binary tree


What is a binary tree

There is a root nodeExpand two child nodes downThe two child nodes can expand downward. A structure similar to this becomes a binary tree

<img style=”zoom:50%;” />

The above is enough to be a binary tree. Of course, if there is a binary tree, there are Trident trees and Quad trees.

<img style=”zoom:50%;” />

Concept of corresponding node in tree

<img style=”zoom:50%;” />

  • Node a is the of node B【Parent node
  • Node B is the of node a【Child node
  • B. The parent nodes of C and D are the same node, so they are called each other【Sibling node
  • E node has no parent node, so we call it【Root node
  • G. H, I, J, K and l have no child nodes, so we call it【Leaf node
  • Height of node: the longest path from node to leaf node
  • Node depth: the number of nodes from the root node to this node
  • Layers of node: depth of node + 1

Types of binary trees

On the binary tree, with a variety of other attributes, other tree structures will be derived.

  • Full binary tree

    <img style=”zoom:50%;” />

    Leaf nodes are all at the bottom. Except leaf nodes, each node has two child nodes. This binary tree is called【Full binary tree】。

  • Complete binary tree

    <img style=”zoom:50%;” />

    Leaf nodes are in the bottom two layers. The last leaf nodes are arranged on the left, and the number of nodes in other layers except the last layer should reach the maximum. This binary tree is called【Complete binary tree

  • Binary search tree

    <img style=”zoom:50%;” />

    If the left subtree of any node is not empty, the values of all nodes in the left subtree are less than those of its root node;

    If the right subtree of any node is not empty, the values of all nodes on the right subtree are greater than or equal to the values of its root node;

    Any left subtree or right subtree is also a binary search tree.

  • heap

    <img style=”zoom:50%;” />

    Heap is a binary tree implemented by array, so it does not use parent or child pointers. Heaps are sorted by heap attributes.

    Common methods of heap

    Build priority queues, support heap sorting, and find the minimum (or maximum) value in a set

    Heaps are divided into two types: maximum heap and minimum heap. The difference between them lies in the sorting mode of nodes.

    In the maximum heap, the value of the parent node and the value of each child node should be larger. In the minimum heap, the value of the parent node is smaller than that of each child node. This is the so-called “heap attribute”, and this attribute is true for every node in the heap.

  • AVL
  • Red black tree
  • Segment tree
  • Dictionary tree
  • Joint search set

Tree traversal

  • Preorder root left and right
  • Middle order left root
  • Posterior left and right roots
  • DFS depth first traversal

    Code template (recursive writing)

    visited = set() 
    def dfs(node, visited):
        if node in visited: # terminator
            # already visited 
        # process current node here. 
        for next_node in node.children(): 
            if next_node not in visited: 
                dfs(next_node, visited)

    Non recursive writing

    def DFS(self, tree): 
        if tree.root is None: 
            return [] 
        visited, stack = [], [tree.root]
        while stack: 
            node = stack.pop() 
            process (node) 
            nodes = generate_related_nodes(node) 
        # other processing work 
  • BFS breadth first traversal (sequence traversal)

    Code template

    def BFS(graph, start, end):
        visited = set()
        queue = [] 
        while queue: 
            node = queue.pop() 
            nodes = generate_related_nodes(node) 
        # other processing work 

This paper is based on the operation tool platform of blog group sending one article and multiple sendingOpenWriterelease

Recommended Today

The VBA ide displays a project that is not open. The solution cannot be operated

Reading time |0.24 minutes word count |388 characters primary coverage |1. Introduction & background “VBA ide displays the project that is not open, and the solution cannot be operated” Author | SCscHero Writing time | 2021/12/29 PM7:28 Article type |Series Degree of completion |Completed motto Every great cause has a trivial beginning. 1、 Introduction & […]