当前位置: 首页 > news >正文

9 二叉树-添加

二叉树是 0 或多个结点的集合,一个结点最多有两个子结点。

在这里插入图片描述

#include <stdio.h>
#include <stdlib.h>
#define true 1
#define false 0
#define printd(n) printf("%d\n", n)

typedef struct Node *Nodeptr;
typedef int ElementType;
typedef int boolean;

// 某个结点
typedef struct Node {
	// 结点保存的数据
    ElementType data;
	// 结点的左子结点
    Nodeptr left;
    // 结点的右子结点
    Nodeptr right;
} Node;

// 二叉树
typedef struct {
    // 根结点
    Nodeptr root;
} Tree;

init:初始化。

void init(Tree *tree) {
    tree->root = NULL;
}

add(ElementType e):添加。

原则:小的放左边,大的放右边。如添加 5、3、2、4、7、6、8 时,树为:

在这里插入图片描述

过程:创建结点 node。

当树中一个结点都没有时,定 node 为根结点。

if (tree->root == NULL) {
    tree->root = node;
}

当树中一个结点至少有一个结点时,将 node 与 root 比较,才能确定 node 是放在 root 的左还是右边。

if (tree->root != NULL) {
    compare(node, root);
}

1:node 小于 root。

​ 1.1 如果 root 的左子结点为 NULL,定 node 为 root 的左子结点。

​ 1.2 如果 root 有左子结点,将 node 与 root.left 比较。

if (node->data < root->data) {
    if (root->left == NULL) {
        root->left = node;
    } else {
        compare(node, root->left);
    }
}

2:node 大于等于 root。

​ 2.1 如果 root 的右子结点为 NULL,定 node 为 root 的右子结点。

​ 2.2 如果 root 有右子结点,将 node 与 root.right 比较。

if (node->data >= root->data) {
    if (root->right == NULL) {
        root->right = node;
    } else {
        compare(node, root->right);
    }
}

综合:

void compare(Nodeptr node, Nodeptr root) {
    if (node->data < root->data) {
        if (root->left == NULL) {
            root->left = node;
        } else {
            compare(node, root->left);
        }
    } else {
        if (root->right == NULL) {
            root->right = node;
        } else {
            compare(node, root->right);
        }
    }
}
boolean add(Tree *tree, ElementType e) {
    Node *node = calloc(1, sizeof(Node));
    node->data = e;
    node->left = NULL;
    node->right = NULL;

    if (tree->root == NULL) {
        tree->root = node;
    } else {
        compare(node, tree->root);
    }
    return true;
}

测试程序:

添加 5、3、2、4、7、6、8 时,树为:

在这里插入图片描述

程序运行输出应为 root = 5、root.left = 3、root.right = 7、root.left.left = 2、root.left.right = 4、root.right.left = 6、root.right.right = 8。

而 2、4、6、8 无子结点,所以它们的左子结点、右子结点都为 NULL,转为整数输出 0。

int main() {
    Tree tree;
    init(&tree);
    add(&tree, 5);
    add(&tree, 3);
    add(&tree, 7);
    add(&tree, 2);
    add(&tree, 4);
    add(&tree, 6);
    add(&tree, 8);
    printd(tree.root->data);// 5
    printd(tree.root->left->data);// 3
    printd(tree.root->right->data);// 7
    printd(tree.root->left->left->data);// 2
    printd(tree.root->left->right->data);// 4
    printd(tree.root->right->left->data);// 6
    printd(tree.root->right->right->data);// 8

    printd(tree.root->left->left->left);// 0
    printd(tree.root->left->left->right);// 0
    printd(tree.root->left->right->left);// 0
    printd(tree.root->left->right->right);// 0
    printd(tree.root->right->left->left);// 0
    printd(tree.root->right->left->right);// 0
    printd(tree.root->right->right->left);// 0
    printd(tree.root->right->right->right);// 0
}

下一篇:遍历树(直接顺序输出所有结点)。

相关文章:

  • SSM进阶-搭建Dubbo
  • STM32F103 CAN通讯实操
  • JAVA-----注释、字面量、关键字、制表符
  • numpy数组的变形、级联操作、聚合操作、常用的数学函数以及矩阵相关
  • ActiveMQ(二)
  • 某大学ipv6和ipv4结合的校园网规划设计
  • 【程序员表白大师】html七夕脱单必看源码制作
  • 车载VPA形象发展史:谁是第一个吃螃蟹的人?
  • 22.9.30 喜迎暑假多校联赛第二场(欢乐AK找回自信)ABDEFH
  • C++----智能指针
  • SpringMVC处理Ajax请求及处理和响应json格式的数据
  • 论文复现(一)
  • 龙芯+复旦微FPGA全国产VPX高速数据采集卡解决方案
  • 前端blob数据
  • Jenkins+ant+mysql 自动化构建脚本文件输出日志
  • 【399天】跃迁之路——程序员高效学习方法论探索系列(实验阶段156-2018.03.11)...
  • 【vuex入门系列02】mutation接收单个参数和多个参数
  • 【跃迁之路】【463天】刻意练习系列222(2018.05.14)
  • JAVA 学习IO流
  • maven工程打包jar以及java jar命令的classpath使用
  • Puppeteer:浏览器控制器
  • Python_网络编程
  • Sequelize 中文文档 v4 - Getting started - 入门
  • SpiderData 2019年2月23日 DApp数据排行榜
  • Sublime text 3 3103 注册码
  • VirtualBox 安装过程中出现 Running VMs found 错误的解决过程
  • weex踩坑之旅第一弹 ~ 搭建具有入口文件的weex脚手架
  • 初探 Vue 生命周期和钩子函数
  • 解决iview多表头动态更改列元素发生的错误
  • 离散点最小(凸)包围边界查找
  • 盘点那些不知名却常用的 Git 操作
  • 使用putty远程连接linux
  • 异常机制详解
  • 400多位云计算专家和开发者,加入了同一个组织 ...
  • 策略 : 一文教你成为人工智能(AI)领域专家
  • 翻译 | The Principles of OOD 面向对象设计原则
  • ​批处理文件中的errorlevel用法
  • #微信小程序:微信小程序常见的配置传旨
  • (33)STM32——485实验笔记
  • (4)(4.6) Triducer
  • (ISPRS,2023)深度语义-视觉对齐用于zero-shot遥感图像场景分类
  • (附源码)spring boot球鞋文化交流论坛 毕业设计 141436
  • (六)c52学习之旅-独立按键
  • (原創) 如何優化ThinkPad X61開機速度? (NB) (ThinkPad) (X61) (OS) (Windows)
  • (转载)OpenStack Hacker养成指南
  • .NET HttpWebRequest、WebClient、HttpClient
  • .net 前台table如何加一列下拉框_如何用Word编辑参考文献
  • .net6Api后台+uniapp导出Excel
  • .netcore 如何获取系统中所有session_如何把百度推广中获取的线索(基木鱼,电话,百度商桥等)同步到企业微信或者企业CRM等企业营销系统中...
  • .net打印*三角形
  • @AutoConfigurationPackage的使用
  • @RequestBody的使用
  • @serverendpoint注解_SpringBoot 使用WebSocket打造在线聊天室(基于注解)
  • @test注解_Spring 自定义注解你了解过吗?
  • [ vulhub漏洞复现篇 ] ThinkPHP 5.0.23-Rce