C语言 二叉树
创始人
2024-03-19 19:33:36
0

#include
#include
typedef int DataType;
typedef struct Node{
    DataType data;
    struct Node *leftChild;//左孩子指针 
    struct Node *rightChild;//右孩子指针 
}BiTreeNode;
void visit(DataType item)//访问函数 
{
    printf("%c  ",item);
}
void Initiate(BiTreeNode **root)//初始化 
{
    *root=(BiTreeNode *)malloc(sizeof(BiTreeNode));
    (*root)->leftChild=NULL;
    (*root)->rightChild=NULL;
}
BiTreeNode *InsertLeftNode(BiTreeNode *curr,DataType x)//左插入结点
{
    BiTreeNode *s,*t;
    if(curr==NULL)return NULL;
    t=curr->leftChild;//保存原curr的左孩子指针
    s=(BiTreeNode *)malloc(sizeof(BiTreeNode));
    s->data=x ;
    s->leftChild=t;
    s->rightChild=NULL;
    curr->leftChild=s;
    return curr->leftChild;
}
BiTreeNode *InsertrightNode(BiTreeNode *curr,DataType x)//右插入结点 
{
    BiTreeNode *s,*t;
    if(curr==NULL)return NULL;
    t=curr->rightChild;//保存原curr的左孩子指针
    s=(BiTreeNode *)malloc(sizeof(BiTreeNode));
    s->data=x ;
    s->rightChild=t;
    s->leftChild=NULL;
    curr->rightChild=s;
    return curr->rightChild;
}
void Destroy(BiTreeNode **root)//撤销 
{
    if((*root)!=NULL&&(*root)->leftChild!=NULL)
    Destroy(&(*root)->leftChild);
    if((*root)!=NULL&&(*root)->rightChild!=NULL)
    Destroy(&(*root)->rightChild);
    free(*root);
}
BiTreeNode *DeleteLeftTree(BiTreeNode *curr)//左删除子树 
{
    if(curr==NULL||curr->leftChild==NULL)return NULL;
    Destroy(&curr->leftChild);
    curr->leftChild=NULL;
    return curr;
}

BiTreeNode *DeleterightTree(BiTreeNode *curr)//右删除子树 
{
    if(curr==NULL||curr->rightChild==NULL)return NULL;
    Destroy(&curr->rightChild);
    curr->rightChild=NULL;
    return curr;
}
void PreOrder(BiTreeNode *root,void visit(DataType item))//前序遍历 
{
    if(root!=NULL)
    {
        visit(root->data);
        PreOrder(root->leftChild,visit);
        PreOrder(root->rightChild,visit);
    }
}
void InOrder(BiTreeNode *root,void visit(DataType item))//中序遍历
{
    if(root!=NULL)
    {
        InOrder(root->leftChild,visit);
        visit(root->data);        
        InOrder(root->rightChild,visit);
    }
}
void PostOrder(BiTreeNode *root,void visit(DataType item))//后序遍历
{
    if(root!=NULL)
    {
        PostOrder(root->leftChild,visit);        
        PostOrder(root->rightChild,visit);
        visit(root->data);                
    }
}
void PrintBiTree(BiTreeNode *root,int n)//打印二叉树 
{
    int i;
    if(root==NULL)
    return;
    PrintBiTree(root->rightChild,n+1);
    for(i=0;i     printf("  ");
    if(n>0)
    {
        printf("---");
        printf("%c\n",root->data);
    }
    PrintBiTree(root->leftChild,n+1);
}
BiTreeNode *Search(BiTreeNode *root,DataType x)
{
    BiTreeNode *find=NULL;
    if(root!=NULL)
    {
        if(root->data==x)
        find=root;
        else
        {
            find=Search(root->leftChild,x);
            if(find==NULL)
            find=Search(root->rightChild,x);
        }
    }
    return find;
}
int main()
{
    BiTreeNode *root,*p,*find;
    char x='E';
    Initiate(&root);
    p=InsertLeftNode(root,'A');
    p=InsertLeftNode(p,'B');
    p=InsertLeftNode(p,'D');
    p=InsertrightNode(p,'G');
    p=InsertrightNode(root->leftChild,'C');
    InsertLeftNode(p,'E');
    InsertrightNode(p,'F');
    PrintBiTree(root,0);
    printf("前序遍历:");
    PreOrder(root->leftChild,visit);
    printf("\n中序遍历:");
    InOrder(root->leftChild,visit);
    printf("\n后序遍历:");
    PostOrder(root->leftChild,visit);
}

相关内容

热门资讯

监控摄像头接入GB28181平... 流程简介将监控摄像头的视频在网站和APP中直播,要解决的几个问题是:1&...
Windows10添加群晖磁盘... 在使用群晖NAS时,我们需要通过本地映射的方式把NAS映射成本地的一块磁盘使用。 通过...
protocol buffer... 目录 目录 什么是protocol buffer 1.protobuf 1.1安装  1.2使用...
在Word、WPS中插入AxM... 引言 我最近需要写一些文章,在排版时发现AxMath插入的公式竟然会导致行间距异常&#...
Fluent中创建监测点 1 概述某些仿真问题,需要创建监测点,用于获取空间定点的数据࿰...
educoder数据结构与算法...                                                   ...
MySQL下载和安装(Wind... 前言:刚换了一台电脑,里面所有东西都需要重新配置,习惯了所...
MFC文件操作  MFC提供了一个文件操作的基类CFile,这个类提供了一个没有缓存的二进制格式的磁盘...
有效的括号 一、题目 给定一个只包括 '(',')','{','}'...
【Ctfer训练计划】——(三... 作者名:Demo不是emo  主页面链接:主页传送门 创作初心ÿ...