#include "test_support.h"
int main(void){
puts("MBSKY | mbsky.com | independently authored C11 case");
Tree nodes[5]={{1,NULL,NULL},{2,NULL,NULL},{3,NULL,NULL},{4,NULL,NULL},{5,NULL,NULL}};nodes[0].left=&nodes[1];nodes[0].right=&nodes[2];nodes[1].left=&nodes[3];nodes[1].right=&nodes[4];int expected[4][5]={{1,2,4,5,3},{4,2,5,1,3},{4,5,2,3,1},{1,2,3,4,5}};int out[CAP];for(int mode=0;mode<4;++mode){CHECK(tree_walk(nodes,mode,out,CAP)==5);equal_values(out,expected[mode],5);state((const char*[]) {"preorder","inorder","postorder","levelorder"}[mode],out,5);}CHECK(tree_height(nodes)==2&&tree_height(NULL)==-1);CHECK(tree_walk(NULL,0,out,CAP)==0);Tree *root=NULL;int values[]={50,30,70,20,40,60,80};for(size_t i=0;i<7;++i)CHECK(bst_insert(&root,values[i]));CHECK(bst_insert(&root,40));CHECK(tree_walk(root,1,out,CAP)==7);equal_values(out,(int[]){20,30,40,50,60,70,80},7);state("BST-sorted",out,7);CHECK(bst_find(root,40)&&!bst_find(root,99));allocation_fail_after(0);CHECK(!bst_insert(&root,99));allocation_fail_after(-1);CHECK(!bst_find(root,99));tree_clear(&root);for(int i=0;i<6;++i)CHECK(bst_insert(&root,i));CHECK(tree_height(root)==5);state("ordered-insertion-height",(int[]){tree_height(root)},1);tree_clear(&root);
return finish();
}
本例需在 Windows,Zig0.14.1 cc,C11 -Wall -Wextra -Werror -pedantic -g -O0;浏览器独立状态回放 中运行,请复制源码、保存文件或下载工程包后在本地执行验证。
运行说明与环境独立工程
运行环境:Windows,Zig0.14.1 cc,C11 -Wall -Wextra -Werror -pedantic -g -O0;浏览器独立状态回放
入口文件:projects/dsa-lab/c-dsa-tree.c
预期结果与验证条件观察提示
- 本章固定输入命名断言通过,编译和执行无未处置诊断
- 自有分配器存活计数归零,值/顺序/状态和错误分支符合明确约定;不等同全部内存与全部输入证明
请结合实际环境逐项观察。
附件与关联工程包下载
课程关联工程包(登记关联20章) · 含当前章节或实例 · mbskycom202611a545.zip