算法
使用中序遍历的结果就是排序二叉树的排序输出(从小到大), 因此可以使用中序遍历 来实现判定二叉树是否为二叉排序树。
注意,不能用递归比较root和left,right的大小关系,因为left,right无法判断和root,root->parent, root->parent->parent等的大小关系
代码实现
bool isBSTree(st_trNode * root){
if(NULL == root){
printf("isBSTree param error\n");
return PARAM_ERR;
}
bool rst = true, rst1 = false;
/*递归结束条件*/
if(NULL == root){
return rst;
}
/*左子树*/
if(NULL != root->left){
rst1 = isBSTree(root->left);
rst = rst & rst1;
}
if(g_lastvalue > root->data){
return false;
}
g_lastvalue = root->data;
/*右子树*/
if(NULL != root->right){
rst1 = isBSTree(root->right);
rst = rst & rst1;
}
return rst;
}
void testBSTree(void){
printf("\n************ testBSTree ************ \n");
st_trNode * p = NULL;
st_trNode * root = NULL;
st_trNode * mostLeft = NULL;
bool rst = false;
gBSTree = NULL;
createTree(&gBSTree);
root = gBSTree->root;
printf("insert 5\n");
p = createTreeNode(5);
insertBSTNode(&root, p);
printf("insert 3\n");
p = createTreeNode(3);
insertBSTNode(&root, p);
printf("insert 7\n");
p = createTreeNode(7);
insertBSTNode(&root, p);
printf("insert 2\n");
p = createTreeNode(2);
insertBSTNode(&root, p);
printf("insert 4\n");
p = createTreeNode(4);
insertBSTNode(&root, p);
printf("insert 6\n");
p = createTreeNode(6);
insertBSTNode(&root, p);
printf("insert 8\n");
p = createTreeNode(8);
insertBSTNode(&root, p);
printf("insert 1\n");
p = createTreeNode(1);
insertBSTNode(&root, p);
gBSTree->root = root;
dumpTree(gBSTree);
p = NULL;
p = SearchBSTreeNode(gBSTree->root, 5);
if(NULL != p){
printf("node[%p] = %d\n", p, p->data);
} else {
printf("Cannot find this node\n");
}
p = SearchBSTreeNode(gBSTree->root, 10);
if(NULL != p){
printf("node[%p] = %d\n", p, p->data);
} else {
printf("Cannot find this node\n");
}
p = SearchBSTreeNode(gBSTree->root, 8);
if(NULL != p){
printf("node[%p] = %d\n", p, p->data);
} else {
printf("Cannot find this node\n");
}
mostLeft = getMostLeftNode(gBSTree->root);
if(NULL != mostLeft){
printf("mostLeft[%p] = %d\n", mostLeft, mostLeft->data);
} else {
printf("Null Tree\n");
}
mostLeft = getMostLeftNode(gBSTree->root->right);
if(NULL != mostLeft){
printf("mostLeft[%p] = %d\n", mostLeft, mostLeft->data);
} else {
printf("Null Tree\n");
}
g_lastvalue = 0;
rst = isBSTree(gBSTree->root);
if(rst){
printf("Tree %p is Binary Sorted Tree \n", gBSTree->root);
} else {
printf("Tree %p is not Binary Sorted Tree \n", gBSTree->root);
}
/*删除在最后测试*/
root = gBSTree->root;
//removeBSTreeNode(&root, 3);
//removeBSTreeNode(&root, 1);
//removeBSTreeNode(&root, 7);
//removeBSTreeNode(&root, 8); /*删除右子树*/
removeBSTreeNode(&root, 5); /*删除根节点*/
gBSTree->root = root;
dumpTree(gBSTree);
/*手动修改一个节点,使得树不在排序*/
p = SearchBSTreeNode(gBSTree->root, 4);
if(NULL != p){
printf("node[%p] = %d\n", p, p->data);
} else {
printf("Cannot find this node\n");
}
p->data = 100;
printf("========= Traverse Tree InOder: %p ===========\n\t", gBSTree);
TraverseTreeInOrder(gBSTree->root);
printf("\n===================================\n");
g_lastvalue = 0;
rst = isBSTree(gBSTree->root);
if(rst){
printf("Tree %p is Binary Sorted Tree \n", gBSTree->root);
} else {
printf("Tree %p is not Binary Sorted Tree \n", gBSTree->root);
}
return;
}
代码编译
gcc main.c list.c stack.c queue.c tree.c -g -o a.exe -DDEBUG
调试输出
========= Dump Tree InOder: 0xcb1010 ===========
1 2 3 4 5 6 7 8
===================================
========= Dump Tree InOderUnrec: 0xcb1010 ===========
1 2 3 4 5 6 7 8
===================================
node[0xcb1030] = 5
Cannot find this node
node[0xcb1150] = 8
mostLeft[0xcb1180] = 1
mostLeft[0xcb1120] = 6
Tree 0xcb1030 is Binary Sorted Tree
========= Dump Tree InOder: 0xcb1010 ===========
1 2 3 4 6 7 8
===================================
========= Dump Tree InOderUnrec: 0xcb1010 ===========
1 2 3 4 6 7 8
===================================
node[0xcb10f0] = 4
========= Traverse Tree InOder: 0xcb1010 ===========
1 2 3 100 6 7 8
===================================
Tree 0xcb1090 is not Binary Sorted Tree