[Data structure] The concept of binary tree and the implementation of heap (on)

Table of contents

1. Tree concept and structure

2. The concept of binary tree

 2.3 Storage structure of binary tree

3. The sequential structure of the binary tree and the implementation of the heap

3.1 The concept of heap

3.2 Implementation of the heap

3.3 Realize the relevant interface of the heap

3.5 Downward adjustment method

3.6 Heap initialization

 3.7 Destruction of the heap

3.8 Get the top element of the heap

3.9 Get the number of elements in the heap

3.10 Heap Insertion

3.11 Heap deletion 

 3.12 Heap sort


1. Tree concept and structure

1.1 The concept of a tree
A tree is a nonlinear data structure, which is a set of hierarchical relationships composed of n (n>=0) finite nodes. It is called a tree because it looks like an upside-down tree, which means it has the roots pointing up and the leaves pointing down.
There is a special node called the root node. The root node has no predecessor nodes
. Except the root node, the other nodes are divided into M (M>0) disjoint sets T1, T2, ..., Tm, where Each set Ti (1<= i<= m) is a subtree similar in structure to a tree. The root node of each subtree has one and only one predecessor, and can have 0 or more successors. Therefore, the tree is defined recursively. In the tree structure,
we need to pay attention to the tree structure . There can be no intersection between subtrees, otherwise it is not tree structure

 As shown in the figure above, when there is an intersection between subtrees, the structure cannot be called a tree at this time, it should be called a graph

Degree of a node : The number of subtrees contained in a node is called the degree of the node; As shown in the figure above: A is a 6-
leaf node or terminal node : a node with a degree of 0 is called a leaf node; As shown in the figure above: B, C, Nodes such as H, I... are leaf nodes,
non-terminal nodes or branch nodes : nodes whose degree is not 0; as shown in the above figure: nodes such as D, E, F, G... are branch nodes,
parent nodes or parent nodes : if A node contains child nodes, then this node is called the parent node of its child nodes; As shown in the above figure: A is the parent node of B Child node
or child node : the root node of the subtree contained in a node is called the child node of the node; As shown in the figure above: B is a child node of A.
Brother nodes : nodes with the same parent node are called brother nodes; as shown in the figure above: B and C are sibling nodes ( note that they must be brothers) . The degree of a node is called the degree of the tree; as shown in the figure above: the degree of the tree is 6 (the degree of the tree can also be understood as how many child nodes a parent node has, as many degrees ) The hierarchy of nodes : defined from the root , the root is the first layer, the child nodes of the root are the second layer, and so on; (there are 4 layers in the above figure) tree height or depth : the maximum level of nodes in the tree; as in the above figure: the height of the tree is 4 (4 layer) cousin nodes : nodes whose parents are on the same layer are cousins ; as shown in the figure above: H and I are each other’s ancestors of sibling nodes : all nodes on the branches from the root to the node; as shown in the figure above: A is all node's ancestors





: Any node in the subtree rooted at a node is called a descendant of the node. As shown in the figure above: All nodes are descendants of A
Forest : A collection of m (m>0) disjoint trees is called a forest;


The tree structure is more complicated than the linear table, and it is more troublesome to store and express. Since the value range is saved, the relationship between nodes and nodes is also saved. In practice, there are many ways to represent trees, such as : parent representation , child representation , child parent representation , and child sibling representation . Here we simply understand the most commonly used child brother notation.

 The child brother notation is also known as the left child right brother notation, as shown in the figure below, the child pointer points to its child node, and the brother pointer points to its brother

 The practical application of the tree is like the file directory in our computer is realized by the tree structure

2. The concept of binary tree

2.1 Concept
A binary tree is a finite set of nodes, the set:
1. Or empty
2. Consists of a root node plus two binary trees called left subtree and right subtree


It can be seen from the above figure:
1. There is no node with a degree greater than 2 in a binary tree
2. The subtrees of a binary tree are divided into left and right, and the order cannot be reversed, so a binary tree is an ordered tree
Note: For any binary tree, it consists of the following Combination of situations:

 

 In reality, we may also have seen binary trees, as shown in the figure below (the tree is a complete binary tree)

2.2 Special binary tree:
1. Full binary tree: a binary tree, if the number of nodes in each layer reaches the maximum value, then this binary tree is a full binary tree. That is to say, if the number of layers of a binary tree is K, and the total number of nodes is 2^k-1, then it is a full binary tree.
2. Complete binary tree: A complete binary tree is a very efficient data structure, and a complete binary tree is derived from a full binary tree. For a binary tree with a depth of K and n nodes, it is called a complete binary tree if and only if each node has a one-to-one correspondence with the nodes numbered from 1 to n in the full binary tree with a depth of K. It should be noted that a full binary tree is a special kind of complete binary tree.
2.4 Properties of binary trees 1. If the number of layers of the root node is specified as i, then there are at most 2^(i-1) nodes on the i-th layer
of a non-empty binary tree . 2. If the number of layers of the root node is specified as h, then the maximum number of nodes in a binary tree with depth h is 2^h-1 . 3. For any binary tree, if the degree is 0, the number of leaf nodes is X0, and the number of branch nodes with degree 2 is X2, then there is X0=X2+1 4. If the number of layers of the root node is specified as h, the depth of a full binary tree with n nodes: h= log(n+1)  (log is based on 2, n+1 logarithm) 5. For a complete binary tree with n nodes, if all nodes are numbered from 0 according to the order of the array from top to bottom and from left to right, then for the node with the sequence number i:



1. If i>0, the parent number of the node at i position: (i-1)/2; i=0, i is the root node number, no parent node 2. If 2i+1<
n, the left child number: 2i+ 1, 2i+1>=n otherwise there is no left child
3. If 2i+2<n, the serial number of the right child: 2i+2, 2i+2>=n otherwise there is no right child

The above (1.2.3) can be understood as Know the parent node in the array, find the subscript of the left child node 2*parnet+1, and the subscript of the right child node is 2*parent+2. If you know the subscript child of one of the child nodes, find the subscript of the parent node as (child -1)/2

The following figure is a schematic diagram of a full binary tree and a complete binary tree

 2.3 Storage structure of binary tree


Binary trees can generally be stored using two structures, a sequential structure and a chain structure.
1. Sequential storage
Sequential structure storage uses arrays for storage . Generally, arrays are only suitable for representing complete binary trees , because it is not a waste of space for complete binary trees
. In reality, only heaps are stored in arrays. Binary tree sequential storage is physically an array and logically a binary tree. If it is , it will waste a lot of space

 2. Linked storage
The linked storage structure of the binary tree means that a linked list is used to represent a binary tree, that is, a link is used to indicate the logical relationship of elements. The usual method is that each node in the linked list is composed of three fields, the data field and the left and right pointer fields, and the left and right pointers are used to give the storage addresses of the link points where the left child and right child of the node are located. The chain structure is divided into binary chain and triple chain. The implementation of binary tree is generally binary chain, and the data structure of triple chain is used, such as red-black tree.

3. The sequential structure of the binary tree and the implementation of the heap

3.1 The concept of heap


Ordinary binary trees are not suitable for storage in arrays, because there may be a lot of wasted space. The complete binary tree is more suitable for sequential structure storage. In reality, we usually store the heap (a binary tree) in an array of sequential structures. It should be noted that the heap here and the heap in the virtual process address space of the operating system are two different things. One is the data structure, and the other is the management in the operating system. A region of memory is segmented
The concept and structure of the heap

Heap is a general term for a special class of data structures in computer science. A heap is usually an array of objects that can be thought of as a tree. A heap always satisfies the following properties:

The value of a node in the heap is always not greater than or not less than the value of its parent node;

The heap is always a complete binary tree.

The heap with the largest root node is called the largest heap or the big root heap, and the heap with the smallest root node is called the smallest heap or the small root heap. Common heaps include binary heaps, Fibonacci heaps , etc.

A heap is a non-linear data structure, equivalent to a one-dimensional array.

 The picture above shows a large pile, which is characterized by the fact that the parent node is larger than the child nodes (for sorting in ascending order)

 The above picture is a picture of a small heap. The characteristic of a small heap is that the parent nodes are smaller than the child nodes (for sorting in descending order)

3.2 Implementation of the heap

Heap Down Adjustment Algorithm
Now we are given an array, logically regarded as a complete binary tree. We can adjust it into a small heap through the downward adjustment algorithm starting from the root node. The downward adjustment algorithm has a premise: the left and right subtrees must be a heap to be adjusted.

 The structure like the above picture can be adjusted by the downward adjustment method to get the following picture

 But when we are given an array that is out of order and the left and right subtrees are not small heaps, how should we build the heap? At this time, we can consider building the heap from the bottom up. First, build the heap below. Build the above heap, and the time complexity of building the heap at this time is O(n)

3.3 Realize the relevant interface of the heap

3.5 Downward adjustment method

 

 

3.6 Heap initialization

 3.7 Destruction of the heap

3.8 Get the top element of the heap

3.9 Get the number of elements in the heap

3.10 Heap Insertion

 First insert a 10 to the end of the array, and then perform an upward adjustment algorithm until the heap is satisfied. As shown below

 the code

 

3.11 Heap deletion 

Deleting the heap is to delete the data at the top of the heap, replace the data at the top of the heap with the last data, then delete the last data in the array, and then perform the downward adjustment algorithm.

 

 3.12 Heap sort

If we build a large heap, we can sort in ascending order. Why can we do this? Because the element at the top of the heap is the largest element at this time, we only need to exchange the element at the top of the heap with the last element in the heap, and then use to The downward adjustment method can select the next largest number, and then change it again, and so on, and finally the numbers in the array become ascending order

 

Guess you like

Origin blog.csdn.net/m0_72532428/article/details/129938408