虽然对树还是比较了解的,但是确实没有认真的写过一次完整的关于树的代码,这一次逼着自己写了一些关于二叉树的比较完整的操作,还有一些非递归的操作(如树的遍历)没有写呢,有机会会添加进去的。
    头文件:
   
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>

#define MAX_QUEUE_SIZE 50

typedef char elemType;
typedef struct BiTNode
{
  elemType data;                         //值域
  struct BiTNode *lchild;     //指向左孩子的指针
  struct BiTNode *rchild;    //指向右孩子的指针
}BiTNode,*ptr_BiTNode;


void InitBinaryTree(ptr_BiTNode *ptr_bitree)
{//初始化一棵空二叉树
  (*ptr_bitree)=NULL;
}


void CreateBinaryTree(ptr_BiTNode *ptr_bitree)
{     //按先序次序输入二叉树中结点的值(一个字符),空格字符表示空树,
  //构造二叉链表表示的二叉树
  elemType value;
  scanf("%c",&value);
  if(value==' ')
  {
    *ptr_bitree=NULL;
  }
  else
  {
    if(!((*ptr_bitree)=(BiTNode *)malloc(sizeof(BiTNode))))
    {
      printf("allocation failure.\n");
    }
    else
    {
      (*ptr_bitree)->data=value;                                            //生成根结点
      CreateBinaryTree(&((*ptr_bitree)->lchild));        //构造左子树
      CreateBinaryTree(&((*ptr_bitree)->rchild));     //构造右子树
    }
  }
}

void DestroyBinaryTree(ptr_BiTNode *ptr_bitree)
{//销毁二叉树
  if(*ptr_bitree)
  {//二叉树存在
    if((*ptr_bitree)->lchild)
    {//销毁左子树
      DestroyBinaryTree(&((*ptr_bitree)->lchild));
    }
    if((*ptr_bitree)->rchild)
    {//销毁右子树
      DestroyBinaryTree(&((*ptr_bitree)->rchild));
    }
    free(*ptr_bitree);//释放根节点指针
    (*ptr_bitree)=NULL;
  }
}

#define ClearBinaryTree DestroyBinaryTree

bool IsBinaryTreeEmpty(ptr_BiTNode ptr_bitree)
{//判断二叉树是否为空
  if(ptr_bitree)
  {
    return false;
  }
  else
  {
    return true;
  }
}

int BinaryTreeDepth(ptr_BiTNode ptr_bitree)
{//返回树的高度
  int l_depth,r_depth;
  if(ptr_bitree==NULL)
  {//树空,深度为0
    return 0;
  }
  else
  {
    if(ptr_bitree->lchild)
    {//左子树不空
      l_depth=BinaryTreeDepth(ptr_bitree->lchild);
    }
    else
    {//左子树为空
      l_depth=0;
    }
    if(ptr_bitree->rchild)
    {//右子树不空
      r_depth=BinaryTreeDepth(ptr_bitree->rchild);
    }
    else
    {//左子树为空
      r_depth=0;
    }
    return (l_depth>r_depth ? l_depth+1 : r_depth+1);
  }
}

elemType BinaryTreeRoot(ptr_BiTNode ptr_bitree)
{//获取树的根节点的值
  if(ptr_bitree)
  {
    return ptr_bitree->data;
  }
  else
  {
    printf("The binary_tree is empty.\n");
  }
}

elemType Value(ptr_BiTNode ptr_bitree,ptr_BiTNode ptr_node)
{//ptr_node是二叉树中某个结点指针,返回该结点的值
  if(ptr_bitree)
  {//树非空
    if(ptr_node)
    {//结点在树中
      return ptr_node->data;
    }
    else
    {//结点不在树中
      printf("The node is not in the binary_tree.\n");
    }
  }
  else
  {//树空
    printf("The binary_tree is empty.\n");
  }
}

ptr_BiTNode Ptr_value(ptr_BiTNode ptr_bitree,elemType value)
{//二叉树存在,value是树中某结点的值,返回指向该结点的指针
  ptr_BiTNode ptr_node;
  if(ptr_bitree)
  {//树非空
    if(ptr_bitree->data==value)
    {//检查树根
      return ptr_bitree;
    }
    else
    {
      if(ptr_node=Ptr_value(ptr_bitree->lchild,value))
      {//检查左子树
        return ptr_node;
      }
      if(ptr_node=Ptr_value(ptr_bitree->rchild,value))
      {//检查右子树
        return ptr_node;
      }
    }
    return NULL;//树中不存在值为value的结点
  }
  else
  {
    return NULL;
  }
}

void Assign(ptr_BiTNode ptr_bitree,elemType elem,elemType value)
{//树存在,树中存在某一结点值为elem,将该结点赋值为value
  ptr_BiTNode ptr_node;
  ptr_node=Ptr_value(ptr_bitree,elem);//获取值为elem的结点指针
  if(ptr_node)
  {//树中存在值为elem的结点
    ptr_node->data=value;
  }
  else
  {
    printf("The node doesn't in the binary_tree.\n");
  }
}

elemType Parent(ptr_BiTNode ptr_bitree,elemType elem)
{//树不空,elem是树中某结点值,返回该结点的父结点
  ptr_BiTNode ptr_node;
  ptr_BiTNode queue[MAX_QUEUE_SIZE];
  int front=0;
  int rear=0;
  if(ptr_bitree)
  {//树非空,根结点指针入队列
    queue[rear]=ptr_bitree;
    rear=(rear+1)%MAX_QUEUE_SIZE;
    if(ptr_bitree->data==elem)
    {//根节点没有父节点
      printf("The root doesn't have a parent.\n");
      return NULL;
    }
  }
  while(front!=rear)
  {
    ptr_node=queue[front];
    front=(front+1)%MAX_QUEUE_SIZE;
    if(ptr_node->lchild && ptr_node->lchild->data==elem || ptr_node->rchild && ptr_node->rchild->data==elem)
    {//左或右孩子结点值为elem,返回父节点
      return ptr_node->data;
    }
    else
    {//左右孩子的结点值不为elem
      if(ptr_node->lchild)
      {//左孩子结点入队列
        queue[rear]=ptr_node->lchild;
        rear=(rear+1)%MAX_QUEUE_SIZE;
      }
      if(ptr_node->rchild)
      {//右孩子结点入队列
        queue[rear]=ptr_node->rchild;
        rear=(rear+1)%MAX_QUEUE_SIZE;
      }
    }
  }
}

elemType LeftChild(ptr_BiTNode ptr_bitree,elemType elem)
{//树非空,elem是树中某结点值,返回该结点的左孩子
  ptr_BiTNode ptr_node;
  if(ptr_bitree)
  {//树非空
    ptr_node=Ptr_value(ptr_bitree,elem);//得到值为elem的结点指针
    if(ptr_node && ptr_node->lchild)
    {
      return ptr_node->lchild->data;
    }
    else
    {
      return NULL;
    }
  }
}

elemType RightChild(ptr_BiTNode ptr_bitree,elemType elem)
{//树非空,elem是树中某结点值,返回该结点的右孩子
  ptr_BiTNode ptr_node;
  if(ptr_bitree)
  {//树非空
    ptr_node=Ptr_value(ptr_bitree,elem);//得到值为elem的结点指针
    if(ptr_node && ptr_node->rchild)
    {
      return ptr_node->rchild->data;
    }
    else
    {
      return NULL;
    }
  }
}

elemType LeftSibling(ptr_BiTNode ptr_bitree,elemType elem)
{//树非空,elem为树中某结点的值,返回该结点的左兄弟
  elemType parent_value;
  ptr_BiTNode ptr_node;
  if(ptr_bitree)
  {//树非空
    parent_value=Parent(ptr_bitree,elem);
    ptr_node=Ptr_value(ptr_bitree,parent_value);
    if(ptr_node->lchild && ptr_node->rchild && ptr_node->rchild->data==elem)
    {
      return ptr_node->lchild->data;
    }
    else
    {
      return NULL;
    }
  }
}

elemType RightSibling(ptr_BiTNode ptr_bitree,elemType elem)
{//树非空,elem为树中某结点的值,返回该结点的右兄弟
  elemType parent_value;
  ptr_BiTNode ptr_node;
  if(ptr_bitree)
  {//树非空
    parent_value=Parent(ptr_bitree,elem);
    ptr_node=Ptr_value(ptr_bitree,parent_value);
    if(ptr_node->lchild && ptr_node->rchild && ptr_node->lchild->data==elem)
    {
      return ptr_node->rchild->data;
    }
    else
    {
      return NULL;
    }
  }
}

bool InsertChild(ptr_BiTNode p,int LR,ptr_BiTNode c)
{//p为树中某结点指针,LR(0:左子树,1:右子树),c指向要插入的树(c是一棵右子树为空的树)
  //根据LR,确定将C所指向的树插入为p的左子树还是右子树,
  if(p)
  {
    if(LR==0)
    {//插入左子树
      c->rchild=p->lchild;
      p->lchild=c;
    }
    else
    {//插入右子树
      c->rchild=p->rchild;
      p->rchild=c;
    }
    return true;
  }
  return false;
}

bool DeleteChild(ptr_BiTNode p,int LR)
{//p指向树中某结点,根据LR(0:左子树,1:右子树)的值决定删除P所指结点的左子树还是右子树
  if(p)
  {
    if(LR==0)
    {//删除左子树
      DestroyBinaryTree(&(p->lchild));
    }
    else
    {//删除右子树
      DestroyBinaryTree(&(p->rchild));
    }
    return true;
  }
  return false;
}


bool print(elemType elem)
{
  printf("%2c",elem);
  return true;
}

bool PreOrderTraverse(ptr_BiTNode bitree,bool (* visit)(elemType elem))
{//前序遍历
  if(bitree)
  {
    if(visit(bitree->data))
    {
      if(PreOrderTraverse(bitree->lchild,visit))
      {
        if(PreOrderTraverse(bitree->rchild,visit))
        {
          return true;
        }
      }
    }
    return false;
  }
  else
  {
    return true;
  }
}

bool InOrderTraverse(ptr_BiTNode bitree,bool (* visit)(elemType elem))
{//中序遍历
  if(bitree)
  {
    if(InOrderTraverse(bitree->lchild,visit))
    {
      if(visit(bitree->data))
      {
        if(InOrderTraverse(bitree->rchild,visit))
        {
          return true;
        }
      }
    }
    return false;
  }
  else
  {
    return true;
  }
}

bool PostOrderTraverse(ptr_BiTNode bitree,bool (* visit)(elemType elem))
{//后序遍历
  if(bitree)
  {
    if(PostOrderTraverse(bitree->lchild,visit))
    {
      if(PostOrderTraverse(bitree->rchild,visit))
      {
        if(visit(bitree->data))
        {
          return true;
        }
      }
    }
    return false;
  }
  else
  {
    return true;
  }
}

bool LevelOrderTraverse(ptr_BiTNode bitree,bool (* visit)(elemType elem))
{//层次遍历
  ptr_BiTNode ptr_node;
  ptr_BiTNode queue[MAX_QUEUE_SIZE];
  int front=0;
  int rear=0;
  if(bitree)
  {//如果根指针不空,根指针入队列
    queue[rear]=bitree;
    rear=(rear+1)%MAX_QUEUE_SIZE;
    while(front!=rear)
    {//队列不空,队首元素出队
      ptr_node=queue[front];
      front=(front+1)%MAX_QUEUE_SIZE;
      visit(ptr_node->data);
      if(ptr_node->lchild)
      {//左孩子指针入队列
        queue[rear]=ptr_node->lchild;
        rear=(rear+1)%MAX_QUEUE_SIZE;
      }
      if(ptr_node->rchild)
      {//右孩子指针入队列
        queue[rear]=ptr_node->rchild;
        rear=(rear+1)%MAX_QUEUE_SIZE;
      }    
    }
    return true;
  }
  else
  {
    return false;
  }
}
    测试代码:
   
#include "stdafx.h"
#include "binary_tree.h"
#include <conio.h>

int _tmain(int argc, _TCHAR* argv[])
{
  elemType root;
  elemType parent;
  elemType l_child;
  elemType r_child;
  elemType value1;
  elemType value2;
  elemType l_sibling;
  elemType r_sibling;
  int depth;
  ptr_BiTNode ptr_node;
  ptr_BiTNode p;
  ptr_BiTNode c;
  ptr_BiTNode ptr_bitree;
  InitBinaryTree(&ptr_bitree);
  if(IsBinaryTreeEmpty(ptr_bitree))
  {
    printf("The binary_tree is empty.\n");
  }
  else
  {
    printf("The binary_tree is not empty.\n");
  }

  printf("Let's create a new binary_tree.\nPlease enter the value of the binary_tree by pre_order.\n");
  CreateBinaryTree(&ptr_bitree);

  if(IsBinaryTreeEmpty(ptr_bitree))
  {
    printf("The binary_tree is empty.\n");
  }
  else
  {
    printf("The binary_tree is not empty.\n");
  }

  if(ptr_bitree)
  {
    printf("We have created a new binary_tree.\n");
  }

  printf("the PreOrderTraverse of the binary_tree:\n");
  if(PreOrderTraverse(ptr_bitree,print))
  {
    printf("\n");
  }

  printf("the InOrderTraverse of the binary_tree:\n");
  if(InOrderTraverse(ptr_bitree,print))
  {
    printf("\n");
  }

  printf("the PostOrderTraverse of the binary_tree:\n");
  if(PostOrderTraverse(ptr_bitree,print))
  {
    printf("\n");
  }

  root=BinaryTreeRoot(ptr_bitree);
  printf("The root of the binary_tree is:%c\n",root);

  depth=BinaryTreeDepth(ptr_bitree);
  printf("The depth of the binary_tree is:%d\n",depth);

  ptr_node=Ptr_value(ptr_bitree,'F');
  value1=Value(ptr_bitree,ptr_node);
  Assign(ptr_bitree,value1,'M');
  value2=Value(ptr_bitree,ptr_node);
  printf("%c after assign is %c\n",value1,value2);

  printf("the PostOrderTraverse of the binary_tree:\n");
  if(PostOrderTraverse(ptr_bitree,print))
  {
    printf("\n");
  }

  l_child=LeftChild(ptr_bitree,'B');
  if(l_child)
  {
    printf("The left_child of B is:%c\n",l_child);
  }
  else
  {
    printf("B doesn't have a left_child.\n");
  }

  r_child=RightChild(ptr_bitree,'E');
  if(r_child)
  {
    printf("The right_child of E is:%c\n",r_child);
  }
  else
  {
    printf("E doesn't have a right_child.\n");
  }

  parent=Parent(ptr_bitree,'G');
  if(parent)
  {
    printf("The parent of G is:%c\n",parent);
  }

  parent=Parent(ptr_bitree,'A');
  if(parent)
  {
    printf("The parent of A is:%c\n",parent);
  }
    
  l_sibling=LeftSibling(ptr_bitree,'C');
  if(l_sibling)
  {
    printf("The left_sibling of C is:%c\n",l_sibling);
  }
  else
  {
    printf("C doesn't have a left_sibling.\n");
  }

  r_sibling=RightSibling(ptr_bitree,'C');
  if(r_sibling)
  {
    printf("The right_sibling of C is:%c\n",r_sibling);
  }
  else
  {
    printf("C doesn't have a right_sibling.\n");
  }

  getchar();
  InitBinaryTree(&c);
  printf("Please enter the value of the binary_tree.\n");
  CreateBinaryTree(&c);
  if(c)
  {
    printf("We have created a inserted tree without right_child.\n");
  }

  depth=BinaryTreeDepth(c);
  printf("The depth of the binary_tree c is:%d\n",depth);

  p=Ptr_value(ptr_bitree,'D');
  if(InsertChild(p,0,c))
  {
    printf("Insert successfully.\n");
  }
  printf("the PostOrderTraverse of the binary_tree:\n");
  if(PostOrderTraverse(ptr_bitree,print))
  {
    printf("\n");
  }

  p=Ptr_value(ptr_bitree,'I');
  if(DeleteChild(p,0))
  {
    printf("Delete_Child successfully.\n");
  }
  printf("the InOrderTraverse of the binary_tree:\n");
  if(InOrderTraverse(ptr_bitree,print))
  {
    printf("\n");
  }

  printf("the LevelOrderTraverse of the binary_tree:\n");
  if(LevelOrderTraverse(ptr_bitree,print))
  {
    printf("\n");
  }
  getch();
  return 0;
}