DS binary tree basics

Preface

We haven't updated the data structure blog post for a long time. Today we will update the tree issue! In previous issues, we have introduced basic linear data structures such as sequence lists, linked lists, stacks and queues and implemented them respectively. In this issue, we will introduce a very important non-linear basic structure - tree structure. .

Introduction to the contents of this issue

Tree concept and structure

Concept and structure of binary tree

1. Concept and structure of tree

tree concept

Tree is anon linear (logically non-continuous) data structure, which is a set of hierarchical relationships composed of n (n >= 0) limited nodes! It is called a tree because its logical structure is similar to an upside down tree!

important point:

(1) There is a special node in the tree calledthe root node. The root node has no predecessor node

(2) Except for the root node, the remaining nodes are divided into M (M > 0) mutually disjoint sets (That is, subtrees cannot intersect )! Each set is a subtree with a structure similar to that of a tree! The root node of each subtree has one and only one predecessor, and can have 0 or more successors!

So in summary, trees are more suitable to be defined recursively!

OK, draw a picture to understand:

Notice:

In the tree structure, subtrees cannot intersect with each other. If they intersect, it will be the graph we will introduce later!

Except for the root node, all other nodes have one and only one parent node.

A tree with N nodes has N-1 edges.

tree related concepts

Degree of node: The number of subtrees contained by a node is called the node degree;

leaf node (terminal node): degree is The node 0 is called a leaf node;

Non-terminal point (minusional point): degrees Unsatisfactory point;

Father node (Parent node): If a node contains child nodes, this node is called The parent node of its child node;

Child node (child node): The root node of the subtree contained by a node is called the child node of the node;

Brother nodes:Nodes with the same parent node are called each other’s heart nodes;

The degree of the tree: In a tree, the degree of the maximum node is called the degree of the tree Spend.

Level of node: Define the first level starting from the root node, the child nodes of the root are the second level, and so on!

The height of the tree (depth): the maximum level of the node in the tree;

Cousin nodes:Nodes whose parents are on the same level are called cousin nodes;

Ancestors of node:All nodes on the branches from the root to the node;

Descendants: Any node in the subtree rooted at a node is called a descendant of that node;

Forest: It is called a set of m (m>0) disjoint trees for the forest;


tree representation

The tree structure is more complicated than the linear structure, and it is more troublesome to store! It is necessary to store both the value range and the relationship between nodes! There are many ways to represent trees, such as: parent representation, child representation, child parent representation, child parent representation!

Here is a brief introduction, and the important ones will be introduced later, such as parent representation (union search set) and child representation (graph adjacency list)!

Parent representation is: use an array to store each node, and store the subscript of their parent node in each node!

Child representation: Use a sequential table and a linked list for storage. The sequential table stores each node, and each node stores the head pointer of the first child

In fact, these are not optimal structures. The optimal structure is the one I am going to introduce below - the child brother representation!

Child brother notation: has two pointer fields and one data field. One is the child pointer field and the other is the sibling pointer field! As long as it is a child of the current node, it will be hung behind the child node. If it is a sibling node, it will be hung behind the sibling node. In this way, the non-binary tree can be converted into a binary tree for processing!

typedef char DataType;
struct Node
{
	struct Node* _LeftChild;
	struct Node* _RightBother;
	DataType _data;
};

Real-life applications of tree structures

In fact, the tree structure is not widely used in reality. The most typical one is the directory structure of the file system!

This is the file directory system structure under Linux, it is a multi-tree!

2. Concept and structure of binary tree

Binary tree concept

two-fork tree:degreemosttwo-fork tree, Titled bifurcated tree!

Note: Here is at most, which means the degree can be 0, 1, or 2!

Binary tree is composed of a root node and left and right subtrees!

Binary trees are divided into left and right! The order cannot be reversed!

5 special cases of binary trees:

All binary trees are composed of the above 5!

special binary tree

Full Binary Tree: A binary tree, ifthe number of nodes in each level reaches the maximum value, Then the binary tree is called a full binary tree! (That is to say, if a binary tree has K levels and the total number of nodes is: 2^k - 1, it means that it is a full binary tree)

Complete binary tree: For a binary tree with depth K and number of nodes n, if and only if each node and depth are K Each number of that layer corresponds (that is to sayThe first K-1 layer is full, and the Kth layer is stored in sequence according to the serial number)< The tree of /span>A full binary tree is actually a special case of a complete binary tree is called a complete binary tree! That is to say

OK, draw a picture to understand:

Complete binary trees are very useful! The heap, heap sorting, and TopK problems we play later are all based on complete binary trees!

Properties of binary trees

(1) If the number of levels of the root node is 1, then a non-empty binary tree has at most 2^(i-1) nodes on the i-th level< /span>

(2) If the number of levels of the root node is 1, then the maximum number of nodes of a binary tree with height h of is 2^(h) - 1

(3) If the number of levels of the root node is 1, then the depth of a full binary tree with n nodes h = log2(N+1)

(4) For any binary tree, assume that the number of nodes (leaf nodes) with degree 0 is n0, and the number of nodes with degree 2 is n2 , then n0 = n2 + 1

(5)对于高为h的彉树的节Point范围Correct[2^(h-1), 2^(h)-1]

(6) For a complete binary tree with n nodes, if all nodes are numbered starting from 0 in the array order from top to bottom, left to right, then for the node with serial number i:

If i > 0, the parent subscript of the position node is: (i-1)/2, and the root node has no parents.

If 2i+1 < n, the subscript of the left child is 2i+1, otherwise if 2i+1 >= n, there is no left child

If 2i+1 < n, then the subscript of the right child is 2i+2, otherwise if 2i+2 >= n, there is no right child

Binary tree storage

The storage of binary trees is generally divided into: sequential storage and chain storage< a i=4>Two kinds~!

1. Sequential storage

Sequential storage is to use array to store , general binary trees are not suitable for array storage , because there will be a waste of space, only complete binary trees are suitable for storing in arrays! This is how the heap is made (will be introduced later)! Binary trees are stored in arrays (sequential storage). Physically it is an array, but logically it is a binary tree!

2. Chain storage

The linked storage of a binary tree means that a linked list is used to represent (storage) a binary tree, that is, a linked list is used to indicate the logical relationship of each node! Generally, each node has three fields, one data field and two pointer fields (left and right pointer fields), They point to the left and right nodes of the binary tree respectively. Chain storage can be divided into two-fork chains and three-fork chains. What we are playing here is a two-fork chain. The three-fork chain will be introduced later. He is playing with the red-black tree!

typedef char BTDataType;
typedef struct BinaryTreeNode
{
	BTDataType datal;
	struct BinaryTreeNode* left;
	struct BinaryTreeNode* right;
}BT;

OK, this is the introduction of the basic knowledge of binary trees in this issue. We will implement sequential storage and chained storage in the next issue!

Guess you like

Origin blog.csdn.net/m0_75256358/article/details/134383515