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

数据结构(HS)

一些概念说明:

Complete Binary tree --> 最后一层的结点需要从最左边开始填充
h=⌊log⁡2n⌋h = \lfloor \log_2 n \rfloorh=log2n

Perfect Binary tree --> 最后一层的结点需要全部填满
h=log2(n+1)−1h = log_2(n+1) - 1h=log2(n+1)1

Balanced binary tree --> 任意一个结点的左孩子和右孩子的高度差不能超过k,k经常取值为1

为什么要计算h,因为很多操作的复杂度和高度成正比

我们规定空树的高度为 -1

二叉树的实现方式

  1. 使用链表
  2. 使用数组(完全二叉树时,左孩子的下标是2i+12i+12i+1,右孩子的下标是2i+22i+22i+2

二叉搜索树(BST)

查找,插入,删除的时间复杂度都是O(logn)O(logn)O(logn)

什么是二叉搜索树?

左孩子的值小于等于根结点,右孩子的值大于根结点

下面给出一个最基础的二叉搜索树

#include <stdio.h>
#include <stdlib.h>typedef struct node{int data;struct node *left;struct node *right;
}node;node *insert(node *root, int data){if(root == NULL){root = (node*)malloc(sizeof(node));root->data = data;root->left = NULL;root->right = NULL;}else if(data <= root->data){root->left = insert(root->left, data);}else{root->right = insert(root->right, data);}return root;
}int search(node *root, int data){if(root == NULL){return 0;}else if(data == root->data){return 1;}else if(data <= root->data){return search(root->left, data);}else{return search(root->right, data);}
}int main(){node *root = NULL;root = insert(root, 50);root = insert(root, 30);root = insert(root, 20);root = insert(root, 40);printf("Search 30: %d\n", search(root, 30));printf("Search 60: %d\n", search(root, 60));return 0;
}

Find height od a binary tree

高度的定义:该结点当叶子结点最长路径中边的数量

Height of tree = Height of root

Height of tree with 1 node = 0

深度的定义:该结点到根结点的路径中边的数量

int findHeight(node *root){if(root == NULL) return -1;int leftHeight = findHeight(root->left);int rightHeight = findHeight(root->right);return (leftHeight > rightHeight)? (leftHeight + 1) : (rightHeight + 1);}

Binary Tree Traversal

  • Breadth -first
  • Depth-first
    • root-left-right 前序遍历
    • left-root-right 中序遍历
    • left-right-root 后序遍历

注:如果中序遍历用于二叉搜索树,那么打印出来的数据会是排序好的

层序遍历,使用队列可以实现,时间复杂度O(n)O(n)O(n),空间复杂度O(n)O(n)O(n)

void levelOrder(Tnode *root){if(root == NULL) return;Lnode *queue = (Lnode*)malloc(sizeof(Lnode));push(queue, root);while(!isEmpty(queue)){Tnode *temp = pop(queue);if(temp == NULL) break;printf("%d ",temp->data);if(temp->left != NULL) push(queue, temp->left);if(temp->right != NULL) push(queue, temp->right);free(temp);}}

深度优先(这个的空间复杂度取决于树的高度O(h)O(h)O(h)),这里的空间复杂度指的是栈空间,时间复杂度O(n)O(n)O(n)

void inOrder(Tnode *root){if(root == NULL) return;inOrder(root->left);printf("%d ",root->data);inOrder(root->right);}

删除结点

  1. 删除叶子结点,直接删除就好
  2. 只有一个孩子结点的情况:直接让该结点的父亲连接到该结点的孩子
  3. 当删除的结点有两个孩子的时候
    1. 将该结点的值改成左子树的最大值,然后去删这个最大值
    2. 将该结点的值改成右子树的最小值,然后去删这个最小值
    3. 上面两种情况二选一
Tnode* delete_Tnode(Tnode* root, int data){if(root == NULL) return NULL;if(data < root->data) root->left = delete_Tnode(root->left, data);else if(data > root->data) root->right = delete_Tnode(root->right, data);else{// in here we have found the node to be deleted//case 1 : no chileif(root->left == NULL && root->right == NULL){free(root);root = NULL;}//case 2 : one childelse if(root->left == NULL){Tnode* temp = root;root = root->right;free(temp);}else if(root->right == NULL){Tnode* temp = root;root = root->left;free(temp);}//case 3 : two childrenelse{// find the max value in the left subtree and replace the node's value with itroot->data = find_max(root->left);root = delete_Tnode(root->left, root->data);}}return root;
}
http://www.dtcms.com/a/529603.html

相关文章:

  • 网站的相对路径上海域名网站吗
  • 电商网站的建设步骤黑龙江新闻联播历年片头
  • 营销型网站建设论坛网站建设公司能信吗
  • 展示型网站怎么做php做网站好吗
  • 网站开发方案及报价单调用别人网站的数据库
  • 个人网站开发报告莆田网站建设模板
  • 小规模开普票网站建设几个点织梦网站修改首页图片
  • 岳阳网站设计改版关键词有哪几种
  • wordpress网站会员太多wordpress设置文章
  • 如何开发微信微网站连云港企业网站制作
  • 公园网站建设方案 ppt模板广告互联网推广
  • 深入理解 Vue Router:底层原理与不同模式区别
  • 给企业做网站用什么程序深圳市官网网站建设报价
  • docker新手教程
  • 2025年主流大模型全景对比:Grok、Claude、ChatGPT与Gemini的战场
  • Spring Boot3零基础教程,SpringApplication 自定义 banner,笔记54
  • 做erp系统的网站网站调研方法有哪些内容
  • 做网站页面视频教学外贸网站建设智能建站
  • 多张图做网站背景wordpress后台翻译
  • 网站建设与维护本科教材中企动力做什么的
  • Spring LTW:类加载时织入全解析
  • 设计君网站wordpress的中文插件安装教程
  • 好口碑的网站制作安装价格世界足球排名
  • 网站开发环境vs2015是什么动漫设计与制作工资多少
  • 有哪些做废品的网站亚马逊平台的运营模式
  • 网站开发 工资高吗简洁高端网页
  • Elasticsearch:隔离环境中的高级向量搜索
  • 网站关键词排名优化推广软件模板下载网站
  • 松滋网站开发建了网站怎么装饰
  • 移动端网站怎么做优化龙华网站建设主要工作