news 2026/4/22 23:12:09

二叉树 (1)

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
二叉树 (1)

1.节点的结构

typedef int BTDatatype; typedef struct BineryTreeNode { BTDatatype val; struct BineryTreeNode* left; struct BineryTreeNode* right; }BTnode;

2.节点的创建

BTnode* BuyNode(int x) { BTnode* newnode = (BTnode*)malloc(sizeof(BTnode)); if (newnode == NULL) { perror("malloc"); return NULL; } newnode->val = x; newnode->left = NULL; newnode->right = NULL; return newnode;//记得返回节点 }

3.手动创建一个二叉树

BTnode* CreatTree() { BTnode* node1 = BuyNode(1); BTnode* node2 = BuyNode(2); BTnode* node3 = BuyNode(3); BTnode* node4 = BuyNode(4); BTnode* node5 = BuyNode(5); BTnode* node6 = BuyNode(6); BTnode* node7 = BuyNode(7); node1->left = node2; node2->left = node3; node1->right = node4; node4->left = node5; node4->right = node6; node6->right = node7; return node1; }

4.前中后序遍历

//前序遍历 void PrevOrder(BTnode* root) { if (root == NULL) { printf("N "); return; } printf("%d ", root->val); PrevOrder(root->left); PrevOrder(root->right); }
//中序遍历 void InOrder(BTnode* root) { if (root == NULL) { printf("N "); return; } InOrder(root->left); printf("%d ", root->val); InOrder(root->right);//是换一下顺序,但是记得把名字改一下 }
//后续遍历 void PostOrder(BTnode* root) { if (root == NULL) { printf("N "); return; } PostOrder(root->left); PostOrder(root->right); printf("%d ", root->val); }

5.求树的节点数

//求树的节点数,可以遍历但代码很挫 int BineryTreeSize(BTnode* root) { if (root == NULL) return 0; return BineryTreeSize(root->left) + BineryTreeSize(root->right) + 1; }

6.树的高度

int BineryTreeHeight(BTnode* root) { if (root == NULL) { return 0; } //记得把求出的值用变量存起来,不然会重复计算,对大大降低效率 int leftH = BineryTreeHeight(root->left); int rightH = BineryTreeHeight(root->right); return leftH > rightH ? leftH + 1 : rightH + 1; }

7.第K层的元素个数

int BineryTreeKcount(BTnode* root,int k) { if (root == NULL) return 0; if (k == 1) return 1; //求第k层的元素个数相当于求两个子树的第k-1层的元素个数 return BineryTreeKcount(root->left, k - 1) + BineryTreeKcount(root->right, k - 1); }

8.查找树种的特定节点

BTnode* BTFind(BTnode* root,int x) { if (root == NULL) return NULL; if (root->val == x) return root; BTnode* left = BTFind(root->left, x); if (left) return left; BTnode* right = BTFind(root->right, x); if (right) return left; }

9.测试的部分

int main() { BTnode* root = CreatTree(); PrevOrder(root); printf("\n"); InOrder(root); printf("\n"); PostOrder(root); printf("\n"); printf("%d",BineryTreeSize(root)); printf("\n"); printf("%d", BineryTreeHeight(root)); printf("\n"); printf("%d", BineryTreeKcount(root,4)); printf("\n"); return 0; }

10.二叉树的销毁

void BTDestroy(BTnode* root) { if (root == NULL) return 0; BTDestroy(root->left); BTDestroy(root->right); free(root); root = NULL; }
版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/4/22 23:10:32

别再傻傻用加法器了!Verilog里数1的个数,这个分治写法能省多少资源?

Verilog高效数1算法:从加法器到分治法的资源优化实战 在FPGA和ASIC设计中,资源优化永远是工程师们绕不开的话题。当我们面对一个看似简单的任务——统计二进制数中1的个数时,新手往往会直接想到用加法器逐位累加。但真正经历过流片痛点的工程…

作者头像 李华
网站建设 2026/4/22 23:08:40

为什么东西而喷,这是个问题,大问题。

本文手搓多年前,我以不亚于现在的力量喷过。当时有人留言,说“你是不是感觉你很优越”,还有人留言说“你这么牛怎么不去上街啊”。我回复说“我不敢”。然后他说“你这个人渣”。我用了多年才醒悟到这一层:你喷的,最终…

作者头像 李华
网站建设 2026/4/22 23:07:45

跨维智能DexWorldModel斩获榜首,世界模型真正的考场在机器人执行里

允中 发自 凹非寺量子位 | 公众号 QbitAIVLA与世界模型:一次被行业自己推着走的范式迁移今年4月,具身智能领域发生了一件看起来不大、但意味深长的事。Generalist AI——也就是PaLM-E、RT-2背后那批人创办的明星公司——发布了GEN-1,并在三个…

作者头像 李华
网站建设 2026/4/22 23:07:03

2026年4款主流降AI率工具横评:嘎嘎降AI性价比碾压同行

2026年4款主流降AI率工具横评:嘎嘎降AI性价比碾压同行 这两年降AI率工具冒出来一堆,挂着"免费试用""全网最低"的招牌,进去一跑结果五花八门。我这半年前前后后把市面上叫得上名字的降AI率工具都试了一遍,真正…

作者头像 李华