#include<stdio.h>
#include<stdlib.h>
#define MAXLen 100 //最大節點數
typedef char DATA;//元素類型
typedef struct CBT
{
DATA data;
struct CBT *left;//左子樹節點指標
struct CBT *right;//右子樹節點指標
}CBTType;
CBTType *InitTree()//初始化二叉樹根
{
CBTType *node;
if(node=(CBTType*)malloc(sizeof(CBTType)))
{
printf("請先輸入一個根節點資料:\n");
scanf("%s",&node->data);
node->left=NULL;
node->right=NULL;
if(node!=NULL)
{
return node;
}else
{
return NULL;
}
}
return NULL;
}
CBTType *TreeFindNode(CBTType *treeNode,DATA data)
{
CBTType *ptr;
if(treeNode==NULL)
{
return NULL;
}
else
{
if(treeNode->data==data)
{
return treeNode;
}
else
{
if(ptr==TreeFindNode(treeNode->left,data))
{
return ptr;
}
else if(ptr==TreeFindNode(treeNode->right,data))
{
return ptr;
}else
{
return NULL;
}
}
}
}
void AddTreeNode(CBTType *treeNode)
{
CBTType *pnode,*parent;
DATA data;
char menusel;
if(pnode=(CBTType *)malloc(sizeof(CBTType)))
{
printf("輸入二叉樹節點資料:\n");
scanf("%s",&pnode->data);
pnode->left=NULL;
pnode->right=NULL;
printf("輸入該節點的父節點資料:\n");
scanf("%s",&data);
parent=TreeFindNode(treeNode,data);
if(!parent)
{
printf("未找到該父節點 !\n");
free(pnode);
return;
}
printf(" 1.添加該節點的左子樹\n2.添加該節點的右子樹\n");
do
{
menusel=getchar();
menusel-='0';
if(menusel==1||menusel==2)
{
if(parent==NULL)
{
printf("不存在父節點,請先設定父節點!\n");
}
else
{
switch(menusel)
{
case 1:
if(parent->left)
{
printf("左子樹節點不為空白!\n");
}
else
{
parent->left=pnode;
}
break;
case 2:
if(parent->right)
{
printf("右子樹節點不為空白!\n");
}
else
{
parent->right=pnode;
}
break;
default:
printf("無效參數!\n");
}
}
}
}while(menusel!=1&&menusel!=2);
}
}
CBTType *TreeLeftNode(CBTType *treeNode)
{
if(treeNode)
{
return treeNode->left;
}else
{
return NULL;
}
}
CBTType *TreeRightNode(CBTType *treeNode)
{
if(treeNode)
{
return treeNode->right;
}else
{
return NULL;
}
}
int TreeIsEmpty(CBTType* treeNode)
{
if(treeNode)
{
return 0;
}
else
{
return 1;
}
}
int TreeDepth(CBTType *treeNode)
{
int depleft,depright;
if(treeNode==NULL)
{
return 0;
}
else
{
depleft=TreeDepth(treeNode->left);
depright=TreeDepth(treeNode->right);
if(depleft>depright)
{
return depleft+1;
}
else
{
return depright+1;
}
}
}
void ClearTree(CBTType *treeNode)
{
if(treeNode)
{
ClearTree(treeNode->left);
ClearTree(treeNode->right);
free(treeNode);
treeNode=NULL;
}
}
void TreeNodeData(CBTType *p)
{
printf("%c",p->data);
}
void LevelTree(CBTType *treeNode,void(*TreeNodeData)(CBTType *p))
{
CBTType *p;
CBTType *q[MAXLen];
int head=0,tail=0;
if(treeNode)
{
tail=(tail+1)%MAXLen;
q[tail]=treeNode;
}
while(head!=tail)
{
head=(head+1)%MAXLen;
p=q[head];
TreeNodeData(p);
if(p->left)
{
tail=(tail+1)%MAXLen;
q[tail]=p->left;
}
if(p->right)
{
tail=(tail+1)%MAXLen;
q[tail]=p->right;
}
}
}
void DLRTree(CBTType *treeNode,void(*TreeNodeData)(CBTType *p))
{
if(treeNode)
{
TreeNodeData(treeNode);
DLRTree(treeNode->left,TreeNodeData);
DLRTree(treeNode->right,TreeNodeData);
}
}
void LDRTree(CBTType *treeNode,void(*TreeNodeData)(CBTType *p))
{
if(treeNode)
{
LDRTree(treeNode->left,TreeNodeData);
TreeNodeData(treeNode);
LDRTree(treeNode->right,TreeNodeData);
}
}
void RDLTree(CBTType *treeNode,void(*TreeNodeData)(CBTType *p))
{
if(treeNode)
{
RDLTree(treeNode->right,TreeNodeData);
TreeNodeData(treeNode);
RDLTree(treeNode->left,TreeNodeData);
}
}