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

AVLT

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct Node {int key;struct Node *left;struct Node *right;int height;
} Node;int max(int a, int b) {return (a > b) ? a : b;
}int height(Node *N) {if (N == NULL)return 0;return N->height;
}
Node* newNode(int key) {Node* node = (Node*)malloc(sizeof(Node));node->key = key;node->left = NULL;node->right = NULL;node->height = 1;return node;
}Node *rightRotate(Node *y) {Node *x = y->left;Node *T2 = x->right;x->right = y;y->left = T2;y->height = max(height(y->left), height(y->right)) + 1;x->height = max(height(x->left), height(x->right)) + 1;return x;
}Node *leftRotate(Node *x) {Node *y = x->right;Node *T2 = y->left;y->left = x;x->right = T2;x->height = max(height(x->left), height(x->right)) + 1;y->height = max(height(y->left), height(y->right)) + 1;return y;
}int getBalance(Node *N) {if (N == NULL)return 0;return height(N->left) - height(N->right);
}Node* insert(Node* node, int key) {if (node == NULL)return(newNode(key));if (key < node->key)node->left = insert(node->left, key);else if (key > node->key)node->right = insert(node->right, key);else return node;node->height = 1 + max(height(node->left), height(node->right));int balance = getBalance(node);if (balance > 1 && key < node->left->key)return rightRotate(node);if (balance < -1 && key > node->right->key)return leftRotate(node);// Left Right (LR) Caseif (balance > 1 && key > node->left->key) {node->left = leftRotate(node->left);return rightRotate(node);}if (balance < -1 && key < node->right->key) {node->right = rightRotate(node->right);return leftRotate(node);}return node;
}void preOrder(Node *root) {if(root != NULL) {printf("%d,", root->key);preOrder(root->left);preOrder(root->right);}
}int main() {char line[2048];char* token;Node *root = NULL;if (fgets(line, sizeof(line), stdin) == NULL) {return 1;}line[strcspn(line, "\n")] = 0;token = strtok(line, ",");while (token != NULL) {if (strlen(token) > 0) {int key = atoi(token);root = insert(root, key);}token = strtok(NULL, ",");}preOrder(root);printf("\n");return 0;
}

 

http://www.gsyq.cn/news/54121.html

相关文章:

  • 一线操作工也能管能耗?MyEMS 的 “傻瓜式仪表盘”,把专业数据变成 “大白话”
  • 2025年【口碑好的/比较好的/靠谱的】工业级/国产化/变电站/变电站/电力/机房/光伏/远动/发电厂/工业级/嵌入式机柜/通讯管理机【公司/工厂/厂家】推荐/排行榜 哪家好/强/靠谱
  • linux dmesg
  • linux dns 服务器 搭建
  • 双指针的“适用边界”:从直方图最大矩形错误,看透三大经典问题的本质差异
  • linux dia
  • linux dhcp服务器配置
  • 一文讲清,生产质量管理的10大核心指标及公式
  • CSharp_Winform控件学习_Winform 上加ToolStrip时图标大小调整
  • oracle 查看定义语句
  • linux dhcp服务器的配置
  • 2025 年 11 月碳化钨(WC/C)涂层厂家推荐排行榜,碳化钨涂层,WC/C 涂层,耐磨碳化钨涂层,耐腐蚀碳化钨涂层公司推荐
  • jenkins docker 启动记录
  • Substance Designer的通道合并(Channel Packing)自动化工作流 - 教程
  • 2025年11月外企求职机构推荐榜单与选择指南:知名机构列表与市场报告分析
  • 2025年11月美国求职机构权威推荐:五大机构详细对比分析报告
  • 2025年11月美国求职机构推荐榜单及选择指南:一份基于数据的全面参考列表
  • WSL2安装PostgreSql并远程连接Windows图形工具
  • 2025年11月香港求职机构推荐榜单及权威选择指南
  • 深入解析:解锁Servlet核心:深入剖析HttpServletRequest与HttpServletResponse
  • 2025年洛阳私立高中学校权威推荐榜单:高中学校/民办初中/私立民办学校精选
  • 北京离婚股权分割律师有哪些?业内推荐榜单参考
  • 2025年11月四川考公机构推荐榜单:五家优质机构综合对比与选择指南
  • Qt5实现Windows平台串口通信
  • 2025年不容错过的十大散装物料处理系统品牌,引领工业革新潮流!
  • 如何避免Stimulsoft报表中按页汇总时出现的计算偏差?——原理解析与最佳实践
  • 山东众和新材科技联系方式:合作前需了解的基本事项
  • 深圳公司招聘电气/自动化工程师
  • linux ddos 攻击
  • 实战案例 | 斯歌 NBS 平台驱动 PTP 采购流程端到端解决方案的架构设计与落地复盘