谁会做道二叉树作业

q135069 2013-05-17 08:30:56
void InOrderBinaryTree(bitree *root)
{
bitree *p;
bitree *s[MAX_STACK_LEVEL];
int top = 0;
short int IsStop;
p = root;
IsStop = FALSE;
while (! IsStop)
{
while ( (p != NULL) && (! IsStop) )
{
if (top < MAX_STACK_LEVEL-1)
{
top++;
s[top] = p;
p = p -> lchild;
}
else {
printf("Stack overflow !\n");
IsStop = TRUE;
}
}
if (! IsStop)
{
if (top != 0)
{
p = s[top];
printf("%c ", p -> data);

top--;
p = p -> rchild;
}
else
IsStop = TRUE;
}
}
}

// 二叉树后序遍历 - 递归版本
void PostOrderRetrieveBinaryTree(bitree *root)
{
// [上机作业 1] - 自己上机实现 ...}

void PostOrderBinaryTree(bitree *root)
{
bitree *p;
bitree *pre = NULL;
bitree *s[MAX_STACK_LEVEL];
int top = 0;
p = root;
while ( (top != 0) || (p != NULL) )
{
while(p != NULL)
{
if (top < MAX_STACK_LEVEL-1)
{
top++;
s[top] = p;
p = p -> lchild;
}
else
{
printf("Stack overflow !\n");
return;
}
}

if(top != 0)
{
p = s[top];
if ( (p -> rchild == NULL) || (p -> rchild == pre) )
{
printf("%c ", p -> data);

top--;
pre = p;
p = NULL;
}
else {
p = p -> rchild;
}
}
}
}
void LevelOrderBinaryTree(bitree *root)
{
bitree *p;
bitree *q[MAX_QUEUE_LENGTH];
int front = MAX_QUEUE_LENGTH - 1;
int rear = MAX_QUEUE_LENGTH - 1;
short int IsStop;
p = root;
IsStop = FALSE;
while (! IsStop)
{
if (p != NULL)
{
printf("%c ", p -> data);
if (p -> lchild != NULL)
{
if ( ((rear + 1) % MAX_QUEUE_LENGTH) == front )
{
printf("Queue full !\n");
IsStop = TRUE;
}
else
{
rear = (rear + 1) % MAX_QUEUE_LENGTH;
q[rear] = p -> lchild;
};
}
if (p -> rchild != NULL)
{
if ( ((rear + 1) % MAX_QUEUE_LENGTH) == front )
{
printf("Queue full !\n");
IsStop = TRUE;
}
else
{
rear = (rear + 1) % MAX_QUEUE_LENGTH;
q[rear] = p -> rchild;
};
}
}
if (rear != front)
{
p = q[(front + 1) % MAX_QUEUE_LENGTH];
front = (front + 1) % MAX_QUEUE_LENGTH;
}
else
IsStop = TRUE;
}
}

bitree *FindTargetNode(bitree *root, char targetdata)
{
bitree *p;
bitree *T;
bitree *s[MAX_STACK_LEVEL];
int top = 0;
short int IsStop;
p = root;
IsStop = FALSE;
while (! IsStop)
{
while ( (p != NULL) && (! IsStop) )
{
if (p -> data == targetdata)
{
T = p;
IsStop = TRUE;
}
else
{
if (top < MAX_STACK_LEVEL-1) {
top++;
s[top] = p;
p = p -> lchild;
}
else
{
printf("Stack overflow !\n");
T = NULL;
IsStop = TRUE;
}
}
}
if (! IsStop)
{
if (top != 0)
{
p = s[top];
top--;
p = p -> rchild;
}
else
{
T = NULL;
IsStop = TRUE;
}
}
}

return T;
}
bitree *FindTargetNode(bitree *root, enum BinaryTreeNodeTypeSet NodeType)
{
bitree *p;
bitree *T;
bitree *s[MAX_STACK_LEVEL];
int top = 0;
short int IsStop;
short int b;

p = root;
IsStop = FALSE;
while (! IsStop)
{
while ( (p != NULL) && (! IsStop) )
{
switch(NodeType)
{
case HAS_NO_CHILD :
b = ( (p -> lchild == NULL) && (p -> rchild == NULL) );

break;

case HAS_NO_LEFTCHILD :
b = ( (p -> lchild == NULL) );

break;

case HAS_NO_RIGHTCHILD :
b = ((p -> rchild == NULL) );

break;

default :
break;
}

if (b)
{
T = p;
IsStop = TRUE;
}
else
{
if (top < MAX_STACK_LEVEL-1)
{
top++;
s[top] = p;
p = p -> lchild;
}
else
{
printf("Stack overflow !\n");
T = NULL;
IsStop = TRUE;
}
}
}

if (! IsStop)
{
if (top != 0)
{
p = s[top];
top--;
p = p -> rchild;
}
else
{
T = NULL;
IsStop = TRUE;
}
}
}

return T;
}

void AddChild(bitree *root, char newdata, enum BinaryTreeNodeTypeSet NodeType)
{
bitree *T;
bitree *p;
T = FindTargetNode(root, NodeType);
if (T != NULL)
{
p = new bitree;
p -> lchild = NULL;
p -> data = newdata;
p -> rchild = NULL;

switch (NodeType)
{
case HAS_NO_CHILD :
T -> lchild = p;

break;

case HAS_NO_LEFTCHILD :
T -> lchild = p;

break;

case HAS_NO_RIGHTCHILD :
T -> rchild = p;

break;

default :
break;
}
}
}
void AddLeftChild(bitree *root, char targetdata, char newdata)
{
bitree *T;
bitree *p;
T = FindTargetNode(root, targetdata);
if (T == NULL) // 未找到 targetdata,则生成一个新节点 ...
{
AddChild(root, newdata, HAS_NO_LEFTCHILD);
}
else
{
p = new bitree;
p -> lchild = NULL;
p -> data = newdata;
p -> rchild = NULL;

T -> lchild = p;
};
}

// 将值 newdata 作为值为 targetdata 的节点的右孩子插入 ...
void AddRightChild(bitree *root, char targetdata, char newdata)
{

// [上机作业 2-1] - 自己上机实现 ...

}

// 删除值为 targetdata 的节点的左孩子 ...
// 注意:为方便起见,将删除该节点的左孩子及左孩子的所有子节点 ...
void DeleteLeftChild(bitree *root, char targetdata)
{
bitree *T;

// 先寻找该节点指针 ...
T = FindTargetNode(root, targetdata);
if (T != NULL)
{

DestroyBinaryTree(T -> lchild);
T -> lchild = NULL;
}
}

// 删除值为 targetdata 的节点的右孩子 ...
void DeleteRightChild(bitree *root, char targetdata)
{

// [上机作业 2-2] - 自己上机实现 ...

}

// 统计二叉树中叶子节点个数 ...
// 下面的 'int &count' 使用了 C++ 中的“引用”语法,详见课堂 ppt ...
void CountBinaryTreeLeafNodeNum(bitree *root, int &count)
{
if (root == NULL)
return;
else
{
if ( (root -> lchild == NULL) && (root -> rchild == NULL) )
{
count++;
return;
}
else
{
CountBinaryTreeLeafNodeNum(root -> lchild, count);
CountBinaryTreeLeafNodeNum(root -> rchild, count);
}
}
}

// [上机作业 3] - 从下面的任务中任意选一题完成,包括设计函数头、参数表和具体的代码设计 ...
//
// (3-1) - 给定一个数据值,计算包含该数据值的节点的度。
//
// (3-2) - 给定一个数据值,打印输出包含该数据值的节点的祖先节点。
//
// (3-3) - 给定一个数据值,打印输出包含该数据值的节点的兄弟节点。
//
// (3-4) - 给定一个数据值,计算包含该数据值的节点的层数。
//
// (3-5) - 计算二叉树的高度。
//
// (3-6) - 计算二叉树的度。
//
// (3-7) - 输入参数为根指针,判断给定的二叉树是否为满二叉树。


void main()
{
char ch;

FILE *fp;
char *filename = "BinaryTreeData.txt"; ...

bitree *root;

int LeafNodeNum;
int &count = LeafNodeNum;
printf("**********************************\n");
printf("! Menu Selection !\n");
printf("! !\n");
printf("! 1) Input from file !\n");
printf("! 2) Input from keyboard !\n");
printf("! 0) Exit !\n");
printf("! !\n");
printf("**********************************\n");
printf("Your choice[1/2/0] : ");
do
{
ch = getchar();
}
while ( (ch != '0') && (ch != '1') && (ch != '2') );

switch (ch)
{
case '1' : // 从文件输入
if ((fp = fopen(filename, "r")) == NULL)
{
printf("Error open binary tree data file !\n");
return;
}

root = CreateBinaryTree(fp);

fclose(fp);

break;

case '2' : // 从键盘输入
printf("Input data to create a binary tree :\n");
root = CreateBinaryTree();

break;

case '0' : // 退出
return;

break;

default :
return;

break;
}

printf("\nThe pre-order is : \n");
//PreOrderRetrieveBinaryTree(root);
PreOrderBinaryTree(root);

// 中序 ...
printf("\nThe in-order is : \n");
//InOrderRetrieveBinaryTree(root);
InOrderBinaryTree(root);

// 后序 ...
printf("\nThe post-order is : \n");
//PostOrderRetrieveBinaryTree(root);
PostOrderBinaryTree(root);

// 层序 ...
printf("\nThe level-order is : \n");
LevelOrderBinaryTree(root);

// 输出叶子节点的个数 ...
LeafNodeNum = 0; // 变量清 0 ...
CountBinaryTreeLeafNodeNum(root, count);
printf("\nThe number of leaf node is : %d\n", LeafNodeNum);
AddLeftChild(root, '2', '7');

printf("\nAfter [ADD] operation, The pre-order is : \n");
PreOrderRetrieveBinaryTree(root);
//PreOrderBinaryTree(root);

printf("\nAfter [ADD] operation, The post-order is : \n");
//PostOrderRetrieveBinaryTree(root);
PostOrderBinaryTree(root);

printf("\nAfter [ADD] operation, The level-order is : \n");
LevelOrderRetrieveBinaryTree(root);

// 删除子节点 ...
DeleteLeftChild(root, '3');

printf("\nAfter [DELETE] operation, The pre-order is : \n");
PreOrderRetrieveBinaryTree(root);
//PreOrderBinaryTree(root);

printf("\nAfter [DELETE] operation, The post-order is : \n");
//PostOrderRetrieveBinaryTree(root);
PostOrderBinaryTree(root);

printf("\nAfter [DELETE] operation, The level-order is : \n");
LevelOrderRetrieveBinaryTree(root);

// 输出叶子节点的个数 ...
LeafNodeNum = 0; // 变量清 0 ...
CountBinaryTreeLeafNodeNum(root, count);
printf("\nThe number of leaf node is : %d\n", LeafNodeNum);
DestroyBinaryTree(root);
}
...全文
14 点赞 收藏 回复
写回复
回复
切换为时间正序
当前发帖距今超过3年,不再开放新的回复
发表回复
相关推荐
发帖
C++ Builder
创建于2007-08-02

1.3w+

社区成员

C++ Builder相关内容讨论区
申请成为版主
帖子事件
创建了帖子
2013-05-17 08:30
社区公告
暂无公告