同过二叉树完成文本输出与链表大同小异。
目标头文件
#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;