C++实现二叉树的常用操作

#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);
        }
    }
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

随风舞落叶殇

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值