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 ...
随机推荐
- Python:使用threading模块实现多线程编程
转:http://blog.csdn.net/bravezhe/article/details/8585437 Python:使用threading模块实现多线程编程一[综述] Python这门解释性 ...
- WinDbg调试DMP格式文件
前言:WinDbg是微软开发的免费源代码级的调试工具.WinDbg可以用于Kernel模式调试和用户模式调试,还可以调试Dump文件.本文的讨论是在安装了Debugging Tools for Win ...
- MVC 国内架构设计
http://www.cnblogs.com/guomingfeng/p/mvc-ef-query.html http://www.cnblogs.com/haogj/p/3127795.html
- Eclipse执行Hadoop WordCount
前期工作 我的Eclipse是安装在Windows下的,通过Eclipse执行程序连接Hadoop, 需要让虚拟机的访问地址和本机的访问地址保持在同一域内,虚拟机的地址更改前面的文章介绍过了,如果想改 ...
- Qt: 自动调整到最合适的大小(不是很明白)
SortDialog::SortDialog(QWidget *parent) : QDialog(parent) { setupUi(this); groupBox_2->hide(); gr ...
- memcache简易教程
1. memcache是什么 memcache是一个高性能的分布式的内存对象缓存系统,用于动态Web应用以减轻数据库负担.它通过在内存中缓存数据和对象,来减少读取数据库的次数.从而提高动态.数据库驱 ...
- tcp断开的4次挥手
http://blog.csdn.net/fw0124/article/details/7452695 由于TCP连 接是全双工的,因此每个方向都必须单独进行关闭.这原则是当一方完成它的数据发送任务后 ...
- bzoj3157 3516
太神了,被数学题虐了 orz http://m.blog.csdn.net/blog/skywalkert/43970331 这道题关键是抓住m较小的特点,构造递推解决 ; ..,..] of lon ...
- PHP Fileinfo组件越界内存破坏漏洞
漏洞版本: PHP PHP 5.x 漏洞描述: BUGTRAQ ID: 66002 CVE(CAN) ID: CVE-2014-2270 PHP是一种HTML内嵌式的语言. PHP的file程序在解析 ...
- webapp开发经验和资料
开发经验: 开发资料: 1. http://xuui.net/librarys/webapps/webapp-development-of-commonly-used-code-snippets.ht ...