二叉树的基本操作(C语言实现)

二叉树的建立、遍历、统计、树状打印

#include <stdio.h>
#include <stdlib.h>
int c_Node = 0;     //节点数
int c_Leaf = 0;     //叶子数
int depth = 0;      //二叉树高度
typedef char ElemType;
typedef struct Node         //节点
{
    ElemType data;          //节点数据
    struct Node *lchild;        //左孩子
    struct Node *rchild;        //右孩子
} biNode, *biTree;

//先序建立二叉树
void createTree(biNode **root)
{

    ElemType data;
    scanf("%c",&data);
    if(data == '#')
    {
        (*root)=NULL;
    }

    else
    {
        *root = (biNode *)malloc(sizeof(biNode));
        if((*root) == NULL)
        {
            printf("分配空间失败!\n");
            exit(0);
        }
        (*root)->data = data;
        createTree(&((*root)->lchild));
        createTree(&((*root)->rchild));
    }

}


//先序遍历二叉树
void preOrder(biNode *root)
{
    if(root)
    {
        printf("%c ",root->data);
        preOrder(root->lchild);
        preOrder(root->rchild);
    }

}


//中序遍历二叉树
void inOrder(biNode *root)
{
    if(root)
    {
        inOrder(root->lchild);
        printf("%c ",root->data);
        inOrder(root->rchild);
    }

}

//后序遍历二叉树
void postOrder(biNode *root)
{
    if(root)
    {
        postOrder(root->lchild);
        postOrder(root->rchild);
        printf("%c ",root->data);
    }
}


//统计节点数
void cal_Node(biNode *root)
{
    if(root)
    {
        c_Node++;
        cal_Node(root->lchild);
        cal_Node(root->rchild);
    }
}

//统计叶子数 方法一
void cal_Leaf(biNode *root)
{
    if(root)
    {
        if(root->lchild==NULL&&root->rchild==NULL) c_Leaf++;
        cal_Leaf(root->lchild);
        cal_Leaf(root->rchild);
    }
}

//统计叶子数 方法二
int calLeaf(biNode *root)
{
    int hl,hr;
    if(root==NULL) return 0;
    if(root)
    {
        if(root->lchild==NULL&&root->rchild==NULL) return 1;
        hl = calLeaf(root->lchild);
        hr = calLeaf(root->rchild);
    }
    return hl+hr;
}

//打印叶子数据
void printLeaf(biNode *root)
{
    if(root)
    {
        if(root->lchild==NULL&&root->rchild==NULL)
            printf("%c ",root->data);
        printLeaf(root->lchild);
        printLeaf(root->rchild);
    }
}

//计算二叉树深度(方法一)
int cal_Depth(biNode *root)
{
    if(root == NULL) return 0;
    if(root)
    {
        int hl = cal_Depth(root->lchild);
        int hr = cal_Depth(root->rchild);
        return hl>hr?hl+1:hr+1;
    }
}

//计算二叉树高度(方法二)
void calDepth(biNode *root, int h)
{

    if(root)
    {
        if(h > depth) depth = h;
        calDepth(root->lchild,h+1);
        calDepth(root->rchild,h+1);
    }
}

//树状打印二叉树
void print_Tree(biNode *root, int h) {

    if(root) {
        print_Tree(root->rchild, h+1);
        for(int i=0; i<h; i++) printf("--");
        printf("%c\n",root->data);
        print_Tree(root->lchild, h+1);
    }

}


int main()
{
    printf("先序输入二叉树数据(# = NULL):");
    biNode *root;
    createTree(&root);
    printf("先序遍历结果:");
    preOrder(root);
    printf("\n\n中序遍历结果:  ");
    inOrder(root);
    printf("\n\n后序遍历结果: ");
    postOrder(root);
    cal_Node(root);
    cal_Leaf(root);
    printf("\n\n节点数: %d\n",c_Node);
    printf("\n\n叶子点数:%d\n",c_Leaf);
    printf("叶子数(方法二):%d\n",calLeaf(root));
    printf("\n打印叶子节点: ");
    printLeaf(root);
    calDepth(root,1);
    printf("\n二叉树高度(方法一):%d\n",cal_Depth(root));
    printf("二叉树高度(方法二):%d\n",depth);
    printf("\n\n树状打印二叉树\n\n");
    print_Tree(root,1);


}
/*
测试数据
ABC##DE#G##F###
*/

测试结果

原文出处https://blog.csdn.net/Vincent_Xupt/article/details/78797907

以下是二叉树基本操作C语言实现: ```c #include <stdio.h> #include <stdlib.h> //定义二叉树结构体 typedef struct TreeNode { int data; struct TreeNode* left; struct TreeNode* right; } TreeNode; //创建二叉树节点 TreeNode* createNode(int data) { TreeNode* node = (TreeNode*)malloc(sizeof(TreeNode)); node->data = data; node->left = NULL; node->right = NULL; return node; } //创建二叉树 TreeNode* createTree() { int data; scanf("%d", &data); if (data == -1) { //输入-1表示该节点为空 return NULL; } TreeNode* root = createNode(data); printf("请输入%d的左子节点:", data); root->left = createTree(); printf("请输入%d的右子节点:", data); root->right = createTree(); return root; } //先序遍历 void preorderTraverse(TreeNode* root) { if (root == NULL) { return; } printf("%d ", root->data); preorderTraverse(root->left); preorderTraverse(root->right); } //中序遍历 void inorderTraverse(TreeNode* root) { if (root == NULL) { return; } inorderTraverse(root->left); printf("%d ", root->data); inorderTraverse(root->right); } //后序遍历 void postorderTraverse(TreeNode* root) { if (root == NULL) { return; } postorderTraverse(root->left); postorderTraverse(root->right); printf("%d ", root->data); } //层序遍历 void levelorderTraverse(TreeNode* root) { if (root == NULL) { return; } TreeNode* queue[1000]; int front = 0, rear = 0; queue[rear++] = root; while (front < rear) { TreeNode* node = queue[front++]; printf("%d ", node->data); if (node->left) { queue[rear++] = node->left; } if (node->right) { queue[rear++] = node->right; } } } //插入节点 void insertNode(TreeNode* root, int data) { if (root == NULL) { return; } if (root->left == NULL) { root->left = createNode(data); } else if (root->right == NULL) { root->right = createNode(data); } else { //如果当前节点的左右子节点都不为空,则递归插入左右子树 insertNode(root->left, data); insertNode(root->right, data); } } //删除节点 void deleteNode(TreeNode* root, int data) { if (root == NULL) { return; } if (root->left != NULL && root->left->data == data) { free(root->left); root->left = NULL; } else if (root->right != NULL && root->right->data == data) { free(root->right); root->right = NULL; } else { //如果当前节点的左右子节点都不为空,则递归删除左右子树 deleteNode(root->left, data); deleteNode(root->right, data); } } //查找节点 TreeNode* searchNode(TreeNode* root, int data) { if (root == NULL) { return NULL; } if (root->data == data) { return root; } TreeNode* left = searchNode(root->left, data); TreeNode* right = searchNode(root->right, data); if (left != NULL) { return left; } if (right != NULL) { return right; } return NULL; } //修改节点 void modifyNode(TreeNode* node, int newData) { if (node == NULL) { return; } node->data = newData; } int main() { printf("请输入二叉树的根节点:"); TreeNode* root = createTree(); printf("先序遍历:"); preorderTraverse(root); printf("\n中序遍历:"); inorderTraverse(root); printf("\n后序遍历:"); postorderTraverse(root); printf("\n层序遍历:"); levelorderTraverse(root); printf("\n插入节点5后:"); insertNode(root, 5); levelorderTraverse(root); printf("\n删除节点5后:"); deleteNode(root, 5); levelorderTraverse(root); printf("\n查找节点3:"); TreeNode* node = searchNode(root, 3); if (node != NULL) { printf("找到了,节点的值为%d\n", node->data); } else { printf("没找到\n"); } printf("修改节点3的值为10后:"); modifyNode(node, 10); levelorderTraverse(root); return 0; } ``` 这段代码实现二叉树创建遍历、插入、删除、查找和修改等基本操作
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值