通过二叉树完成文本输出

同过二叉树完成文本输出与链表大同小异。

目标头文件

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define IS_NOT_LETTER(C) (!((C>='a' && C<='z') || (C>='A' && C<='Z')))

二叉树定义

struct treenode
{
    struct treenode* parent;
    struct treenode* lchild;
    struct treenode* rchild;
    void* data;
};
 
struct tree
{
    struct treenode* root;
    int (*comp)(void* node1, void* node2);
};

初始二叉树

struct tree* firsttree(int(*comp)(void* node1,void* node2))
{
    struct tree* mytree = (struct tree*)malloc(sizeof(struct tree));
    struct treenode*root = (struct treenode*)malloc(sizeof(struct treenode));
    mytree->root = root;
    root->parent = NULL;
    root->lchild = NULL;
    root->rchild = NULL;
    root->data = NULL;
    mytree->comp = comp;
    return mytree;

}

插入

//插入
struct treenode* addtreenode(struct tree* mytree, struct treenode* node,char* pstrat,int wordsize)
{

    if (pstrat == NULL || mytree == NULL)
    {
        return NULL;
    }
    if (mytree->root->data == NULL)
    {
        
        (char*)node->data ;
		node->data= (char*)malloc(sizeof(char)*(wordsize+1));
        memset(node->data, 0, wordsize + 1);
        memcpy(node->data, pstrat, wordsize);
    }
    else
    {
        if (node == NULL)
        {
            node = (struct treenode*)malloc(sizeof(struct treenode));   
            
           
            (char*)node->data ;
			node->data= (char*)malloc(sizeof(char) * (wordsize + 1));
            memset(node->data, 0, wordsize + 1);
            memcpy(node->data, pstrat, wordsize);
            node->lchild = NULL;
            node->rchild = NULL;
           
        }
        else
        {
            if (comp(node->data, pstrat) > 0)
            {
                node->lchild = addtreenode(mytree, node->lchild,pstrat,wordsize);
                node->lchild->parent = node;
            }
            else
            {
                node->rchild = addtreenode(mytree, node->rchild,pstrat,wordsize);
                node->rchild->parent = node;
      
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值