C语言二叉树的建立?完整程序?谢谢!!!

撰写一个程序,能够构建字符串型的二叉排序树并在二叉排序树中查找节点。

所谓二叉排序树,简而言之,是一个每个节点可指向 0、1 或 2 个节点的递归的数据结构。最上层的一个节点称为树根。二叉排序树服从凡是比当前节点小的值都在其左下方,比当前节点大的值都在其右下方的规律。该规律不仅适用于树的局部,也适用于整棵二叉排序树。

例如,在给定 Kiwi, Banana, Apple, Melon, Berry 的输入字符串的顺序下,以此构建二叉排序树的过程为:

建树后,请根据给定的字符串,查找树中是否有该节点。若有,返回其节点在所在的层次数(根节点Kiwi的层次定义为0);若无,返回“Not Found”。

输入:
每行一个字符串,作为要插入二叉排序树的值。若单独一个*字符占一行,则表明用于建树的字符串输入数据结束。随后以[Search]开始的若干行,代表要在树中查找某个字符串值,用于查找的字符串紧跟其后。
若输入行的第一个字符为“#”,则表示输入数据结束。

输出:
输出每个要查找的值所在的层次数。

测试输入 期待的输出 时间限制 内存限制 额外进程
测试用例 1 以文本方式显示 Kiwi↵
Banana↵
Apple↵
Melon↵
Berry↵
*↵
[Search]Berry↵
[Search]Strawberry↵
#↵
以文本方式显示 Berry-2↵
Strawberry-Not Found↵
1秒 64M 0
测试用例 2 以文本方式显示 test↵
*↵
[Search]test↵
[Search]text↵
#↵
以文本方式显示 test-0↵
text-Not Found↵
1秒 64M 0

完整程序自己写吧,我提供个思路:
二叉树的节点包含三个指针:左指针,右指针,字符串链表的头指针;
构造一个通过两个字符串的头指针比较字符窜大小的函数;(先比较串长,再从头对位开始比较);
构造一个插入函数(递归)
最后查找函数(递归)返回查找元素距根的距离;
温馨提示:答案为网友推荐,仅供参考
第1个回答  2011-06-15
C语言二叉树的建立?完整程序?谢谢!!!
d.cpp
C:\Documents and Settings\Administrator\桌面\dsd.cpp(21) : error C2664: 'Init' : cannot convert parameter 1 from 'struct node *' to 'Tree *'
Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
C:\Documents and Settings\Administrator\桌面\dsd.cpp(22) : error C2664: 'Init' : cannot convert parameter 1 from 'struct node *' to 'Tree *'
Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
C:\Documents and Settings\Administrator\桌面\dsd.cpp(31) : error C2664: 'Xbl' : cannot convert parameter 1 from 'struct node *' to 'Tree *'
Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
C:\Documents and Settings\Administrator\桌面\dsd.cpp(32) : error C2664: 'Xbl' : cannot convert parameter 1 from 'struct node *' to 'Tree *'
Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
执行 cl.exe 时出错.

dsd.obj - 1 error(s), 0 warning(s)
VC6 .0报错!本回答被网友采纳