一.概念
1.要记得:
①.节点的度:一个节点含有子树的个数
②.叶节点/终端节点:度为0的节点
③.树的高度/深度:树的层次有多少次
2.了解的:
①.非终端节点/分支节点:度不为0的节点
②双亲节点/父节点:一个节点的前驱称为该节点的双亲节点/父节点
③.兄弟节点:同一个父节点的节点
④.树的度:所有节点中度最大的节点的度
⑤.节点的层次
⑥.堂兄弟节点:双亲在同一层的节点
⑦.节点的祖先:该节点的所有前驱节点都是该节点的祖先
⑧.节点的子孙结点
⑨森林:多棵树
二.二叉树
1.二叉树:每个节点度不大于2的树
typedef char BTDataType;
typedef struct BinaryTreeNode
{
BTDataType _data;
struct BinaryTreeNode * _left;
struct BinaryTreeNode * _right; } BTNode;
Node* creatBinaryTree ( DataType arr[ ] , int * ind) {
if ( arr[ * idx] == '#' ) {
( * idx) ++ ;
return NULL ;
}
NOde* root = ( Node* ) malloc ( sizeof ( Node) ) ;
root-> data = arr[ * idx] ;
( * idx) ++ ;
root-> left = creatBinaryTree ( arr, idx) ;
root-> right = creatBinaryTree ( arr, idx) ;
return root;
void BinaryTreeDestory ( Node* * root) {
if ( * root) {
BinaryTreeDestory ( & ( ( * root) -> left) ) ;
BinaryTreeDestory ( & ( ( * root) -> right) ) ;
free ( * root) ;
* root = NULL ;
}
}
int BinaryTreeSize ( Node* root) {
if ( root == NULL )
return 0 ;
return BinaryTreeSize ( root-> left) + BinaryTreeSize ( root-> right) + 1 ;
int bTreeHeight ( Node* rooot) {
if ( root == NULL )
return 0 ;
int left = bTreeHeight ( root-> left) ;
int right = bTreeHeight ( root-> right) ;
return left > right ? left + 1 : right + 1 ;
int BinaryTreeLeafSize ( Node* root) {
if ( root == NULL )
return 0 ;
if ( root-> left == NULL && root-> right == NULL )
return 1 ;
return BinaryTreeLeafSize ( root-> left) + BinaryTreeLeafSize ( root-> right) ;
int BinaryTreeLevelKSize ( Node* root, int k) {
if ( root == NULL )
return 0 ;
if ( k == 1 )
return 1 ;
return BinaryTreeLevelKSize ( root-> left, k - 1 ) + BinaryTreeLevelKSize ( root-> right, k - 1 ) ;
BTNode* BinaryTreeFind ( Node* root, BTDataType x) {
if ( root) {
Node* node;
if ( root-> data == x)
return root;
node = BinaryTreeFind ( root-> left, x) ;
if ( node)
return node;
else
return BinaryTreeFind ( root-> right, x) ;
}
else
return NULL ;
}
void preOrder ( Node* root) {
if ( root) {
printf ( "%c" , root-> data) ;
preOrder ( root-> left) ;
preOrder ( root-> right) ;
}
)
void inOrder ( Node* root) {
if ( root) {
inOrder ( root-> left) ;
printf ( "%c" , root-> data) ;
inOrder ( root-> right) ;
}
)
void postOrder ( Node* root) {
if ( root) {
postOrder ( root-> left) ;
postOrder ( root-> right) ;
printf ( "%c" , root-> data) ;
}
)
void BinaryTreeLevelOrder ( BTNode* root) ;
int BinaryTreeComplete ( BTNode* root) ;
①二叉链,三叉链
struct BinaryTreeNode
{
struct BinTreeNode * _pLeft;
struct BinTreeNode * _pRight;
BTDataType _data;
}
struct BinaryTreeNode
{
struct BinTreeNode * _pParent;
struct BinTreeNode * _pLeft;
struct BinTreeNode * _pRight;
BTDataType _data;
}
②.二叉树的分类
- 满二叉树:每个节点的度都为2的二叉树
N0 = N2 + 1
- 完全二叉树:除最后一个节点外,度都为2的二叉树(最后一层的节点必须是从左到右连续的)
2.堆(最大堆/最小堆才是堆):将所有元素按完全二叉树的顺序存储
最小堆:根为最小值(子树的根也是最小值) 最大堆:根为最大值(子树的根也是最大值) 堆顶:整个树的根
typedef struct Heap
{
HPDataType* _a;
int _size;
int _capacity;
} Heap;
void HeapCreate ( Heap* hp, HPDataType* a, int n) ;
void HeapDestory ( Heap* hp) ;
void HeapPush ( Heap* hp, HPDataType x) ;
void HeapPop ( Heap* hp) ;
HPDataType HeapTop ( Heap* hp) ;
int HeapSize ( Heap* hp) ;
int HeapEmpty ( Heap* hp) ;
void HeapSort ( int * a, int n) ;
3.栈
4.遍历方式
①前序遍历-----先根节点,再左子树,再右子树(ABDCEF)
void preOrder ( Node* root) {
if ( root) {
printf ( "%c" , root-> data) ;
preOrder ( root-> left) ;
preOrder ( root-> right) ;
}
)
②中序遍历-----先左子树,再根,再右子树(DBAECF)
void inOrder ( Node* root) {
if ( root) {
inOrder ( root-> left) ;
printf ( "%c" , root-> data) ;
inOrder ( root-> right) ;
}
)
③后序遍历-----先左子树,再右子树,再根节点(DBEFCA)
void postOrder ( Node* root) {
if ( root) {
postOrder ( root-> left) ;
postOrder ( root-> right) ;
printf ( "%c" , root-> data) ;
}
)
层序遍历-----从上到下,从左往右依次遍历