Binary Tree and storage structure

First, the definition of binary tree

Binary tree T: a finite set of nodes.

  • This set can be empty
  • If it is empty, it is by the root and known for its left subtree \ (T_L \) and right subtree \ (T_R \) is composed of two disjoint binary trees.

Specific five basic binary form

The binary subtrees left and right of the order

Second, special binary tree

2.1 oblique binary tree (Skewed Binary Tree)

Binary obliquely as shown below:

2.2 perfect binary tree (Perfect Binary Tree)

Perfect binary tree is also known as full binary tree (Full Binary Tree)

Perfect binary tree as shown below:

2.3 complete binary tree (Complete Binary Tree)

There binary tree of n nodes, the nodes of the tree are numbered from top to bottom, left to right order, numbered \ (i (1 \ leq { i} \ leq {n}) \) node and full binary numbers same as the position i in the binary tree nodes.

Complete binary tree as shown below:

As shown below, i.e., the binary tree is not a complete binary tree:

Third, several important properties of the binary tree

  • A binary tree maximum junction layer i is: \ (^ {i-2}. 1, \ Quad {i} \ GEQ 1 \)
  • A binary tree of depth k is the total number of maximum junction: \ (2 ^ k -1, \ Quad k \ GEQ. 1} {\)

  • 对任何非空二叉树T,若\(n_0\)表示叶节点的个数、\(n_2\)是度为2的非叶结点个数,那么两者满足关系\(n_0=n_2+1\)

四、二叉树的抽象数据类型定义

类型名称:二叉树

数据对象集:一个有穷的结点集合。若不为空,则由根节点和其左、右二叉子树组成。

操作集:\(BT\in{BinTree},\quad Item \in ElementType\),其重要操作有:

  1. BooleanIsEmpty(BinTree BT):判断BT是否为空;
  2. void Traversal(BinTree BT):遍历,按某顺序访问每个结点;
  3. BinTree CreateBinTree():创建一个二叉树。

4.1 常用的遍历方法

接下来介绍几个常用的遍历方法,未来会详细讲解

void PreOrderTraversal(BinTree BT):先序 --》根、左子树、右子树;

void InOrderTraversal(BinTree BT):中序 --》左子树、根、右子树;

void PostOrderTraversal(BinTree BT):后序 --》左子树、右子树、根;

void LevelOrderTraversal(BinTree BT):层次遍历,从上到下、从左到右

五、二叉树的存储结构

5.1 顺序存储结构

完全二叉树:按从上至下、从左到右顺序存储,n个结点的完全二叉树的结点父子关系

  • 非根节点(序号i>1)的父结点的序号是\([i/2]\)
  • 结点(序号为i)的左孩子结点的序号是2i(若\(2i\leq{n}\),否则没有左孩子);
  • 结点(序号为i)的右孩子结点的序号是2i+1(若\(2i+1\leq{n}\),否则没有右孩子)

一般二叉树也可以采用这种结构,但会造成空间浪费……

5.2 链表存储

/* c语言实现 */

typedef struct TreeNode *BinTree;
typedef BinTree Position;
struct TreeNode{
  ElementType Data;
  BinTree Left;
  BinTree Right;
}

单个子节点用下图所示链表表示:

使用链表对整棵树的表示如下:

Guess you like

Origin www.cnblogs.com/nickchen121/p/11499145.html