MindMap Gallery [Study Notes] Data Structure-Tree
Some knowledge about trees in data structures, including Huffman trees, tree table search, clue binary trees, binary tree traversal methods, and minimum spanning trees. Everyone is welcome to learn.
Edited at 2023-07-05 11:27:01One Hundred Years of Solitude is the masterpiece of Gabriel Garcia Marquez. Reading this book begins with making sense of the characters' relationships, which are centered on the Buendía family and tells the story of the family's prosperity and decline, internal relationships and political struggles, self-mixing and rebirth over the course of a hundred years.
One Hundred Years of Solitude is the masterpiece of Gabriel Garcia Marquez. Reading this book begins with making sense of the characters' relationships, which are centered on the Buendía family and tells the story of the family's prosperity and decline, internal relationships and political struggles, self-mixing and rebirth over the course of a hundred years.
Project management is the process of applying specialized knowledge, skills, tools, and methods to project activities so that the project can achieve or exceed the set needs and expectations within the constraints of limited resources. This diagram provides a comprehensive overview of the 8 components of the project management process and can be used as a generic template for direct application.
One Hundred Years of Solitude is the masterpiece of Gabriel Garcia Marquez. Reading this book begins with making sense of the characters' relationships, which are centered on the Buendía family and tells the story of the family's prosperity and decline, internal relationships and political struggles, self-mixing and rebirth over the course of a hundred years.
One Hundred Years of Solitude is the masterpiece of Gabriel Garcia Marquez. Reading this book begins with making sense of the characters' relationships, which are centered on the Buendía family and tells the story of the family's prosperity and decline, internal relationships and political struggles, self-mixing and rebirth over the course of a hundred years.
Project management is the process of applying specialized knowledge, skills, tools, and methods to project activities so that the project can achieve or exceed the set needs and expectations within the constraints of limited resources. This diagram provides a comprehensive overview of the 8 components of the project management process and can be used as a generic template for direct application.
Tree
Huffman tree
Weighted path length WPL
Assume that a binary tree has n weighted leaf nodes, then the sum of the path length from the root node to each leaf node and the product of the corresponding node weight is called the weighted path length WPL (weighted Path Length) of the binary tree. .
The same leaf nodes construct different binary trees
The leaf nodes with larger weights are closer to the root and the smaller the value of wpl is.
definition
The binary tree with the minimum weighted path length is called the Huffman tree (also called the optimal tree).
How to construct?
in principle
The leaf nodes with larger weights are closer to the root node.
The smaller the weight, the farther the leaf node is from the root node.
Find the small value (the synthesized nodes must also be compared together)
process
There are n0 leaf nodes with weights
application
Huffman coding (prefix coding)
Characteristics of Huffman coding: The larger the weight, the shorter the character code, and vice versa.
In the Huffman encoding of a set of characters, it is impossible for the Huffman encoding of one character to be the prefix of the Huffman encoding of another character.
application
Character Encoding
machine instructions
Tree table lookup
Binary sorting tree
left<root<right
Balanced Binary Tree AVL
An improved version of the binary sorting tree (a special binary sorting tree), try to keep the tree as short as possible!
How to tell if a tree is growing normally?
Balance factor = left subtree height - right subtree height
Binary sorting tree with balance factor <= 1
Balance the maximum depth and minimum number of nodes of a binary tree
B-tree (an extension of balanced binary tree)
insert
Creating a B-tree is a process of continuous insertion operations
clue binary tree
The conditions for the left empty chain field: it is at the leftmost end of the sequence, and the original left pointer is empty.
The conditions for the right empty chain domain: it is at the right end of the sequence, and the original right pointer is empty.
Binary tree traversal method
for root node
prologue
root-left-right
mid-order
left-root-right
Afterword
left-right-root
level
Traverse line by line
minimum spanning tree
Prim's algorithm
Require
The weight is the smallest and cannot form a cycle.
For questions with few nodes
The core steps of the Prime algorithm are: in a weighted connected graph, V is a set containing all vertices. U is already a node in the minimum spanning tree, starting from any vertex v in the graph. At this time, the set U={v}, Repeat the following operations: find an edge with the smallest weight among all edges (u,w)∈E of all u∈U,w∈V-U, and add the edge (u,w) to the set of found edges. And add point w to the set U. When U=V, the minimum spanning tree is found.
Kruskal algorithm
First determine the nodes and select the ones with the shortest paths in order.
Cannot form a ring
For questions with a small number of sides
Basic idea: edge-led. In a weighted connected graph, U is the set of all edges, N is the number of vertices, and let SU be the set of edges that are already on the minimum spanning tree. Repeat the following operations: each time select an edge e∈U-SU with the smallest weight and an edge that does not form a cycle with the edges in SU, and add it to SU. When the number of edges in SU is equal to N-1, the minimum spanning tree is found.
question
If a node is not a leaf node, then it will have children, and these children are a group of brothers.
The number of nodes without a right pointer is: root node number of non-leaf nodes
Exclude symmetrical ones, count the number, and make sure it is always less on the left and more on the right or more on the left and less on the right.
Tree
ASL average search length
related to the height of the tree
Chapter 5 Trees and Binary Trees
Binary tree traversal and Huffman tree
Difficulty: Conversion between binary trees and trees and forests
Most of the tests are in the form of multiple-choice questions, which also involve tree traversal and Huffman tree algorithms.
Tree
finite set of n nodes
The root node is unique
There is no limit to the number of subtrees, but they do not intersect with each other.
Degree: the number of subtrees owned by the node
Binary tree: degree <=2
nature
There are at most 2^i-1 nodes on the i-th level of the binary tree.
If the depth in the binary tree is k, then there are at most 2^k-1 nodes. (k>=1)
n0=n2 1, n0 represents the number of nodes with degree 0, and n2 represents the number of nodes with degree 2.
In a complete binary tree, the depth of a complete binary tree with n nodes is [log2n] 1, where [log2n] is rounded down
[Binary tree] Node calculation formula N = n0 n1 n2
Total number of nodes = total degree 1=Oxn0 1xn1 2xn2 ..
full binary tree
All nodes have left and right subtrees, and all leaf nodes are at the same level.
complete binary tree
Leaf nodes can only appear on the lowest level and the next lower level.
And the nodes of the bottom layer are concentrated in the binary tree at the leftmost positions of the layer.
The traversal correspondence between trees, forests, and binary trees
heap
definition
Must be a complete binary tree
A complete binary tree only allows the last row to be less than full. And the last row must be sorted from left to right. There must be no space between elements in the last row
Heap orderliness
dagendui
small root pile
Basic operations
Heap storage
1. Traverse the number according to the layer sequence, represented by a one-dimensional array
The parent node is i, the subscript of the left child node is 2i 1, and the right child node is 2i 2 (this rule is often used in algorithms)
Upper filter
Only the last element of the tree violates heap ordering
Mainly used to insert new elements into the heap
Complexity: O(logN)
Filter down
Only the root node violates heap ordering
Adjust the root node downwards
Complexity: O(logN)
Build a pile
How to convert an unordered array into a heap?
top down
Insert into heap, filter on
Complexity: O(NlogN)
bottom up
First adjust the elements into a heap and filter down. Starting from the second to last row, perform the filter down operation on the parent node.
Complexity: O(N)
Application: priority queue
two operations
insert queue
pop up minimal element
Can be implemented using a small root heap
Because the root node of the small root heap is min, after popping up, the remaining elements are adjusted into a heap (put the last element at the root node and filter down)
Complexity: O(logN)
Heap sort: Pop elements from the priority queue in sequence
Complexity: O(NlogN)