-->
当前位置:首页 > 题库 > 正文内容

程序填空题:IsRBT

Luz4年前 (2021-05-10)题库1267
The functions `IsRBT` is to check if a given binary search tree `T` is a red-black tree. Return `true` if `T` is, or `false` if not.

The red-black tree structure is defined as the following:

```
typedef enum { red, black } colors;
typedef struct RBNode *PtrToRBNode;
struct RBNode{
int Data;
PtrToRBNode Left, Right, Parent;
int BlackHeight;
colors Color;
};
typedef PtrToRBNode RBTree;
```

Please fill in the blanks.

```c++
bool IsRBT( RBTree T )
{
int LeftBH, RightBH;
if ( !T ) return true;
if ( T->Color == black ) T->BlackHeight = 1;
else {
if ( T->Left && @@[(T->Left->Color == red)](2)) return false;
if ( T->Right && (T->Right->Color == red) ) return false;
}
if ( !T->Left && !T->Right ) return true;
if ( @@[IsRBT( T->Left ) && IsRBT( T->Right )](2)) {
if ( T->Left ) LeftBH = T->Left->BlackHeight;
else LeftBH = 0;
if ( T->Right ) RightBH = T->Right->BlackHeight;
else RightBH = 0;
if ( LeftBH == RightBH ) {
@@[T->BlackHeight += LeftBH](2);
return true;
}
else return false;
}
else return false;
}
```






答案:
第1空:(T->Left->Color == red)

第2空:IsRBT( T->Left ) && IsRBT( T->Right )

第3空:T->BlackHeight += LeftBH

发表评论

访客

◎欢迎参与讨论,请在这里发表您的看法和观点。