数据结构--二叉树代码实现(建立,前序,中序,后续遍历,树的深度,销毁)

代码的树的结构为

                                        1

                              2             3

                     4          5     6         7

              8

#include <stdio.h>
#include <assert.h>
#include <stdlib.h>

/* 结构体 */
typedef struct treenode
{
    struct treenode *pstLeftChild;
    struct treenode *pstRightChild;
    int idata;
}TreeNode_S,TreeHead_S;

/* 初始化树 */
TreeHead_S *InitTree(TreeNode_S *pstNode)
{
    TreeHead_S *tree = pstNode;
    return tree;
}

/* 创建树的节点 */
TreeNode_S *CreateTreeNode(int idata, TreeNode_S *pstleftchild, TreeNode_S *pstrightchild)
{
    TreeNode_S *pstNewNode;

    pstNewNode = malloc(sizeof(TreeNode_S));
    if (pstNewNode != NULL)
    {
        pstNewNode->idata = idata;
        pstNewNode->pstLeftChild = pstleftchild;
        pstNewNode->pstRightChild = pstrightchild;
    }

    return pstNewNode;
}

/* 销毁树节点 */
void DestroyTreeNode(TreeNode_S *pstTreeNode)
{
    if (pstTreeNode != NULL)
    {
        printf("%d ", pstTreeNode->idata);
        free(pstTreeNode);
    }

    return;
}

/* 销毁树*/
void DestroyTree(TreeHead_S *pstTreeHead)
{
    if (pstTreeHead != NULL)
    {
        if (pstTreeHead->pstLeftChild != NULL)
        {
            DestroyTree(pstTreeHead->pstLeftChild);
        }

        if (pstTreeHead->pstRightChild != NULL)
        {
            DestroyTree(pstTreeHead->pstRightChild);
        }

        DestroyTreeNode(pstTreeHead);
    }
}

/* 获取树的深度 */
int GetTreeDepth(TreeHead_S *pstTreeHead)
{
    int l = 0;
    int r = 0;
    TreeNode_S *pstLeftChild;
    TreeNode_S *pstRightChild;

    if (pstTreeHead == NULL)
    {
        return 0;
    }
    l = 1;
    r = 1;


    l += GetTreeDepth(pstTreeHead->pstLeftChild);
    r += GetTreeDepth(pstTreeHead->pstRightChild);

    if (l < r)
    {
        l = r;
    }

    return l;

}

/* 前序遍历 */
void Front_Scan(TreeHead_S *pstTreeHead)
{
    if (pstTreeHead == NULL)
    {
        return;
    }

    printf("%d ",pstTreeHead->idata);
    Front_Scan(pstTreeHead->pstLeftChild);
    Front_Scan(pstTreeHead->pstRightChild);

    return;
}

/* 中序遍历 */
void Middle_Scan(TreeHead_S *pstTreeHead)
{
    if (pstTreeHead == NULL)
    {
        return;
    }

    Middle_Scan(pstTreeHead->pstLeftChild);
    printf("%d ",pstTreeHead->idata);
    Middle_Scan(pstTreeHead->pstRightChild);

    return;
}

/* 后序遍历 */
void End_Scan(TreeHead_S *pstTreeHead)
{
    if (pstTreeHead == NULL)
    {
        return;
    }

    End_Scan(pstTreeHead->pstLeftChild);
    End_Scan(pstTreeHead->pstRightChild);
    printf("%d ",pstTreeHead->idata);

    return;
}

int main(void)
{
    TreeHead_S *pstTree;
    TreeNode_S *pstNode8 = CreateTreeNode(8, NULL, NULL);
    TreeNode_S *pstNode7 = CreateTreeNode(7, NULL, NULL);
    TreeNode_S *pstNode6 = CreateTreeNode(6, NULL, NULL);
    TreeNode_S *pstNode5 = CreateTreeNode(5, NULL, NULL);
    TreeNode_S *pstNode4 = CreateTreeNode(4, pstNode8, NULL);
    TreeNode_S *pstNode3 = CreateTreeNode(3, pstNode6, pstNode7);
    TreeNode_S *pstNode2 = CreateTreeNode(2, pstNode4, pstNode5);
    TreeNode_S *pstNode1 = CreateTreeNode(1, pstNode2, pstNode3);

    assert((pstNode1 != NULL)&&(pstNode2 != NULL)&&(pstNode3 != NULL)&&
           (pstNode4 != NULL)&&(pstNode5 != NULL)&&(pstNode6 != NULL)&&
           (pstNode7 != NULL)&&(pstNode8 != NULL));

    pstTree = InitTree(pstNode1);

    printf("depth:%d\n", GetTreeDepth(pstTree));

    printf("front scan: ");
    Front_Scan(pstTree);
    printf("\nmiddle scan: ");
    Middle_Scan(pstTree);
    printf("\nend scan: ");
    End_Scan(pstTree);
    printf("\ndestroy tree: ");
    DestroyTree(pstTree);
    printf("\n");

    return 0;
}


建立二叉树实现前序中序和后遍历通常涉及递归算法。这里是一个简单的C语言示例,假设我们有一个`TreeNode`结构表示二叉树节点: ```c #include <stdio.h> #include <stdlib.h> // 定义二叉树节点结构 typedef struct TreeNode { int val; struct TreeNode *left; struct TreeNode *right; } TreeNode; // 前序遍历 (根 -> 左 -> 右) void preorderTraversal(TreeNode* root) { if (root != NULL) { printf("%d ", root->val); // 访问根节点 preorderTraversal(root->left); // 遍历左子 preorderTraversal(root->right); // 遍历右子 } } // 中序遍历 (左 -> 根 -> 右) void inorderTraversal(TreeNode* root) { if (root != NULL) { inorderTraversal(root->left); // 遍历左子 printf("%d ", root->val); // 访问根节点 inorderTraversal(root->right); // 遍历右子 } } // 后遍历 (左 -> 右 -> 根) void postorderTraversal(TreeNode* root) { if (root != NULL) { postorderTraversal(root->left); // 遍历左子 postorderTraversal(root->right); // 遍历右子 printf("%d ", root->val); // 访问根节点 } } // 创建二叉树的例子(插入节点) TreeNode* insertNode(TreeNode* root, int val) { if (root == NULL) { root = (TreeNode*)malloc(sizeof(TreeNode)); root->val = val; root->left = NULL; root->right = NULL; } else if (val < root->val) { root->left = insertNode(root->left, val); } else { root->right = insertNode(root->right, val); } return root; } int main() { // 创建一个简单的二叉搜索,然后进行遍历演示 TreeNode* root = NULL; root = insertNode(root, 50); insertNode(root, 30); insertNode(root, 20); insertNode(root, 40); insertNode(root, 70); insertNode(root, 60); insertNode(root, 80); printf("前序遍历: "); preorderTraversal(root); printf("\n"); printf("中序遍历: "); inorderTraversal(root); printf("\n"); printf("后遍历: "); postorderTraversal(root); printf("\n"); return 0; } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值