问答题

【说明】 函数DeleteNode (Bitree *r, int e)的功能是:在树根结点指针为r的二叉查找(排序)树上删除键值为e的结点,若删除成功,则函数返回0,否则函数返回-1。二叉查找树结点的类型定义为: typedef struct Tnode{ int data; /*结点的键值*/ struct Tnode *Lchild, *Rchild; /*指向左、右子树的指针*/ }*Bitree: 在二叉查找树上删除一个结点时,要考虑3种情况: ①若待删除的结点p是叶子结点,则直接删除该结点; ②若待删除的结点p只有一个子结点,则将这个子结点与待删除结点的父结点直接连接,然后删除结点p; ③若待删除的结点p有两个子结点,则在其左子树上,用中序遍历寻找关键值最大的结点s,用结点s的值代替结点p的值,然后删除结点s,结点s必属于上述①、②情况之一。 【函数】 int DeleteNode (Bitree *r,int e) { Bitree p=*r,pp,s,c; while ( (1) ){ /*从树根结点出发查找键值为e的结点*/ pp=p; if(e<p->data) p=p->Lchild; else p=p->Rchild; } if(!P) return-1; /*查找失败*/ if(p->Lchild && p->Rchild) {/*处理情况③*/ s= (2) ;pp=p while (3) {pp=s;s=s->Rchild;} p->data=s->data; p=s; } /*处理情况①、②*/ if ( (4) ) c=p->Lchild; else c=p->Rchild; if(p==*r) *r=c; else if ( (5) ) pp->Lchild=c; else pp->Rchild=c; free (p); return 0; }

【参考答案】

[解析] (1)p &&p->data!=e;或p!=NULL&&p->data!=e或p&&(*p).data!=e......

(↓↓↓ 点击下方‘点击查看答案’看完整答案 ↓↓↓)
热门 试题

问答题
【说明】C++语言本身不提供对数组下标越界的判断。为了解决这一问题,在程序6中定义了相应的类模板,使得对厂任意类型的二维数组,可以在访问数组元素的同时,对行下标和列下标进行越界判断,并给出相应的提示信息。#include<iostream.h>template <class T> class Array;template <class T> class ArrayBody {friend (1) T* tpBody;int iRows, iColumns, iCurrentRow;ArrayBody (int iRsz, int iCsz) {tpBody = (2) iRows = iRsz; iColumns =iCsz; iCurrentRow =-1;}public:T& operator[] (int j) {bool row_error, column_error;row_error=column_error=false;try{if (iCurrentRow < 0 || iCurrentRow >=iRows)row_error=true;if (j < 0 || j >=iColumns)column_error=true;if ( row_error==true || column_error == true)(3) }catch (char) {if (row_error==true)cerr << 行下标越界[ << iCurrentRow << ] ;if (column_error== true )cerr << 列下标越界[ <<j << ] ;cout << n ;}return tpBody[iCurrentRow * iColumns +j];};~ArrayBody ( ) { delete[] tpBody; }};template <class T> class Array {ArrayBody<T> tBody;public:ArrayBody<T> & operator[] (int i) {(4) return tBody;}Array (int iRsz, int iCsz) : (5) {}};void main(){ Array<int>a1(10,20);Array<double>a2(3,5);int b1;double b2;b1=a1[-5][10]; 有越界提示:行下标越界[-5]b1=a1[10][15]; 有越界提示:行下标越界[10]b1=a1[1][4]; 没有越界提示b2=a2[2][6]; 有越界提示:列下标越界[6]b2=s2[10][20]; 有越界提示:行下标越界[10]列下标越界[20]b2=a2[1][4]; 没有越界提示}
问答题