COJ1005(Binary Search Tree analog)
题目链接
二分查找树的模拟。不能用数组模拟,那样内存会爆掉。
1 #include <stdio.h> 2 #define N 1000 3 struct node 4 { 5 int x; 6 struct node *left,*right; 7 }node[N]; 8 char f; 9 void Insert(struct node *r,struct node *p) 10 { 11 if(!r) return; 12 if(r->x>p->x) 13 { 14 if(r->left) Insert(r->left,p); 15 else r->left=p; 16 } 17 else 18 { 19 if(r->right) Insert(r->right,p); 20 else r->right=p; 21 } 22 } 23 void pretral(struct node *p) 24 { 25 if(!p) return; 26 if(f) printf("%d",p->x),f=0; 27 else printf(" %d",p->x); 28 pretral(p->left); 29 pretral(p->right); 30 } 31 void intral(struct node *p) 32 { 33 if(!p) return; 34 intral(p->left); 35 if(f) printf("%d",p->x),f=0; 36 else printf(" %d",p->x); 37 intral(p->right); 38 } 39 void postod(struct node *p) 40 { 41 if(!p) return; 42 postod(p->left); 43 postod(p->right); 44 if(f) printf("%d",p->x),f=0; 45 else printf(" %d",p->x); 46 } 47 int main() 48 { 49 int t,i,n,x; 50 struct node root; 51 struct node *p; 52 scanf("%d",&t); 53 while(t--) 54 { 55 scanf("%d",&n); 56 scanf("%d",&root.x); 57 root.left=root.right=0; 58 for(i=0;i<n-1;i++) 59 { 60 scanf("%d",&x); 61 p=&node[i]; 62 p->x=x; 63 p->left=p->right=0; 64 Insert(&root,p); 65 } 66 f=1; 67 pretral(&root); 68 printf("\n"); 69 f=1; 70 intral(&root); 71 printf("\n"); 72 f=1; 73 postod(&root); 74 printf("\n"); 75 printf("\n"); 76 } 77 return 0; 78 }
转载于:https://www.cnblogs.com/algorithms/archive/2012/04/25/2470345.html
COJ1005(Binary Search Tree analog)相关推荐
- 笔试算法题(58):二分查找树性能分析(Binary Search Tree Performance Analysis)
议题:二分查找树性能分析(Binary Search Tree Performance Analysis) 分析: 二叉搜索树(Binary Search Tree,BST)是一颗典型的二叉树,同时任 ...
- 【ACM】二叉搜索树(Binary Search Tree /BS Tree) 小结
动态管理集合的数据结构--二叉搜索树 搜索树是一种可以进行插入,搜索,删除等操作的数据结构,可以用字典或者优先队列. 二叉排序树又称为二叉查找树,他或者为空树,或者是满足如下性质的二叉树. (1)若它 ...
- 二叉搜索树(binary search tree)的建立、删除、查找
由于输入的数据顺序不同,建立的bst会不一样.最坏的情况就是一个链,所以我们引入了平衡二叉树的概念.这里我们先来看binary search tree.(我随笔里面有一些相关知识) 建立(也就是插入) ...
- 查找二叉树中出现次数最多的数 Find Mode in Binary Search Tree
为什么80%的码农都做不了架构师?>>> 问题: Given a binary search tree (BST) with duplicates, find all the ...
- LeetCode 426. Convert Binary Search Tree to Sorted Doubly Linked List--转换二叉树为双向链表--Java,C++,Python解法
题目地址:Convert Binary Search Tree to Sorted Doubly Linked List - LeetCode Convert a BST to a sorted ci ...
- LeetCode: 109. Convert Sorted List to Binary Search Tree
题目 Given a singly linked list where elements are sorted in ascending order, convert it to a height b ...
- 学习数据结构 二叉查找树(binary search tree)
2019独角兽企业重金招聘Python工程师标准>>> 为学习 LLVM 的 ImmutableSet,其底层的实现选择为 AVL 树(平衡二叉搜索树),我不很熟悉该树,虽然大致知道 ...
- Recover Binary Search Tree
Two elements of a binary search tree (BST) are swapped by mistake. Recover the tree without changing ...
- Lowest Common Ancestor of a Binary Search Tree(树中两个结点的最低公共祖先)
题目描述: Given a binary search tree (BST), find the lowest common ancestor (LCA) of two given nodes in ...
最新文章
- 从程序媛到启明星辰集团云安全总经理,郭春梅博士揭秘云时代安全攻防之道...
- java流实例_java流汇总以及使用实例
- sass部分知识小结
- 寄存器和pin_16x2 LCD的PIN图和寄存器
- 历时五天用 SwiftUI 做了一款 APP,阿里工程师如何做的?
- oracle tsn 数据库,【Oracle数据库恢复】ORA-00600[25027]错误解析
- Nginx反向代理与负载均衡等配置文件示例
- 20191013:快速排序1.1
- 经典浪漫的爱情英语妙语
- 探测远程主机操作系统指纹的全新技术
- IP通信基础 3月6日
- 1079. Total Sales of Supply Chain (25)-求数的层次和叶子节点
- easyui图标使用(查找)
- Java 读取p12 证书私钥
- 盘点近年来的各国各行较知名的互联网安全事件
- 离散数学笔记--重言式
- 卷积法求解系统的零状态响应_因果系统的零状态响应的一种简易计算方法
- Sushi的MISO:不断扩展的DeFi边界
- 使用网址--java JPI代码示例
- FastReport for Delphi
热门文章
- JZOJ 3786. 【NOI2015模拟8.19】图
- python中赋值不正确的_python中关于赋值、浅拷贝与深拷贝的问题
- html代码在线运行环境,ES5/可执行代码与执行环境
- 图像数据流识别圆形_人工智能大赛视觉处理(一)图形识别
- Visual Question Answering概述
- 非常好用的一些软件和网站
- git 改了一段代码不想要了_初识Git
- label文字颜色_HowTo —— SwiftUI2.0如何使用Label
- 大型网站系统架构系列:负载均衡详解(一)
- Selenium WebDriver- 操作 IFrame 中的页面元素