编程题

完善程序:(二叉查找树)二叉查找树具有如下性质:每个节点的值都大于其左子树上所有节点的 值、小于其右子树上所有节点的值。 试判断一棵树是否为二叉查找树。

输入的第一行包含一个整数n,表示这棵树有n个顶点, 编号分别为1,2, … ,n,其 中编号为1的为根结点。之后的第i行有三个数value,left_child,right_child,分别表示 该节点关键字的值、左子节点的编号、右子节点的编号;如果不存在左子节点或右子节 点,则用0代替。输出1表示这棵树是二叉查找树,输出0则表示不是。


#include

using namespace std;


const int SIZE = 100;

const int INFINITE = 1000000;


struct node {

\tint left_child, right_child, value;

};


node a[SIZE];


int is_bst(int root, int lower_bound, int upper_bound)

{

\tint cur;


\tif (root == 0)

\t\treturn 1;

\tcur = a[root].value;

\tif ((cur > lower_bound) && ( (1) ) && //(3 分)


\t\t(is_bst(a[root].left_child, lower_bound, cur) == 1) &&

\t\t(is_bst( (2) , (3) , (4) ) == 1)) //(3 分,3 分,3 分)

\treturn 1;

return 0;

}


int main()

{

\tint i, n;

\tcin>>n;

\tfor (i = 1; i <= n; i )

\t\tcin>>a[i].value>>a[i].left_child>>a[i].right_child;

\tcout< (5) , -INFINITE, INFINITE)<

\treturn 0;

}

点赞(0) 打赏

评论列表 共有 0 条评论

暂无评论