[Data structure-tree and binary tree]

[Data structure-tree and binary tree]

1. The definition of tree and binary tree

(1) Definition of tree

A tree is a finite set of n nodes, it is either an empty tree (n=0); or a non-empty tree
Insert picture description here

(2) Basic terminology

Insert picture description here
Insert picture description here
Insert picture description here

(3) The definition of binary tree

1. Definition of Binary Tree

A binary tree is a collection of n (n>=0) nodes. It is either an empty tree (n=0) or a non-empty tree. For a non-empty tree: T
(1) There is one and only one called The node of the root;
(2) The nodes other than the root node are divided into two disjoint subsets T1 and T2, which are called the left subtree and the right subtree of T, and both T1 and T2 themselves It is a binary tree.

Insert picture description here

2. The basic characteristics of binary trees

(1) The degree of the node is less than or equal to 2;
(2) Ordered tree (subtrees are divided into left and right, and the order cannot be reversed arbitrarily)

Insert picture description here

Second, the nature and storage structure of the binary tree

(1) The nature of the binary tree

Insert picture description here
Full binary tree: A binary tree with a depth of K and 2 to the power of K-1 nodes. (Feature: each layer is full of nodes)
Insert picture description here

Complete binary tree: A binary tree with a depth of K and n nodes, if and only if each node corresponds to the node numbered from 1 to n in the full binary tree with a depth of K.

Insert picture description here

(2) The storage structure of the binary tree

1. Sequential storage structure

achieve: According to the node level number of the complete binary tree, the data elements in the binary tree are sequentially stored.
Features:
(1) The relationship between nodes is contained in its storage location;
(2) A waste of space, suitable for storing full binary trees and complete binary trees.

Node i:
Parent node: [i/2]
Left son: 2i
Right son: 2i + 1

Insert picture description hereInsert picture description here

#define MAXTSIZE 100   //二叉树的最大结点数
typedef TElemType SqBiTree[MAXTSIZE];//0号单元存储根结点
SqBiTree bt;

2. Chain storage structure

(1) Binary linked list

Insert picture description here

typedef struct BiNode
{
    
    
	TElemType data;   //数据域
	struct BiNode* lchild, * rchild;   //左右孩子指针
}BiNode,*BiTree;   //二叉树结点
BiTree root;

Insert picture description here
In a binary linked list of n nodes, there are n+1 null pointer fields

(2) Three-pronged linked list
Insert picture description here

typedef struct TriTNode
{
    
    
	TElemType data;   //数据域
	struct TriTNode* lchild,* parent * rchild;   //左右孩子指针
}TriTNode,*TriTree;   //二叉树结点

Insert picture description here

Guess you like

Origin blog.csdn.net/m0_46518461/article/details/109262296