Tree - "Data Structures and Algorithms"

Dear uu from CSDN, it’s been a long time. I haven’t updated my data structure and algorithm column for a long time. Now, I’m going to start to regain the knowledge I left behind. This time, Xiao Yalan will introduce a brand new data structure to uu. , Next, let us enter the world of trees! ! !


Tree Concept and Structure

Binary tree concept and structure

Sequential Structure and Realization of Binary Tree


Tree Concept and Structure

tree concept

A tree is a non-linear 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.

 

 


 Tree related concepts

 

Concept: tree + description of human kinship 

Degree of node: The number of subtrees contained in a node is called the degree of the node; as shown in the figure above: A is 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, H, I... and other nodes 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 node or parent node: If a node contains child nodes, this node is called the parent node of its child nodes; as shown above: 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 above: B is the child node of A  

Brother nodes: nodes with the same parent node are called brother nodes; as shown above: B and C are brother nodes  

Degree of the tree: In a tree, the degree of the largest node is called the degree of the tree; as shown above: the degree of the tree is 6  

The level of nodes: starting from the definition of the root, the root is the first level, the child nodes of the root are the second level, and so on; 

Tree height or depth: the maximum level of nodes in the tree; as shown above: the height of the tree is 4  

 

 

  

Cousin nodes: Nodes whose parents are on the same layer are cousins; as shown in the figure above: H and I are sibling nodes  

 Ancestors of a node: all nodes on the branch from the root to the node; as shown in the figure above: A is the ancestor of all nodes

Descendants: Any node in the subtree rooted at a node is called a descendant of the node. As shown above: all nodes are descendants of A  

 Forest: A collection of m (m>0) disjoint trees is called a forest;

 And check set is a forest


There is a special node, called the root node, the root node has no predecessor node (no parent node)

Except the root node, other nodes are divided into M (M>0) disjoint sets T1, T2, ..., Tm, where each set Ti (1 is a subtree similar in structure to a tree. Each The root node of a subtree has one and only one predecessor, and can have 0 or more successors

Therefore, the tree is defined recursively.

If it becomes such a structure, it is not a tree, but a graph! ! !

 

tree representation 

struct TreeNode
{
	int data;
	struct TreeNode* child1;
	struct TreeNode* child2;
	//......
};

 1. If the degree of the tree is specified, then a specific number of children can be defined

 2. The sequence table stores children

struct TreeNode
{
	int data;
	SeqList childArr;//struct TreeNode*
	//......
};

3. Parent notation (each location only stores the pointer or subscript of the parent)

struct TreeNode
{
	int data;
	int parent;
};

 

4. Left child right brother notation (simplified tree structure definition)

struct TreeNode
{
	struct TreeNode* firstchild1;//第一个孩子的结点
	struct TreeNode* pNextBrother;//指向其下一个兄弟结点
	int data;//结点的数据域
};

 

 

Windows Filesystem: Forest 

 

The use of trees in practice (representing the directory tree structure of the file system)

 


 Binary tree concept and structure

concept

A binary tree is a finite set of nodes that:

1. or empty

2. It consists of a root node plus two binary trees called left subtree and right subtree

As can be seen from the figure above:

1. There is no node with degree greater than 2 in the binary tree

2. The subtrees of the binary tree are divided into left and right, and the order cannot be reversed, so the binary tree is an ordered tree

Note: For any binary tree, it is composed of the following situations:

 

A real binary tree:

 

  

Special binary tree:

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 , then it is a full binary tree.

Every floor is full

 

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.

 

 

  


Sequential Structure and Realization of Binary Tree

Sequential structure of binary tree

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

The heap is always a complete binary tree.

Large root heap: any parent of the tree is greater than or equal to the child

Small root pile: any parent of the tree is less than or equal to the child

 

 

1. The following keyword sequence is a heap: ( A )

A 100,60,70,50,32,65

B 60,70,65,50,32,100

C 65,100,70,32,50,60

D 70,65,100,32,50,60

E 32,50,100,70,65,60

F 50,100,70,65,60,32

Heap application:

  • Heap sort - O(N*logN)
  • TopK
  • priority queue

 


 Alright, this is the end of Xiao Yalan's blog content today. Use code to realize heap and heap sorting. Please look forward to Xiao Yalan's future blog, and keep going! ! !

 

Guess you like

Origin blog.csdn.net/weixin_74957752/article/details/131148078