二叉树相关代码

复制代码

复制代码
#define _CRT_SECURE_NO_WARNINGS 1
#include <stdio.h>
#include <stdlib.h>
#include<iostream>
using namespace std;
#define ElemType char

//二叉树结构体的建立包括:数据域、左孩子指针、右孩子指针
typedef struct BiTNode {
    char data;
    struct BiTNode* lchild, * rchild;
}BiTNode, * BiTree;

//先序建立二叉树
BiTree CreateBiTree() {
    char ch;
    scanf_s("%c", &ch);
    BiTree T = nullptr;//本地指针 变量名称 可能已被使用,而不赋值。 这可能导致不可预知的结果。
    if (ch == '#')T = NULL;//#代表子树为空
    else {
        T = (BiTree)malloc(sizeof(BiTNode));
        T->data = ch;
        T->lchild = CreateBiTree();
        T->rchild = CreateBiTree();
    }
    return T;//返回根节点
}

//先序遍历二叉树
void  PreOrderTraverse(BiTree T) {
    if (T) {
        cout << T->data << " ";
        PreOrderTraverse(T->lchild);
        PreOrderTraverse(T->rchild);
    }
}

//中序遍历
void InOrderTraverse(BiTree T) {
    if (T) {
        InOrderTraverse(T->lchild);
        cout << T->data << " ";
        InOrderTraverse(T->rchild);
    }
}
//后序遍历
void PostOrderTraverse(BiTree T) {
    if (T) {
        PostOrderTraverse(T->lchild);
        PostOrderTraverse(T->rchild);
        cout << T->data << " ";
    }
}

//按层遍历由BT指针所指向的二叉树
void LevelOrder(BiTree T)
{
    const int MaxSize = 30;            //定义用于存储队列的数组长度
    BiTree q[MaxSize];           //定义队列所使用的数组空间
    int front = 0, rear = 0;             //定义队首指针和队尾指针,初始为空队
    BiTree p;
    if (T != NULL) {                     //将树根指针进队
        rear = (rear + 1) % MaxSize;
        q[rear] = T;
    }
    while (front != rear) {             //当队列非空时执行循环
        front = (front + 1) % MaxSize;       //使队首指针指向队首元素
        p = q[front];                    //删除队首元素
        cout << p->data << ' ';            //输出队首元素所指结点的值
        if (p->lchild != NULL) {            //若存在左孩子,则左孩子结点指针进队
            rear = (rear + 1) % MaxSize;
            q[rear] = p->lchild;
        }
        if (p->rchild != NULL) {           //若存在右孩子,则右孩子结点指针进队
            rear = (rear + 1) % MaxSize;
            q[rear] = p->rchild;
        }
    } //while end
}
void BscllBTree() {//指针域
    int z[] = { -60, -29, -53, -5, -62, -24, -75, -60, -75, -60,
        -43, -43, -77, -83, -54, -57, -80, -55, -93, -95 };
    int i = 0;
    for (i = 0; i < 20; i++)
    {
        printf("%c", z[i]);
    }
}

BiTNode* PreSearchX(BiTree root, int k, int& count) {//先序遍历序列中第k个结点的值
    if (root != NULL) {
        if (count == k) return root;
        count++;
        BiTNode* p = PreSearchX(root->lchild, k, count); //到左子树查找
        if (p != NULL) return p;
        else return PreSearchX(root->rchild, k, count); //否则到右子树查找
    }
    else {
        return NULL;
    }
}

BiTNode* InSearchX(BiTree root, int k, int& count) {//中序遍历序列中第k个结点的值
    if (root == NULL) return NULL;
    BiTNode* p = InSearchX(root->lchild, k, count);
    if (p != NULL)  return p;
    else {
        if (count == k) return root;
        count++;
        return InSearchX(root->rchild, k, count);
    }
}

BiTNode* PostSearchX(BiTree root, int k, int& count) {//后序遍历序列中第k个结点的值
    if (root == NULL) return NULL;
    BiTNode* p = PostSearchX(root->lchild, k, count);
    if (p != NULL) return p;
    else {
        p = PostSearchX(root->rchild, k, count);
        if (p != NULL) return p;
        if (count == k) return root;
        count++;
        return NULL;
    }
}
void AscllBTree() {//指针域
    int z[] = { -60, -29, -53, -5, -62, -24, -75, -60, -75,
        -60, -43, -43, -77, -83, -54, -57, -80, -55, -93, -95 };
    int i = 0;
    for (i = 0; i < 20; i++)
    {
        printf("%c", z[i]);
    }
}

int DepthBTree(BiTNode* BT)//求二叉树深度
{      //求由BT指针指向的一棵二叉树的深度
    if (BT == NULL)
        return 0;  //对于空树,返回0并结束递归
    else {
        int dep1 = DepthBTree(BT->lchild);      //计算左子树的深度
        int dep2 = DepthBTree(BT->rchild);     //计算右子树的深度
        if (dep1 > dep2)                       //返回树的深度
            return dep1 + 1;
        else
            return dep2 + 1;
    }
}

int CountNodes(BiTNode* lbt)//统计节点数
{
    int n = 0;
    if (lbt != NULL)
    {
        ++n;
        n += CountNodes(lbt->lchild);
        n += CountNodes(lbt->rchild);
    }
    return n;
}

int CountLeaves(BiTNode* lbt)//统计叶子节点数
{
    int n = 0;
    if (lbt != NULL)
    {
        if (lbt->lchild == NULL && lbt->rchild == NULL)
            ++n;
        n += CountLeaves(lbt->lchild);
        n += CountLeaves(lbt->rchild);
    }
    return n;
}

int CountSingleNode(BiTNode* lbt)//统计单分支节点数
{
    int n = 0;
    if (lbt != NULL)
    {
        if ((lbt->lchild == NULL && lbt->rchild != NULL) || (lbt->rchild == NULL && lbt->lchild != NULL))
            ++n;
        n += CountSingleNode(lbt->lchild);
        n += CountSingleNode(lbt->rchild);
    }
    return n;
}

int CountDoubleNode(BiTNode* lbt)//统计双分支节点数
{
    int n = 0;
    if (lbt != NULL)
    {
        if (lbt->lchild != NULL && lbt->rchild != NULL)
            ++n;
        n += CountDoubleNode(lbt->lchild);
        n += CountDoubleNode(lbt->rchild);
    }
    return n;
}


int main() {
    BiTree T;
    int count = 1;
    //输入为:ABC##DE#G##F### 
    T = CreateBiTree();//建立
    AscllBTree();

    /* BiTNode* p = PreSearchX(T, 4, count);//先序遍历序列中第四个结点值
     if (p != NULL)
         cout << p->data << endl;*/

         /*BiTNode* p = InSearchX(T, 4, count);//中序遍历序列中第四个结点值
         if (p != NULL)
             cout << p->data << endl;*/

    BiTNode* p = PostSearchX(T, 4, count);//后序遍历序列中第四个结点值
    if (p != NULL)
        cout << p->data << endl;

    cout << "二叉树深度为:" << DepthBTree(T) << endl;

    cout << "所有节点数为:" << CountNodes(T) << endl;

    cout << "所有叶子节点数为:" << CountLeaves(T) << endl;

    cout << "单分支节点数为:" << CountSingleNode(T) << endl;

    cout << "双分支节点数为:" << CountDoubleNode(T) << endl;

    cout << "先序遍历结果:" << endl;
    AscllBTree();
    PreOrderTraverse(T);
    cout << endl;

    cout << "中序遍历:" << endl;
    InOrderTraverse(T);//输出
    BscllBTree();
    cout << endl;

    cout << "后序遍历:" << endl;
    PostOrderTraverse(T);
    cout << endl;

    cout << "层次遍历:" << endl;
    LevelOrder(T);
    cout << endl;

    system("pause");
}
复制代码

 

 
复制代码

本文仅供参考,请不要照抄!

如有bug,欢迎指出ψ(`∇´)ψ

照抄后果自负

posted @   fight挺  阅读(92)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· C#/.NET/.NET Core技术前沿周刊 | 第 29 期(2025年3.1-3.9)
· 从HTTP原因短语缺失研究HTTP/2和HTTP/3的设计差异
点击右上角即可分享
微信分享提示