当前位置:网站首页>Binary tree structure and heap structure foundation
Binary tree structure and heap structure foundation
2022-06-27 07:45:00 【KevinJune】
1 Binary tree structure
Binary tree (binary tree) It means that the degree of nodes in the tree is not greater than 2 The ordered tree of , It's the simplest and most important tree . The recursion of binary tree is defined as : A binary tree is an empty tree , Or a tree made up of a root node and two disjoint trees , They are called non empty trees composed of left and right subtrees of roots respectively ; The left subtree and the right subtree are also binary trees .
1、 Full binary tree : If a binary tree has only a degree of 0 The node and degree of are 2 The node of , And the degree is 0 The nodes of are on the same layer , Then this binary tree is a full binary tree [4] .
2、 Perfect binary tree : Depth is k, Yes n A binary tree of nodes if and only if each node has a depth of k From the full binary tree of 1 To n When the nodes of are one-to-one corresponding , It's called a complete binary tree [4] .
The characteristic of complete binary tree is that leaf nodes can only appear on the two largest layers of sequence , And the maximum sequence of the descendants of the left branch of a node is equal to or larger than that of the descendants of the right branch 1 [4] .
For a complete binary tree, the following properties are satisfied , Example :
2 Heap structure
1. Heap structure is a complete binary tree structure realized by array
2. In a complete binary tree, if the maximum value of each subtree is at the top, it is a large root heap
3. In a complete binary tree, if the minimum value of each subtree is at the top, it is the small root heap
4. Reactor structure heapInsert And heapify operation
① How to adjust an array to a large root heap
heapinsert principle —— Compare with the parent node every time
Add the numbers in the array to the heap in turn . For each number added to the heap , Compare it with the value of its parent node , If he's older , Then the values of the parent node and the parent node are exchanged . His index position is i , Then the index position of the parent node is (i-1)/2 .
notes :-1/2 As the result of the 0, therefore index=0 Don't be afraid when you are
#include<iostream>
#include<vector>
using namespace std;
void heapinsert(vector<int>&nums){
if(!nums.size()) return;
int temp=0, index=0;
for(int i=0; i<nums.size(); i++){
index=i;
// -1/2 As the result of the 0, therefore index=0 Don't be afraid when you are
// Constantly compare with the parent node
while(nums[index]>nums[(index-1)/2]){
temp=nums[index];
nums[index]=nums[(index-1)/2];
nums[(index-1)/2]=temp;
index=(index-1)/2;
}
}
for(auto i:nums)
cout<<i<<endl;
}
int main(){
vector<int>nums={1,2,3,4,5,6,7,8,9};
heapinsert(nums);
return 0;
}
②heapify The process —— Compare with child nodes every time
A value in the heap has changed , How to adjust .
For example, in the large root heap , The index location is i The value of the element of has changed ( smaller ), Compare it with the left and right children , Exchange them with older children . The index position of the left child is 2*i+1 , The index position of the right child is 2*i+2.
#include<iostream>
#include<vector>
using namespace std;
void heapify(vector<int>&nums){
if(!nums.size()) return;
int largest=0, i=0, temp=0;
for(int index=0; index<nums.size(); index++){
i=index;
while((2*i+1)<nums.size()){
// First judge whether the right child has crossed the boundary , Choose the older of the two children , Store the index position of the larger one in largest in
largest=(2*i+2)<nums.size()?
nums[2*i+1]>nums[2*i+2]?2*i+1:2*i+2
:2*i+1;
// explain nums[i] Can't move down any more
if(nums[i]>=nums[largest]) break;
// Exchange the values of two elements
temp=nums[i];
nums[i]=nums[largest];
nums[largest]=temp;
// Now it's time to continue the downward comparison
i=largest;
}
}
for(auto i:nums)
cout<<i<<endl;
}
int main(){
vector<int>nums={1,9,8,7,6,5,4,3,2};
heapify(nums);
return 0;
}
5. Increase and decrease of heap structure
6. Priority queue structure , Introduce the heap structure
3 Heap sort
1. Let's make the whole array into a big root heap structure , The process of building a heap :
① From top to bottom , The time complexity is 0(N*logN)
② The bottom-up approach , The time complexity is 0(N)
2. Swap the maximum value of the heap with the value at the end of the heap , And then reduce the size of the heap , Then adjust the stack , It's been going on and on , The time complexity is O(N*logN)
3. The size of the heap is reduced to 0 after , Sort complete
4 Heap sort extension problem
We know an almost ordered array , Almost orderly means , If you put the array in order , Each element can move no more than k, also k Smaller than array . Please select an appropriate sorting algorithm to sort the data .
Their thinking :
Prerequisite : Given an array arr, as well as k=6
① First, traverse the first seven numbers of the array , Put it in the small root pile
② The minimum value must be in the first position after sorting , Set the minimum value of the small root heap eject ( Remove from the small root heap and place in 0 Location )
③ Then put the next data into the small root heap , The next steps are the same as ②
④ Go round and round until the end of the array , Pop up the data of the small root heap in turn
边栏推荐
- js用switch语句根据1-7输出对应英文星期几
- 突破从0到1后,鲜花电商2.0时代怎么走?
- JS use the switch statement to output the corresponding English day of the week according to 1-7
- What is the difference between volatile and synchronized?
- R language consumption behavior statistics based on association rules and cluster analysis
- MSSQL how to export and delete multi table data using statements
- Hutool symmetric encryption
- JS uses the while cycle to calculate how many years it will take to grow from 1000 yuan to 5000 yuan if the interest rate for many years of investment is 5%
- JS to print prime numbers between 1-100 and calculate the total number of optimized versions
- R 中的 RNA-Seq 数据分析 - 调查数据中的差异表达基因!
猜你喜欢
「短视频」临夏消防救援支队开展消防安全培训授课
【11. 二维差分】
Goodbye, agile Scrum
【批处理DOS-CMD命令-汇总和小结】-批处理命令中的参数%0、%1、%2、%[0-9]、%0-9和批处理命令参数位置切换命令shift,dos命令中操作符%用法
js输出1-100之间所有的质数并求总个数
磁选机是什么?
Cookie encryption 7 fidder analysis phase
Testing network connectivity with the blackbox exporter
Origin of forward slash and backslash
认识O(NlogN)的排序
随机推荐
JS example print the number and sum of multiples of all 7 between 1-100
How can the flower e-commerce 2.0 era go after the breakthrough from 0 to 1?
Mysql-8 download, installation and configuration tutorial under Windows
Speech signal feature extraction process: input speech signal - framing, pre emphasis, windowing, fft- > STFT spectrum (including amplitude and phase) - square the complex number - > amplitude spectru
ggplot2的自定义调色板
野风药业IPO被终止:曾拟募资5.4亿 实控人俞蘠曾进行P2P投资
JS use switch to output whether the result is qualified
Bean copy details
Set the address book function to database maintenance, and add user name and password
js例题打印1-100之间所有7的倍数的个数及总和
JS use the switch statement to output the corresponding English day of the week according to 1-7
Speech signal processing - concept (4): Fourier transform, short-time Fourier transform, wavelet transform
JDBC reads MySQL data list
Common operation and Principle Exploration of stream
Futures reverse Documentary - training for traders
Multi table associated query -- 07 -- hash join
Origin of forward slash and backslash
Idea method template
期货反向跟单—交易员的培训问题
js来打印1-100间的质数并求总个数优化版