dataStructure@ Binary Search Tree
#include<iostream>
#include<cstdio>
#include<cstring>
#include<limits>
#include<vector>
using namespace std;
struct BST{
int key;
BST *lc, *rc;
BST(){
this->key = ;
lc = rc = NULL;
}
BST(int k){
this->key = k;
lc = rc = NULL;
}
};
BST* newNode(int item){
BST *tmp = (BST*)malloc(sizeof(BST));
tmp->key = item;
tmp->lc = tmp->rc = NULL;
return tmp;
}
BST* insert(BST *root, int item){
if(root==NULL) return newNode(item);
if(item < root->key){
root->lc = insert(root->lc,item);
}
else if(item > root->key){
root->rc = insert(root->rc,item);
}
return root;
}
void inorder(BST *root){
if(root!=NULL){
inorder(root->lc);
cout<< root->key << endl;
inorder(root->rc);
}
}
int main(){
BST *root = new BST();
for(int i=;i<=;i+=){
root = insert(root, i);
}
inorder(root);
return ;
}
dataStructure@ Binary Search Tree的更多相关文章
- 【数据结构05】红-黑树基础----二叉搜索树(Binary Search Tree)
目录 1.二分法引言 2.二叉搜索树定义 3.二叉搜索树的CRUD 4.二叉搜索树的两种极端情况 5.二叉搜索树总结 前言 在[算法04]树与二叉树中,已经介绍过了关于树的一些基本概念以及二叉树的前中 ...
- [数据结构]——二叉树(Binary Tree)、二叉搜索树(Binary Search Tree)及其衍生算法
二叉树(Binary Tree)是最简单的树形数据结构,然而却十分精妙.其衍生出各种算法,以致于占据了数据结构的半壁江山.STL中大名顶顶的关联容器--集合(set).映射(map)便是使用二叉树实现 ...
- Leetcode 笔记 99 - Recover Binary Search Tree
题目链接:Recover Binary Search Tree | LeetCode OJ Two elements of a binary search tree (BST) are swapped ...
- Leetcode 笔记 98 - Validate Binary Search Tree
题目链接:Validate Binary Search Tree | LeetCode OJ Given a binary tree, determine if it is a valid binar ...
- Leetcode: Convert sorted list to binary search tree (No. 109)
Sept. 22, 2015 学一道算法题, 经常回顾一下. 第二次重温, 决定增加一些图片, 帮助自己记忆. 在网上找他人的资料, 不如自己动手. 把从底向上树的算法搞通俗一些. 先做一个例子: 9 ...
- [LeetCode] Closest Binary Search Tree Value II 最近的二分搜索树的值之二
Given a non-empty binary search tree and a target value, find k values in the BST that are closest t ...
- [LeetCode] Closest Binary Search Tree Value 最近的二分搜索树的值
Given a non-empty binary search tree and a target value, find the value in the BST that is closest t ...
- [LeetCode] Verify Preorder Sequence in Binary Search Tree 验证二叉搜索树的先序序列
Given an array of numbers, verify whether it is the correct preorder traversal sequence of a binary ...
- [LeetCode] 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 the BS ...
随机推荐
- firefly笔记一之http模块
原地址:http://www.9miao.com/question-15-54380.html Firefly是免费开源的游戏服务器端框架,开发语言是python,基于twisted框架开发,作为一名 ...
- OneAPM 云监控部署与试用体验
作为 Zabbix 骨灰级粉丝,一直以来对第三方监控(APM)都是拒绝的.一来觉得收费,二来担心数据被人所知,三来觉得 Zabbix 牛逼到无可取代.但是,随着 APM 市场的火爆,我决定「放下身段」 ...
- python读写配置文件
#coding:utf-8 import ConfigParser class Conf(): def __init__(self,name): self.name = name self.cp = ...
- Chp3: Stacks and Queue
1. 说明如何用两个队列来实现一个栈,并分析有关栈操作的运行时间. 解法:1.有两个队列q1和q2,先往q1内插入a,b,c,这做的都是栈的push操作.2.现在要做pop操作,即要得到c,这时可以将 ...
- jmeter 响应结果分析一
转自:http://www.cnblogs.com/Carrie_Liang/archive/2008/11/05/1327604.html Jmeter测试结果分析这一篇,我打算分成上下两部分.上篇 ...
- Struts 2 + Spring2.5 + Hibernate3整合例子
一.效果 1. 2. 二.结构 1. 2.用到jar包 antlr-2.7.6.jaraspectjrt.jaraspectjweaver.jarc3p0-0.9.1.jarcglib-nodep-2 ...
- 省市区 Mysql 数据库表
1.查省SELECT * FROM china WHERE china.Pid=02.查市SELECT * FROM chinaWHERE china.Pid=3300003.查区SELECT * F ...
- IIS8报错 403 404
当IIS报403错误,而打开目录浏览权限后,又出404错误,这种错误很可能是.net的版本安装问题 注意勾选上asp.net4.5
- 泛型编程、STL的概念、STL模板思想及其六大组件的关系,以及泛型编程(GP)、STL、面向对象编程(OOP)、C++之间的关系
2013-08-11 10:46:39 介绍STL模板的书,有两本比较经典: 一本是<Generic Programming and the STL>,中文翻译为<泛型编程与STL& ...
- Oracle DBA常用SQL
监控SQL 1.监控事例的等待: select event,sum(decode(wait_time,0,0,1)) prev, sum(decode(wait_time,0,1,0)) curr,c ...