Basic properties and storage structure of binary tree

The following part of the picture refers to the relevant course content of "Data Structure" by Lazy Cat Teacher, as a study note

Table of contents

Features of a binary tree:

Special binary tree:

(1) Inclined tree

(2) Full binary tree

(3) Complete binary tree

 Summary of the basic properties of binary trees:

The storage structure of the binary tree:

(1) Binary tree sequential storage

(2) Binary linked list

(3) Three-fork linked list


Features of a binary tree:

(1) Each node has at most two subtrees;

(2) The binary tree is ordered, and its order cannot be reversed arbitrarily;

Special binary tree:

(1) Inclined tree

 

1. A binary tree whose nodes have only left subtrees is called a left oblique tree;
2. A binary tree whose nodes only have right subtrees is called a right oblique tree;

Features:

1. In an oblique tree , each layer has only one node
. 2. The number of nodes in an oblique tree is the same as its depth .

(2) Full binary tree

 In a binary tree, if all branch nodes have left subtrees and right subtrees, and all leaves are on the same level.

A full binary tree has the largest number of nodes in a binary tree of the same depth ; the largest number of leaf nodes in a binary tree of the same depth 

Features:

1, leaves can only appear in the bottom layer
2, only nodes with degree 0 and degree 2

(3) Complete binary tree

The node number of a complete binary tree is exactly the same as the number of a full binary tree of the same depth at the same position; it can also be understood that in a full binary tree, starting from the last node, any number of nodes are removed continuously, that is, a complete tree binary tree.

 Features:

1. Leaf nodes can only appear in the bottom two layers and the leaf nodes in the bottom layer are all concentrated on the left side of the binary tree ;
2. If there is a node with degree 1 in a complete binary tree, there can only be one, and the node Only left child .
3. A complete binary tree with a depth of k must be a full binary tree on the k-1 level.

4. In the binary tree with the same number of nodes, the depth of the complete binary tree is the smallest

 Summary of the basic properties of binary trees:

(Properties 4 and 5 are for complete binary trees)

The storage structure of the binary tree:

(1) Binary tree sequential storage

That is, an array is used for storage, and it is stored according to the numbering method of a complete binary tree. If there is no number, no value will be assigned. As shown in the figure below, the numbering method of the binary tree below:

But when the binary tree is a slanted tree, it will still be stored according to the number of the complete binary tree, which will cause a lot of waste of storage space, so the sequential storage of the binary tree is generally only used to store the complete binary tree .

(2) Binary linked list

Each node of the binary tree corresponds to each node of the linked list, the data field stores the content of the binary tree node, and the two pointer fields point to the left and right children respectively

In a binary linked list with n nodes, there are n+1 null pointers

(It can be understood that, except the root node, every other node is connected to an edge, a total of n-1 edges, one edge occupies a pointer field, there are 2n pointer fields in total, and the empty pointer field is 2n- (n-1)=n+1)

data structure:

typedef struct binarytree{
	Datatype data;//数据内容
	struct binarytree* Lchild;//指向左孩子结点
	struct binarytree* rchild;//指向右孩子结点
}binarytree;

But you will find that if you want to visit the parents of a node, how can you use a binary linked list? It is found that the binary linked list cannot directly access the parents through the node, so the triangular linked list is introduced here

(3) Three-fork linked list

3.1 Linked list form

On the basis of the binary linked list, a pointer field pointing to the parent is added

 data structure:

typedef struct binarytree1{
	Datatype data;//数据内容
	struct binarytree1* Lchild;//指向左孩子结点
	struct binarytree1* rchild;//指向右孩子结点
	struct binarytree1* parent;//指向双亲结点
}binarytree1;

 3.2 Static linked list (array)

Both left and right children and parent nodes store their subscripts in the array. This numbering method is directly sequentially subscripted

As shown below:

 data structure:

typedef struct binarytree2{
	Datatype data;//数据内容
	int Lchild;//左孩子下标
	int rchild;//右孩子下标
	int parent;//双亲下标
}binarytree2;

Beginner Xiaobai, welcome to correct me if I make mistakes! ~

Guess you like

Origin blog.csdn.net/m0_63223213/article/details/125789141