目录
基本术语
二叉树
两个特例:
满二叉树
完全二叉树
性质
存储结构
顺序存储
#include<stdio.h>
#define MAXSIZE 100
typedef int SqBiTree[MAXSIZE]; //binary tree 二叉树
SqBiTree bt;
int main(){
return 0;
}
适用于满二叉树和完全二叉树,否则存储密度好低
以及顺序存储结构自带的优缺点和适用条件
链式存储
双叉链表
#include<stdio.h>
typedef struct BiTree{
int data;
struct BiNode *lchild , *rchild;
}BiNode , *BiTree;
int main(){
return 0;
}
多个 *parent 就是三叉链表
遍历顺序
*很重要!
排序原理例题:
递归
代码:
#include<stdio.h>
typedef struct BiNode{
int data;
struct BiNode *lchild , *rchild;
}BiNode , *BiTree;
void Pre(BiTree T){
if(T != NULL){
printf("%d\t",T->data);
Pre(T->lchild);
Pre(T->rchild);
}
}
int main(){
return 0;
}
分析:
非递归
代码:
小蒟蒻手敲的:
emmm准确率不知道如何,只知道语法没错(doge……)
#include<stdio.h>
#include<stdbool.h>
#define MAXSIZE 100
typedef struct BiNode{
int data;
struct BiNode *lchild , *rchild;
}BiNode , *BiTree;
typedef struct SqStack{
BiNode** base;
BiNode** top;
int stacksize;
}SqStack;
void InitStack(SqStack &S){
S.base = new BiNode*[MAXSIZE];
S.top = S.base;
S.stacksize = MAXSIZE;
}
void Push(SqStack &S , BiNode* e){
if(S.top - S.base != S.stacksize){
*S.top++ = e;
}
}
void Pop(SqStack &S , BiNode* &e){
if(S.top != S.base){
e = *--S.top;
}
}
bool StackEmpty(SqStack S){
if(S.top == S.base) return true;
return false;
}
void InOrderTraverse(BiTree T){
BiTree p = T;
SqStack S;
InitStack(S);
while(p != 0 || StackEmpty(S) == false){
if(p != 0){
Push(S , p);
p = p->lchild;
}else{
BiTree q;
Pop(S , q);
printf("%d\n",q);
p = q->rchild;
}
}
}
int main(){
return 0;
}
(感觉如果在BiNode里加个 *parent 会方便很多,不用栈也可以回溯)
层次遍历
小蒟蒻手敲的:
emmm准确率不知道如何,只知道语法没错(doge……)
#include<stdio.h>
#define MAXSIZE 100
typedef struct BiNode{
int data;
struct BiNode *lchild , *rchild;
}BiNode , *BiTree;
typedef struct SqQueue{
BiNode** base;
int front;
int rear;
}SqQueue;
void InitQueue(SqQueue &Q){
Q.base = new BiNode*[MAXSIZE];
Q.front = Q.rear = 0;
}
void EnQueue(SqQueue &Q , BiNode* e){
Q.base[Q.rear] = e;
Q.rear = (Q.rear + 1) % MAXSIZE;
}
void OutQueue(SqQueue &Q , BiNode* &e){
e = Q.base[Q.front];
Q.front = (Q.front + 1) % MAXSIZE;
}
bool QueueEmpty(SqQueue Q){
if(Q.front == Q.rear) return true;
return false;
}
void LevelOrder(BiTree T){
BiNode* p = T;
SqQueue que;
InitQueue(que);
EnQueue(que , T);
while(QueueEmpty(que) != true){
OutQueue(que , p);
printf("%d\n",p->data);
if(p->lchild != NULL) EnQueue(que , p->lchild);
if(p->rchild != NULL) EnQueue(que , p->rchild);
}
}
int main(){
return 0;
}
常用操作
读入
typedef struct BiNode{
char data;
struct BiNode *lchild , *rchild;
}BiNode , *BiTree;
void CreatBiTree(BiTree &T){
char ch;
scanf("%c",&ch);
if(ch == '#'){
T = NULL;
}else{
T = new BiNode;
T->data = ch;
CreatBiTree(T->lchild);
CreatBiTree(T->rchild);
}
}
复制
typedef struct BiNode{
char data;
struct BiNode *lchild , *rchild;
}BiNode , *BiTree;
void Copy(BiTree T , BiTree &NewT){
if(T == NULL){
NewT = NULL;
}else{
NewT = new BiNode;
NewT->data = T->data;
Copy(T->lchild , NewT->lchild);
Copy(T->rchild , NewT->rchild);
}
}
计算深度
typedef struct BiNode{
char data;
struct BiNode *lchild , *rchild;
}BiNode , *BiTree;
int Depth(BiTree T){
if(T == NULL){
return 0;
}else{
int m = Depth(T->lchild);
int n = Depth(T->rchild);
if(m > n) return m+1;
return n+1;
}
}
计算结点总数
typedef struct BiNode{
char data;
struct BiNode *lchild , *rchild;
}BiNode , *BiTree;
int NodeCount(BiTree T){
if(T == NULL){
return 0;
}else{
return NodeCount(T->lchild) + NodeCount(T->rchild) + 1;
}
}
计算叶子结点数
typedef struct BiNode{
char data;
struct BiNode *lchild , *rchild;
}BiNode , *BiTree;
int LeafCount(BiTree T){
if(T==NULL){
return 0;
}else if(T->lchild == NULL && T->rchild == 0){
return 1;
}else{
return LeafCount(T->lchild) + LeafCount(T->rchild);
}
}
线索二叉树(Threaded Binary Tree)
结点的结构为
lchild | ltag | data | rtag | rchild |
tag = 0 指孩子 tag = 1 指地址
typedef struct BiThrNode{
int data;
int ltag , rtag;
struct BiThrNode *lchild , *rchild;
}BiThrNode , *BiThrTree;