网上一边参考一边写的二叉树
#include <stdio.h>#include <stdlib.h>
#include <malloc.h>
#define OK 1
#define ERROR 0
typedef struct Node
{
struct Node *lc;
struct Node *rc;
int data;
}BiNode,*BiTree;
/*Init a null bitree*/
BiTree InitBiTree(BiNode *node)
{
BiTree tree = node;
return tree;
}
/*make node*/
BiTree MakeNode(int temp,BiNode *left,BiNode *right)
{
BiTree p = (BiTree)malloc((sizeof(BiNode)));
if(p)
{
p->data = temp;
p->lc = left;
p->rc = right;
}
return p;
}
/*free Node*/
unsigned char FreeNode(BiTree p)
{
if(p)
{
free(p);
return OK;
}
return ERROR;
}
/*clear a BiTree*/
void ClearBiTree(BiTree tree)
{
BiTree p = tree;
if(p->lc != NULL)
ClearBiTree(p->lc);
if(p->rc != NULL)
ClearBiTree(p->rc);
free(p);
}
/*Judge Tree NULL?*/
unsigned char StaictionTree(BiTree tree)
{
if(tree)
return OK;
else
return ERROR;
}
/*is leaf node*/
unsigned char IsLeafNode(BiTree node)
{
if(node->lc == NULL && node->rc == NULL)
return OK;
else
return ERROR;
}
/*Get the Tree Depth*/
unsigned char GetDepth(BiTree node)
{
unsigned char l=0,r=0,max=0;
BiTree p = node;
if(p)
{
l = GetDepth(p->lc);
r = GetDepth(p->rc);
max = (l > r ? l : r);
return max+1;
}
else
return 0;
}
/*Get Node Data*/
int GetNodeData(BiTree node)
{
if(node)
{
return node->data;
}
return ERROR;
}
/*set node data*/
unsigned char SetNodeData(int a,BiTree node)
{
if(node)
{
node->data = a;
node->lc = NULL;
node->rc = NULL;
return OK;
}
return ERROR;
}
/*Set LeftChard and Right Chaild*/
unsigned char SetLeftChild(BiTree node,BiTree l)
{
if(node && l)
{
node -> lc = l;
return OK;
}
return ERROR;
}
unsigned char SetRightChild(BiTree node,BiTree r)
{
if(node && r)
{
node -> rc = r;
return OK;
}
return ERROR;
}
/*Set LeftChard and Right Chaild*/
/*Insert child,attention the next 2 level*/
BiTree InsertChild(BiTree parent,BiTree child,char location)
{
BiTree node;
if(parent && child)
{
if(IsLeafNode(parent))
{
if(location == 0)
{
parent -> lc = child;
return child;
}else
{
parent -> rc = child;
return child;
}
}else
{
if(location == 0)
{
node = parent -> lc;
parent -> lc = child;
child -> lc = node;
return child->lc;
}else
{
node = parent -> rc;
parent -> rc = child;
child -> rc = node;
return child->rc;
}
}
}
return parent;
}
/*delete leaf node*/
unsigned char DeleteNode(BiTree node,char loc)
{
if(!IsLeafNode(node))
{
if(loc == 0)
free(node->lc);
else
free(node->rc);
return OK;
}
return ERROR;
}
/*printf the node number*/
void visit(int num)
{
printf(" -%d- ",num);
}
/* Mid Left Right*/
void PreOrderTravel(BiTree node)
{
BiTree p = node;
if(p)
{
visit(p->data);
PreOrderTravel(p->lc);
PreOrderTravel(p->rc);
}
}
/*Left Mid Right*/
void MidTravel(BiTree node)
{
if(node)
{
MidTravel(node->lc);
visit(node->data);
MidTravel(node->rc);
}
}
/*Left Mid Right*/
void LastTravel(BiTree node)
{
if(node)
{
LastTravel(node->lc);
LastTravel(node->rc);
visit(node->data);
}
}
void main()
{
BiNode n4,n5,n6,n7,n8,n9;
BiTree n1 = MakeNode(10,NULL,NULL);
BiTree n2 = MakeNode(20,NULL,NULL);
BiTree n3 = MakeNode(100,n1,n2);
SetNodeData(11,&n4);
SetNodeData(12,&n5);
SetNodeData(13,&n6);
SetNodeData(14,&n7);
SetNodeData(33,&n8);
SetNodeData(44,&n9);
SetLeftChild(n1,&n4);
SetRightChild(n1,&n5);
SetLeftChild(n2,&n6);
SetRightChild(n2,&n7);
InsertChild(n1,&n8,0);
InsertChild(&n5,&n9,1);
printf("The Depth of the tree is %d\n",GetDepth(n3));
PreOrderTravel(n3);
//LastTravel(n3);
}