[Data structure-tree and binary tree]
table of Contents:
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
(2) Basic terminology
(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.
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)
Second, the nature and storage structure of the binary tree
(1) The nature of the binary tree
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)
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.
(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
#define MAXTSIZE 100 //二叉树的最大结点数
typedef TElemType SqBiTree[MAXTSIZE];//0号单元存储根结点
SqBiTree bt;
2. Chain storage structure
(1) Binary linked list
typedef struct BiNode
{
TElemType data; //数据域
struct BiNode* lchild, * rchild; //左右孩子指针
}BiNode,*BiTree; //二叉树结点
BiTree root;
In a binary linked list of n nodes, there are n+1 null pointer fields
(2) Three-pronged linked list
typedef struct TriTNode
{
TElemType data; //数据域
struct TriTNode* lchild,* parent * rchild; //左右孩子指针
}TriTNode,*TriTree; //二叉树结点