#include <iostream>
using namespace std;
#define maxSize 20
struct BTNode {
char data;
struct BTNode *LChild;
struct BTNode *RChild;
};
struct st {
BTNode *p;
int Lno;
};
void visit(BTNode *p);//输出结点值
void preOrder(BTNode *p);//先序遍历
void inOrder(BTNode *p);//中序遍历
void postOrder(BTNode *p);//后序遍历
int op(int a, int b, char opt);
int getDepth(BTNode *p);//获得树的深度
int comp(BTNode *p);//计算表达式的值
void search(BTNode *p, BTNode *&q, int key);//在二叉树中查找是否具有与key值相等的结点
void travel(BTNode *p, int k);//输出先序遍历的第K个结点
void level(BTNode *p);//层次遍历
int maxNode(BTNode *p);//计算二叉树的宽度
void preOrderNoRecursion(BTNode *p);//非递归实现先序遍历
void inOrderNoRecursion(BTNode *p);//非递归实现中序遍历
void postOrderNoRecursion(BTNode *p);//非递归实现后序遍历
void visit(BTNode *p) {
if (p == nullptr)
return;
cout << p->data;
}
void preOrder(BTNode *p) {
if (p != nullptr) {
visit(p);
preOrder(p->LChild);
preOrder(p->RChild);
}
}
void inOrder(BTNode *p) {
if (p != nullptr) {
inOrder(p->LChild);
visit(p);
inOrder(p->RChild);
}
}
void postOrder(BTNode *p) {
if (p != nullptr) {
postOrder(p->LChild);
postOrder(p->RChild);
visit(p);
}
}
int comp(BTNode *p) {
int A, B;//A为左子树的值,B为右子树的值
if (p != nullptr) {
if (p->LChild != nullptr && p->RChild != nullptr) {
A = comp(p->LChild);
B = comp(p->RChild);
return op(A, B, p->data);
} else
return p->data - '0';
} else {
return 0;
}
}
int op(int a, int b, char opt) {
if (opt == '+') return a + b;
if (opt == '-') return a - b;
if (opt == '*') return a * b;
if (opt == '/') return a / b;
return 0;
}
int getDepth(BTNode *p) {
int LD, RD;
if (p == nullptr) {
return 0;
} else {
LD = getDepth(p->LChild);
RD = getDepth(p->RChild);
return (LD > RD ? LD : RD) + 1;
}
}
void search(BTNode *p, BTNode *&q, int key) {
if (p != nullptr) {
if (p->data == key) {
q = p;
return;
} else {
search(p->LChild, q, key);
if (q == nullptr) {
search(p->RChild, q, key);
}
}
}
}
void travel(BTNode *p, int k) {
int n = 0;
++n;
if (n == k) {
cout << p->data << endl;
return;
}
travel(p->LChild, k);
travel(p->RChild, k);
}
void level(BTNode *p) {
int rear = 0;
int front = 0;
BTNode *queue[maxSize];
BTNode *q;
if (p != nullptr) {
rear = (rear + 1) % maxSize;
queue[rear] = p;
while (front != rear) {
front = (front + 1) % maxSize;
q = queue[front];
visit(q);
if (q->LChild != nullptr) {
rear = (rear + 1) % maxSize;
queue[rear] = q->LChild;
}
if (q->RChild != nullptr) {
rear = (rear + 1) % maxSize;
queue[rear] = q->RChild;
}
}
}
}
int maxNode(BTNode *p) {
st queue[maxSize];
BTNode *q;
int front = 0;
int rear = 0;
int Lno = 0;
int i, j;
int n;
int max = 0;
if (p != nullptr) {
++rear;
queue[rear].p = p;
queue[rear].Lno = 1;
while (front != rear) {
++front;
q = queue[front].p;
Lno = queue[front].Lno;
if (q->LChild != nullptr) {
++rear;
queue[rear].p = q->LChild;
queue[rear].Lno = Lno + 1;
}
if (q->RChild != nullptr) {
++rear;
queue[rear].p = q->RChild;
queue->Lno = Lno + 1;
}
}
for (i = 0; i < Lno; ++i) {
n = 0;
for (j = 0; j < rear; ++j) {
if (queue[j].Lno == i) {
++n;
}
if (max < n) {
max = n;
}
return max;
}
}
} else
return 0;
return 0;
}
void preOrderNoRecursion(BTNode *p) {
if (p != nullptr) {
BTNode *Stack[maxSize];
int top = -1;
BTNode *q;
Stack[++top] = p;
while (top != -1) {
q = Stack[--top];
visit(q);
if (p->RChild != nullptr)
Stack[++top] = p->RChild;
if (p->LChild != nullptr)
Stack[++top] = p->LChild;
}
}
}
void inOrderNoRecursion(BTNode *p) {
if (p != nullptr) {
BTNode *Stack[maxSize];
int top = -1;
BTNode *q = p;
Stack[++top] = p;
while (top != -1 || q != nullptr) {
while (q != nullptr) {
Stack[++top] = q;
q = q->LChild;
}
if (top != -1) {
q = Stack[top--];
visit(q);
p = p->RChild;
}
}
}
}
void postOrderNoRecursion(BTNode *p) {
if (p != nullptr) {
BTNode *Stack1[maxSize];
BTNode *Stack2[maxSize];
int top1 = -1;
int top2 = -1;
BTNode *q = p;
Stack1[++top1] = p;
while (top1 != -1) {
q = Stack1[top1--];
Stack2[++top2] = q;
if (q->LChild != nullptr) {
Stack1[++top1] = q;
q = q->LChild;
}
if (q->RChild != nullptr) {
Stack1[++top1] = q;
q = q->RChild;
}
}
while (top2 != -1){
q = Stack2[top2--];
visit(q);
}
}
}
C++实现二叉树的常用操作
最新推荐文章于 2022-07-25 17:08:48 发布