1.二叉树的结构体:
typedef struct BiTNode {
int data;
struct BiTNode* lchild, * rchild;
}BiTNode,*BiTree;
2.使用遍历创建二叉树
void CreateBinTree(BiTree& t) { //t引用
int ch;
cin >> ch;
if (ch == -1) {
t = nullptr;
return;
}
else {
t = (BiTree)malloc(sizeof(BiTNode));
t->data = ch;
CreateBinTree(t->lchild);
CreateBinTree(t->rchild);
}
}
3.先根遍历,中根遍历,后根遍历,层次遍历
void PreOrder(BiTree t) { //先根遍历
if (t == nullptr)return;
cout << t->data;
PreOrder(t->lchild);
PreOrder(t->rchild);
}
void InOrder(BiTree t) { //中根遍历
if (t == nullptr)return;
InOrder(t->lchild);
cout << t->data;
InOrder(t->rchild);
}
void PostOrder(BiTree t) { //后根遍历
if (t == nullptr)return;
PostOrder(t->lchild);
PostOrder(t->rchild);
}
void LevelOrder(BiTree t) { //层次遍历
queue<BiTree>q; //需要调用stl里的queue
BiTree p = t;
if (p != nullptr)q.push(p);
while (!q.empty()) {
p=q.front();
q.pop();
cout << p->data;
if (p->lchild != nullptr) {
q.push(p->lchild);
}
if (p->rchild != nullptr) {
q.push(p->rchild);
}
}
}
4.复制二叉树
void Copy(BiTree t, BiTree& p) {
if (t == nullptr) {
p = nullptr;
return;
}
else {
p = (BiTree)malloc(sizeof(BiTNode));
p->data = t->data;
Copy(t->lchild, p->lchild);
Copy(t->rchild, p->rchild);
}
}
5.搜索父结点
BiTree Father(BiTree t, BiTree p) { //搜索父节点
BiTree q;
if (t==nullptr|| p == nullptr||p==t)return q = nullptr;
if (t->lchild == p || t->rchild == p)return q = t;
BiTree q0;
q0 = Father(t->lchild, p);
if (q0 != nullptr)return q = q0;
q0 = Father(t->rchild, p);
if (q0 != nullptr)return q = q0;
}
6.三叉链(拓展)(类似于二叉树)
typedef struct BiTPNode { //三叉链结构体
int data;
struct BiTPNode* parent, * lchild, * rchild;
}BiTPNode, *BiPTree;
void CreateBiTree(BiPTree& t) { //创建三叉链
int ch;
cin >> ch;
if (ch == -1) {
t = nullptr;
return;
}
else {
t = (BiPTree)malloc(sizeof(BiTPNode));
t->data = ch;
t->parent = nullptr;
CreateBiTree(t->lchild);
if (t->lchild)t->lchild->parent = t;
CreateBiTree(t->rchild);
if (t->rchild)t->rchild->parent = t;
}
}
7.计算二叉树的深度(递归)
int BiTreeDepth(BiTree t) {
if (t == nullptr)return -1;
return max(BiTreeDepth(t->lchild), BiTreeDepth(t->rchild)) + 1;
}
8.搜索数据
BiTree Find(BiTree t, int item) {
BiTree q,q0;
if (t == nullptr)return q = nullptr;
if (t->data == item)return q = t;
q0=Find(t->lchild, item);
if (q0 != nullptr)return q = q0;
q0 = Find(t->rchild,item);
if(q0!=nullptr)return q = q0;
}
9.插入结点作为某结点的左儿子
void InsertLeft(BiTree t,BiTree q,int item) {
if (t == nullptr)return;
BiTree p;
p=Find(t, item);
if (p == nullptr)return;
q->lchild = p->lchild;
p->lchild = q;
}
10.释放二叉树
void Del(BiTree t) {
if (t != nullptr) {
Del(t->lchild);
Del(t->rchild);
free(t);
}
}
11.删除给定结点及其左右子树
void DST(BiTree t,BiTree p) {
if (p == nullptr)return;
if (t == p) { //p结点是根结点时
Del(p);
t = nullptr;
return;
}
BiTree q = p,q0;
q0 = Father(t, q);
if (q0 != nullptr && q0->lchild == p)q0->lchild = nullptr;
if (q0 != nullptr && q0->rchild == p)q0->rchild = nullptr;
Del(p);
}
12.非递归先根遍历
void NPO(BiTree t) {
BiTree p = t;
while (p || !s.empty()) {
if (p) {
s.push(p);
cout << p->data;
p = p->lchild;
}
else {
p = s.top();
s.pop();
p = p->rchild;
}
}
}
13.非递归中根遍历
void NIO(BiTree t) {
BiTree p = t;
while(p!=nullptr||!s.empty()){
if (p) {
s.push(p);
p = p->lchild;
}
else {
p = s.top();
s.pop();
cout << p->data;
p = p->rchild;
}
}
}
14.非递归后根遍历
typedef struct BTNode {
BiTree node;
int cnt;
}BTNode,*BTree;
stack<BTree>s;
void NPO1(BiTree t) {
BiTree p = t;
BTree tmp;
while (p || !s.empty()) {
while (p) {
BTree q = (BTree)malloc(sizeof(BTNode));
q->node = p;
q->cnt = 1;
s.push(q);
p = p->lchild;
}
if (!s.empty()) {
tmp = s.top();
s.pop();
if (tmp->cnt == 1) {
tmp->cnt++;
s.push(tmp);
p = tmp->node->rchild;
}
else {
cout << tmp->node->data;
p = nullptr;
}
}
}
}