Description

/已知二叉排序树BiSortTree的部分代码如下,勿改动。
请在此基础上补充实现递归查找算法SearchBST和InsertBST。
/
#include
using namespace std;
//BiNode
template
struct BiNode
{
DataType data;
BiNode lchild, rchild;
};

//BiSortTree
template
class BiSortTree
{
public:
BiSortTree(DataType r[],int n); //建立关键字序列r[0]~r[n-1]的二叉排序树
~BiSortTree( ); //析构函数,释放二叉排序树中所有结点,同二叉链表的析构函数
void InsertBST(BiNode&bt,DataType key); //插入key
BiNode
SearchBST(BiNode bt,DataType key); //查找值为k的结点,返回值为k所在结点的地址
void InOrderBST(BiNode
bt); //中序遍历二叉树(递归)
BiNode *GetRoot();//获取root值

private:
BiNode root; //二叉排序树(即二叉链表)的根指针
void Release(BiNode
&bt); //析构函数调用
};

//构造函数,将r[0]~r[n-1]各个元素依次插入,生成一棵二叉排序树
template
BiSortTree::BiSortTree(DataType r[],int n)//构造函数
{
root=nullptr; //初始化空二叉树
int i;
for(i=0;iInsertBST(root,r[i]);//将结点s插入到二叉排序树中
}
}

//析构函数,调用Release释放内存
template
BiSortTree::~BiSortTree( )
{
Release(root);
}

//释放二叉排序树的存储空间,调用析构函数实现
template
void BiSortTree::Release(BiNode* &bt)
{
if (bt){
Release(bt->lchild); //释放左子树
Release(bt->rchild); //释放右子树
delete bt;
}
}

template
BiNode *BiSortTree::GetRoot()
{
return root;
}

template
void BiSortTree::InOrderBST(BiNode*bt)
{
if(bt==nullptr) return;
else
{
InOrderBST(bt->lchild);
cout<data<<” “;
InOrderBST(bt->rchild);
}
}

//在下面补充实现相关算法

//二叉排序树的主函数
int main( ){
int a[10],n;
n=0;
while(1)
{
cin>>a[n];
if(!a[n])break; //输入数据以0结束
n++;
}
BiSortTree bst(a,n);//构造二叉排序树
BiNode root;
root=bst.GetRoot();
cout<<”Inorder:”;
bst.InOrderBST(root);//中序遍历二叉排序树(得到递增有序序列)
cout<int x;
cin>>x;
BiNode
s;
s=bst.SearchBST(root,x);
if(s==nullptr) cout<<”Find “<else cout<<”Find “<cout<return 0;
}

Input

Output

Sample Input

3 42 4 32 42 42 32 56 37 0 32

Sample Output

Inorder:3 4 32 37 42 56
Find 32 success

HINT

Source

Submit

  1. package Q1031;
  2. import java.util.Scanner;
  3. /**
  4. *
  5. * 二叉链式存储结构下的二叉树结点
  6. *
  7. */
  8. class BiTreeNode {
  9. public Object data;// 结点的数据元素
  10. public BiTreeNode lchild, rchild; // 左右孩子
  11. public BiTreeNode() {// 构造一个空结点
  12. this(null);
  13. }
  14. public BiTreeNode(Object data) {// 构造一棵左右孩子为空的结点
  15. this(data, null, null);
  16. }
  17. public BiTreeNode(Object data, BiTreeNode lchild, BiTreeNode rchild) {// 构造一棵数据元素和左右孩子都不为空的结点
  18. this.data = data;
  19. this.lchild = lchild;
  20. this.rchild = rchild;
  21. }
  22. }
  23. /**
  24. * 顺序表记录结点类
  25. */
  26. class RecordNode {
  27. public Comparable key; // 关键字
  28. public Object element; // 数据元素
  29. /*
  30. public Object getElement() {
  31. return element;
  32. }
  33. public void setElement(Object element) {
  34. this.element = element;
  35. }
  36. public Comparable getKey() {
  37. return key;
  38. }
  39. public void setKey(Comparable key) {
  40. this.key = key;
  41. }
  42. */
  43. public RecordNode() {
  44. }
  45. public RecordNode(Comparable key) { // 构造方法1
  46. this.key = key;
  47. }
  48. public RecordNode(Comparable key, Object element) { // 构造方法2
  49. this.key = key;
  50. this.element = element;
  51. }
  52. public String toString() { // 覆盖toString()方法
  53. return key+"";
  54. }
  55. }
  56. class BSTree { //二叉排序树类
  57. public BiTreeNode root; //根结点
  58. public BSTree() { //构造空二叉排序树
  59. root = null;
  60. }
  61. /*
  62. public BiTreeNode getRoot() {
  63. return root;
  64. }
  65. public void setRoot(BiTreeNode root) {
  66. this.root = root;
  67. }
  68. */
  69. public boolean isEmpty() { //判断是否空二叉树
  70. return this.root == null;
  71. }
  72. public void inOrderTraverse(BiTreeNode p) { //中根次序遍历以p结点为根的二叉树
  73. if (p != null) {
  74. inOrderTraverse(p.lchild);
  75. System.out.print(((RecordNode) p.data).toString() + " ");
  76. inOrderTraverse(p.rchild);
  77. }
  78. }
  79. //查找关键字值为key的结点,若查找成功返回结点值,否则返回null
  80. public Object searchBST(Comparable key) {
  81. if (key == null || !(key instanceof Comparable)) {
  82. return null;
  83. }
  84. return searchBST(root, key);
  85. }
  86. //二叉排序树查找的递归算法
  87. //在二叉排序树中查找关键字为key的结点。若查找成功则返回结点值,否则返回null
  88. private Object searchBST(BiTreeNode p, Comparable key) {
  89. if (p != null) {
  90. if (key.compareTo(((RecordNode) p.data).key) == 0) //查找成功
  91. {
  92. return p.data;
  93. }
  94. // System.out.print(((RecordNode) p.data).key + "? ");
  95. if (key.compareTo(((RecordNode) p.data).key) < 0) {
  96. return searchBST(p.lchild, key); //在左子树中查找
  97. } else {
  98. return searchBST(p.rchild, key); //在右子树中查找
  99. }
  100. }
  101. return null;
  102. }
  103. //在二叉排序树中插入关键字为Key,数据项为theElement的结点,若插入成功返回true,否则返回false
  104. public boolean insertBST(Comparable key, Object theElement) {
  105. if (key == null || !(key instanceof Comparable)) {//不能插入空对象或不可比较大小的对象
  106. return false;
  107. }
  108. if (root == null) {
  109. root = new BiTreeNode(new RecordNode(key, theElement)); //建立根结点
  110. return true;
  111. }
  112. return insertBST(root, key, theElement);
  113. }
  114. //将关键字为key,数据项为theElement的结点插入到以p为根的二叉排序树中的递归算法
  115. private boolean insertBST(BiTreeNode p, Comparable key, Object theElement) {
  116. if (key.compareTo(((RecordNode) p.data).key) == 0) {
  117. return false; //不插入关键字重复的结点
  118. }
  119. if (key.compareTo(((RecordNode) p.data).key) < 0) {
  120. if (p.lchild == null) { //若p的左子树为空
  121. p.lchild=new BiTreeNode(new RecordNode(key, theElement)); //建立叶子结点作为p的左孩子
  122. return true;
  123. } else { //若p的左子树非空
  124. return insertBST(p.lchild, key, theElement); //插入到p的左子树中
  125. }
  126. } else if (p.rchild == null) { //若p的右子树为空
  127. p.rchild=new BiTreeNode(new RecordNode(key, theElement)); //建立叶子结点作为p的右孩子
  128. return true;
  129. } else { //若p的右子树非空
  130. return insertBST(p.rchild, key, theElement); //插入到p的右子树中
  131. }
  132. }
  133. //二叉排序树中删除结点算法。若删除成功返回删除结点值,否则返回null
  134. public Object removeBST(Comparable key) {
  135. if (root == null || key == null || !(key instanceof Comparable)) {
  136. return null;
  137. }
  138. //在以root为根的二叉排序树中删除关键字为elemKey的结点
  139. return removeBST(root, key, null);
  140. }
  141. //在以p为根的二叉排序树中删除关键字为elemKey的结点。parent是p的父结点,递归算法
  142. private Object removeBST(BiTreeNode p, Comparable elemKey, BiTreeNode parent) {
  143. if (p != null) {
  144. if (elemKey.compareTo(((RecordNode) p.data).key) < 0) { //在左子树中删除
  145. return removeBST(p.lchild, elemKey, p); //在左子树中递归搜索
  146. } else if (elemKey.compareTo(((RecordNode) p.data).key) > 0) { //在右子树中删除
  147. return removeBST(p.rchild, elemKey, p); //在右子树中递归搜索
  148. } else if (p.lchild != null && p.rchild != null) {
  149. //相等且该结点有左右子树
  150. BiTreeNode innext = p.rchild; //寻找p在中根次序下的后继结点innext
  151. while (innext.lchild != null) { //即寻找右子树中的最左孩子
  152. innext = innext.lchild;
  153. }
  154. p.data=innext.data; //以后继结点值替换p
  155. return removeBST(p.rchild, ((RecordNode) p.data).key, p); //递归删除结点p
  156. } else {//p是1度和叶子结点
  157. if (parent == null) { //删除根结点,即p==root
  158. if (p.lchild != null) {
  159. root = p.lchild;
  160. } else {
  161. root = p.rchild;
  162. }
  163. return p.data; //返回删除结点p值
  164. }
  165. if (p == parent.lchild) { //p是parent的左孩子
  166. if (p.lchild != null) {
  167. parent.lchild=p.lchild; //以p的左子树填补
  168. } else {
  169. parent.lchild=p.rchild;
  170. }
  171. } else if (p.lchild != null) { //p是parent的右孩子且p的左子树非空
  172. parent.rchild=p.lchild;
  173. } else {
  174. parent.rchild=p.rchild;
  175. }
  176. return p.data;
  177. }
  178. }
  179. return null;
  180. }
  181. }
  182. public class Main {
  183. public static void main(String[] args) {
  184. int temp;
  185. int n;
  186. n = 0;
  187. Scanner scanner = new Scanner(System.in);
  188. BSTree bst = new BSTree();//构造二叉排序树
  189. while(true){
  190. temp = scanner.nextInt();
  191. if(temp == 0)break;
  192. bst.insertBST(temp,n);
  193. n++;
  194. }
  195. System.out.print("Inorder:");
  196. bst.inOrderTraverse(bst.root);//中序遍历二叉排序树(得到递增有序序列)
  197. System.out.println();
  198. int x = scanner.nextInt();;
  199. Object s = bst.searchBST(x);
  200. if (s == null) System.out.println("Find " + x +" failure");
  201. else System.out.println("Find " + x +" success");
  202. }
  203. }