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
BiNode
void InOrderBST(BiNode
BiNode
private:
BiNode
void Release(BiNode
};
//构造函数,将r[0]~r[n-1]各个元素依次插入,生成一棵二叉排序树
template
BiSortTree
{
root=nullptr; //初始化空二叉树
int i;
for(i=0;i
}
}
//析构函数,调用Release释放内存
template
BiSortTree
{
Release(root);
}
//释放二叉排序树的存储空间,调用析构函数实现
template
void BiSortTree
{
if (bt){
Release(bt->lchild); //释放左子树
Release(bt->rchild); //释放右子树
delete bt;
}
}
template
BiNode
{
return root;
}
template
void BiSortTree
{
if(bt==nullptr) return;
else
{
InOrderBST(bt->lchild);
cout<
InOrderBST(bt->rchild);
}
}
//在下面补充实现相关算法
//二叉排序树的主函数
int main( ){
int a[10],n;
n=0;
while(1)
{
cin>>a[n];
if(!a[n])break; //输入数据以0结束
n++;
}
BiSortTree
BiNode
root=bst.GetRoot();
cout<<”Inorder:”;
bst.InOrderBST(root);//中序遍历二叉排序树(得到递增有序序列)
cout<
cin>>x;
BiNode
s=bst.SearchBST(root,x);
if(s==nullptr) cout<<”Find “<
}
Input
Output
Sample Input
Sample Output
Inorder:3 4 32 37 42 56
Find 32 success
HINT
Source
Submit
package Q1031;import java.util.Scanner;/**** 二叉链式存储结构下的二叉树结点**/class BiTreeNode {public Object data;// 结点的数据元素public BiTreeNode lchild, rchild; // 左右孩子public BiTreeNode() {// 构造一个空结点this(null);}public BiTreeNode(Object data) {// 构造一棵左右孩子为空的结点this(data, null, null);}public BiTreeNode(Object data, BiTreeNode lchild, BiTreeNode rchild) {// 构造一棵数据元素和左右孩子都不为空的结点this.data = data;this.lchild = lchild;this.rchild = rchild;}}/*** 顺序表记录结点类*/class RecordNode {public Comparable key; // 关键字public Object element; // 数据元素/*public Object getElement() {return element;}public void setElement(Object element) {this.element = element;}public Comparable getKey() {return key;}public void setKey(Comparable key) {this.key = key;}*/public RecordNode() {}public RecordNode(Comparable key) { // 构造方法1this.key = key;}public RecordNode(Comparable key, Object element) { // 构造方法2this.key = key;this.element = element;}public String toString() { // 覆盖toString()方法return key+"";}}class BSTree { //二叉排序树类public BiTreeNode root; //根结点public BSTree() { //构造空二叉排序树root = null;}/*public BiTreeNode getRoot() {return root;}public void setRoot(BiTreeNode root) {this.root = root;}*/public boolean isEmpty() { //判断是否空二叉树return this.root == null;}public void inOrderTraverse(BiTreeNode p) { //中根次序遍历以p结点为根的二叉树if (p != null) {inOrderTraverse(p.lchild);System.out.print(((RecordNode) p.data).toString() + " ");inOrderTraverse(p.rchild);}}//查找关键字值为key的结点,若查找成功返回结点值,否则返回nullpublic Object searchBST(Comparable key) {if (key == null || !(key instanceof Comparable)) {return null;}return searchBST(root, key);}//二叉排序树查找的递归算法//在二叉排序树中查找关键字为key的结点。若查找成功则返回结点值,否则返回nullprivate Object searchBST(BiTreeNode p, Comparable key) {if (p != null) {if (key.compareTo(((RecordNode) p.data).key) == 0) //查找成功{return p.data;}// System.out.print(((RecordNode) p.data).key + "? ");if (key.compareTo(((RecordNode) p.data).key) < 0) {return searchBST(p.lchild, key); //在左子树中查找} else {return searchBST(p.rchild, key); //在右子树中查找}}return null;}//在二叉排序树中插入关键字为Key,数据项为theElement的结点,若插入成功返回true,否则返回falsepublic boolean insertBST(Comparable key, Object theElement) {if (key == null || !(key instanceof Comparable)) {//不能插入空对象或不可比较大小的对象return false;}if (root == null) {root = new BiTreeNode(new RecordNode(key, theElement)); //建立根结点return true;}return insertBST(root, key, theElement);}//将关键字为key,数据项为theElement的结点插入到以p为根的二叉排序树中的递归算法private boolean insertBST(BiTreeNode p, Comparable key, Object theElement) {if (key.compareTo(((RecordNode) p.data).key) == 0) {return false; //不插入关键字重复的结点}if (key.compareTo(((RecordNode) p.data).key) < 0) {if (p.lchild == null) { //若p的左子树为空p.lchild=new BiTreeNode(new RecordNode(key, theElement)); //建立叶子结点作为p的左孩子return true;} else { //若p的左子树非空return insertBST(p.lchild, key, theElement); //插入到p的左子树中}} else if (p.rchild == null) { //若p的右子树为空p.rchild=new BiTreeNode(new RecordNode(key, theElement)); //建立叶子结点作为p的右孩子return true;} else { //若p的右子树非空return insertBST(p.rchild, key, theElement); //插入到p的右子树中}}//二叉排序树中删除结点算法。若删除成功返回删除结点值,否则返回nullpublic Object removeBST(Comparable key) {if (root == null || key == null || !(key instanceof Comparable)) {return null;}//在以root为根的二叉排序树中删除关键字为elemKey的结点return removeBST(root, key, null);}//在以p为根的二叉排序树中删除关键字为elemKey的结点。parent是p的父结点,递归算法private Object removeBST(BiTreeNode p, Comparable elemKey, BiTreeNode parent) {if (p != null) {if (elemKey.compareTo(((RecordNode) p.data).key) < 0) { //在左子树中删除return removeBST(p.lchild, elemKey, p); //在左子树中递归搜索} else if (elemKey.compareTo(((RecordNode) p.data).key) > 0) { //在右子树中删除return removeBST(p.rchild, elemKey, p); //在右子树中递归搜索} else if (p.lchild != null && p.rchild != null) {//相等且该结点有左右子树BiTreeNode innext = p.rchild; //寻找p在中根次序下的后继结点innextwhile (innext.lchild != null) { //即寻找右子树中的最左孩子innext = innext.lchild;}p.data=innext.data; //以后继结点值替换preturn removeBST(p.rchild, ((RecordNode) p.data).key, p); //递归删除结点p} else {//p是1度和叶子结点if (parent == null) { //删除根结点,即p==rootif (p.lchild != null) {root = p.lchild;} else {root = p.rchild;}return p.data; //返回删除结点p值}if (p == parent.lchild) { //p是parent的左孩子if (p.lchild != null) {parent.lchild=p.lchild; //以p的左子树填补} else {parent.lchild=p.rchild;}} else if (p.lchild != null) { //p是parent的右孩子且p的左子树非空parent.rchild=p.lchild;} else {parent.rchild=p.rchild;}return p.data;}}return null;}}public class Main {public static void main(String[] args) {int temp;int n;n = 0;Scanner scanner = new Scanner(System.in);BSTree bst = new BSTree();//构造二叉排序树while(true){temp = scanner.nextInt();if(temp == 0)break;bst.insertBST(temp,n);n++;}System.out.print("Inorder:");bst.inOrderTraverse(bst.root);//中序遍历二叉排序树(得到递增有序序列)System.out.println();int x = scanner.nextInt();;Object s = bst.searchBST(x);if (s == null) System.out.println("Find " + x +" failure");else System.out.println("Find " + x +" success");}}
