[Algorithm and Data Structure 09] What are trees, binary trees, and binary search trees?

Hello everyone! I am [AI bacteria] , a 不熬夜programmer. Me 热爱AI、热爱分享、热爱开源! This blog is my summary and reflection on learning. If you are also 深度学习、机器视觉、算法、Python、C++interested, you can focus on my dynamic, we learn together and progress together ~

Speaking of trees, everyone is familiar with them. After all, they are common things in daily life. But today’s protagonist is not a tree, let’s talk about trees, binary trees and binary search trees in the data structure, and their basic operations!

Insert picture description here


One, tree and binary tree

1.1 What is a tree?

A tree is a data structure, which is a hierarchical collection composed of n (n>=1) finite nodes. It is called a "tree" because it looks like an upside-down tree, which means it has its roots facing up and its leaves facing down.

The tree is composed of nodes and edges, and there is no data structure in the ring. Through the following figure, we can understand the structure of the tree more intuitively.

Insert picture description here

The tree satisfies the characteristics of the recursive definition. In other words, if a data structure is a tree structure, then after removing the root node, several substructures are also trees, usually called subtrees.

In a tree, depending on the hierarchical relationship between nodes, the names of nodes are also different. Let's take a look at the following tree, as shown in the figure below:
Insert picture description here
The relationships and names of different nodes are as follows:

  • A higher-level node is the node B and node C, then A and C B is the parent node , A, B and C are the child nodes .
  • If B and C are both the "children" of A, then B and C can be called sibling nodes .
  • If A has no parent node, then A can be called the root node .
  • Nodes G, H, I, and F have no child nodes, so G, H, I, and F are called leaf nodes .

When there is a tree, you need to use depth and layer to describe the location of the nodes in the tree . As shown in the figure above, the level of nodes is counted from the root node:

  • The root is the first level, such as: A
  • The "children" of the root are the second level, such as B, C
  • The "child" of the root "child" is the third level, such as D, E, F
  • By analogy, the fourth layer (the last layer) is: G, H, I

The maximum number of layers of nodes in the tree is the depth of the tree (called depth, also known as height), so this is a tree with a depth of 4.

1.2 What is a binary tree?

In the large family of trees, there is a special tree that is frequently used, and it is a binary tree . In a binary tree, each node has at most two branches, that is, each node has at most two child nodes, which are called the left child node and the right child node .

In the binary tree, there are two most special types, as shown in the following figure:

Insert picture description here

  • A full binary tree is defined as all nodes except leaf nodes have 2 child nodes .
  • A complete binary tree is defined as the maximum number of nodes in other layers except the last layer, and the leaf nodes of the last layer are all arranged to the left .

You may be confused. A complete binary tree does not look complete, but why do you call it that way? This is actually related to the storage of the binary tree. There are two ways to store a binary tree, one is the pointer-based chain storage method , and the other is the array-based sequential storage method .

  • The chain storage method is like a linked list. Each node has three fields, one stores data, and the other two store pointers to the left and right child nodes, as shown in the following figure:

Insert picture description here

  • The sequential storage method is to store the nodes in the array according to the law, as shown in the figure below. For the convenience of calculation, we will agree to place the root node at the position where the subscript is 1. Subsequently, node B is stored at the position of subscript 2, node C is stored in the position of subscript 3, and so on.
    Insert picture description here

The reason why it is called a complete binary tree is from the perspective of storage space utilization efficiency . For a complete binary tree, only the storage location with subscript 0 is wasted. If it is an incomplete binary tree, a lot of storage space will be wasted.

In the incomplete binary tree shown in the figure below, it needs to reserve the positions of 5 and 6. At the same time, the positions of the two child nodes 10 and 11 of 5 and the positions of the two child nodes 12 and 13 of 6. Such a binary tree does not fully utilize the storage space of the array.
Insert picture description here


Second, the pre-order, middle-order, and post-order traversal of the tree

Next, we take the binary tree as an example to introduce the operation of the tree. The operations of other types of trees are basically similar to the binary tree.

It can be found that the data structures we have learned before are all "one-to-one" relationships, that is, the previous data only has a connection relationship with the following data, such as linked lists, stacks, and queues. The tree structure is a " one-to-many " relationship, that is, the previous parent node has a connection relationship with several child nodes below.

In the "one-to-one" structure, the search has a certain value, and each piece of data can be traversed directly in sequence. However, the tree is a "one-to-many" relationship, so in what order should it be traversed?

In fact, there are three very classic methods for traversing a tree, which are pre-order traversal, middle-order traversal, and post-order traversal. The order here refers to the traversal order of the parent node . The first order is to traverse the parent node first, the middle order is to traverse the parent node in the middle, and the latter order is to traverse the parent node last.

No matter what kind of traversal, it is done through recursive calls . As shown below:

Insert picture description here

  • Preorder traversal , for any node in the tree, print this node first, then traverse its left subtree in preorder, and finally traverse its right subtree in preorder.
  • Middle-order traversal , for any node in the tree, first traverse its left subtree in middle order, then print this node, and finally traverse its right subtree in middle order.
  • Post-order traversal , for any node in the tree, sequentially traverse its left subtree, then post-order its right subtree, and finally print itself.

Three, the characteristics of the binary search tree

For a binary tree without any special properties, apart from the time complexity of traversal, the time complexity of actually performing addition and deletion operations is O(1). The search operation of tree data is the same as the linked list. It needs to traverse each data to judge, so the time complexity is O(n).

But when the binary tree has some characteristics (such as binary search tree), you can use these characteristics to reduce the time complexity.

Binary search tree (also called binary search tree) has the following characteristics:

  • At any node in the binary search tree, the value of each node in the left subtree must be less than the value of this node; the value of each node in the right subtree must be greater than this The value of the node.
  • In a binary search tree, the situation where the values ​​of two nodes are equal will be avoided as much as possible.
  • By traversing the binary search tree in order, you can output an ordered data queue from small to large. As shown in the figure below, the result of the in-order traversal is 10, 13, 15, 16, 20, 21, 22, 26.

Insert picture description here
So the binary search tree can be simply thought of as a binary tree sorted by rules.


Fourth, the addition, deletion, and check operation of the binary search tree

3.1 Find operation

When using a binary search tree to perform a search operation, we can make the following judgments:

  • First judge whether the root node is equal to the data to be searched, and return if it is.
  • If the root node is greater than the data to be searched, the search is performed recursively in the left subtree until the leaf node.
  • If the root node is smaller than the data to be searched, the search is performed recursively in the right subtree until the leaf node.

The time complexity consumed by such " binary search " can be reduced to O(logn) . Regarding binary search, I will talk about it later in the algorithm part.

3.2 Insert operation

Performing an insert operation in a binary search tree is also very simple. Starting from the root node, if the data to be inserted is larger than the data of the root node, and the right child node of the root node is not empty, continue to try to perform the insert operation in the right subtree of the root node. Insertion is performed until an empty child node is found.

As shown in the figure below, if the value of X to be inserted is 14, you need to determine the size relationship between X and the root node:

  • Since 14 is less than 16, focus on its left subtree and continue to judge the relationship between X and 13.
  • Since 14 is greater than 13, focus on the right subtree and continue to judge the relationship between X and 15.
  • Since 14 is less than 15, focus on its left subtree.

Because the left subtree is empty at this time, the relationship between the left pointer of 15 nodes and the node X is established directly through the pointer, and the insertion action is completed.
Insert picture description here
The time complexity of inserting data into a binary search tree is O(logn). But this does not mean that it is more complicated than ordinary binary trees. The reason is that the time complexity here is more spent traversing the data to find the search position, and the time complexity of actually executing the insertion action is still O(1).

3.3 Delete operation

The deletion operation of the binary search tree will be more complicated, because the tree after deleting a certain node still has to satisfy the properties of the binary search tree . We divide the discussion into the following three situations.

(1) If the node to be deleted is a leaf node , delete it directly and point its parent node pointer to null.
Insert picture description here

(2) If the node to be deleted has only one child node , just replace the pointer of the child node pointed to by its parent node with the pointer of its child node.
Insert picture description here(3) If the node to be deleted has two sub-nodes , there are two feasible operation methods.

  • The first is to find the largest node in the left subtree of this node and replace the node to be deleted.

Insert picture description here

  • The second is to find the smallest node in the right subtree of this node and replace the node to be deleted.

Insert picture description here


This article is my study notes for studying "Relearning Data Structures and Algorithms". It is only used for learning and communication. It is hereby shared, please do not reprint it commercially! Picture reference in the article: https://kaiwu.lagou.com/course/courseInfo.htm?courseId=185#/detail/pc?id=3347

Insert picture description here

Develop a habit, like first and then watch! Your support is the biggest motivation for my creation!

Guess you like

Origin blog.csdn.net/wjinjie/article/details/108484738