Data Structure Binary Search Tree: Find k-th smallest element in BST (Order Statistics in BST)
http://www.geeksforgeeks.org/find-k-th-smallest-element-in-bst-order-statistics-in-bst/
#include <iostream>
#include <vector>
#include <algorithm>
#include <queue>
#include <stack>
#include <string>
#include <fstream>
#include <map>
using namespace std; struct node {
int data;
struct node *left, *right;
node() : data(), left(NULL), right(NULL) { }
node(int d) : data(d), left(NULL), right(NULL) { }
}; node *insert(node *root, int key) {
if (!root) return new node(key);
if (root->data > key) root->left = insert(root->left, key);
else root->right = insert(root->right, key);
return root;
} void prints(node *root) {
if (!root) return;
prints(root->left);
cout << root->data << " ";
prints(root->right);
} node *k_smallest(node *root, int k) {
stack<node*> S;
while () {
while (root) {
S.push(root);
root = root->left;
}
if (!S.empty()) {
root = S.top();
S.pop();
if (k- == ) return root;
k--;
root = root->right;
}
else break;
}
return NULL;
} int main() {
node *root = NULL;
root = insert(root, );
insert(root, );
insert(root, );
insert(root, );
insert(root, );
insert(root, );
insert(root, );
if (k_smallest(root, )) cout << k_smallest(root, )->data << endl;
else cout << "No node" << endl;
return ;
}
Data Structure Binary Search Tree: Find k-th smallest element in BST (Order Statistics in BST)的更多相关文章
- Data Structure Binary Search Tree: Inorder Successor in Binary Search Tree
struct node { int val; node *left; node *right; node *parent; node() : val(), left(NULL), right(NULL ...
- Binary search tree system and method
A binary search tree is provided for efficiently organizing values for a set of items, even when val ...
- [数据结构]——二叉树(Binary Tree)、二叉搜索树(Binary Search Tree)及其衍生算法
二叉树(Binary Tree)是最简单的树形数据结构,然而却十分精妙.其衍生出各种算法,以致于占据了数据结构的半壁江山.STL中大名顶顶的关联容器--集合(set).映射(map)便是使用二叉树实现 ...
- Lintcode177-Convert Sorted Array to Binary Search Tree With Minimal Height-Easy
177. Convert Sorted Array to Binary Search Tree With Minimal Height Given a sorted (increasing order ...
- Validate Binary Search Tree leetcode java
题目: Given a binary tree, determine if it is a valid binary search tree (BST). Assume a BST is define ...
- 算法与数据结构基础 - 二叉查找树(Binary Search Tree)
二叉查找树基础 二叉查找树(BST)满足这样的性质,或是一颗空树:或左子树节点值小于根节点值.右子树节点值大于根节点值,左右子树也分别满足这个性质. 利用这个性质,可以迭代(iterative)或递归 ...
- [Data Structure] 二叉搜索树(Binary Search Tree) - 笔记
1. 二叉搜索树,可以用作字典,或者优先队列. 2. 根节点 root 是树结构里面唯一一个其父节点为空的节点. 3. 二叉树搜索树的属性: 假设 x 是二叉搜索树的一个节点.如果 y 是 x 左子树 ...
- [leetcode]272. Closest Binary Search Tree Value II二叉搜索树中最近的值2
Given a non-empty binary search tree and a target value, find k values in the BST that are closest t ...
- 将百分制转换为5分制的算法 Binary Search Tree ordered binary tree sorted binary tree Huffman Tree
1.二叉搜索树:去一个陌生的城市问路到目的地: for each node, all elements in its left subtree are less-or-equal to the nod ...
随机推荐
- 在 Linux 系统下使用 PhotoRec & TestDisk 工具来恢复文件
当你在系统中有意或无意地使用 shift + delete 组合键.删除选项,或是清空回收站的方式来删除一个文件时,该文件的内容并没有从硬盘(或是其它存储设备)上直接销毁. 它仅仅是从系统的目录结构中 ...
- 【转】.NET(C#):浅谈程序集清单资源和RESX资源 关于单元测试的思考--Asp.Net Core单元测试最佳实践 封装自己的dapper lambda扩展-设计篇 编写自己的dapper lambda扩展-使用篇 正确理解CAP定理 Quartz.NET的使用(附源码) 整理自己的.net工具库 GC的前世与今生 Visual Studio Package 插件开发之自动生
[转].NET(C#):浅谈程序集清单资源和RESX资源 目录 程序集清单资源 RESX资源文件 使用ResourceReader和ResourceSet解析二进制资源文件 使用ResourceM ...
- windows利用jconsole远程监控linux的tomcat
1.配置tomcat ① 编辑tomcat的catelina.sh文件,进入tomcat安装目录,使用命令:sudo vim bin/catalina.sh,如果是普通用户启动的,则修改damen. ...
- sass的脑图
- JSTL JSP页面推断某个cookie的值和读取值....
<c:if test="${cookie['woshop'].value eq '1'}"> <div> ...
- [译]GLUT教程 - 移动镜头2
Lighthouse3d.com >> GLUT Tutorial >> Input >> Move the Camera II 本节的最后一个示例是回顾.现在我们 ...
- 利用SQL server 的复制功能分散用户访问服务器的负载
先来了解一下一个基本的关于复制的概念. 什么是复制? 复制就是把数据的多个拷贝(复制品)分发到公司中的各个服务器中,通过复制为多台服务器提供相同的数据.这样用户就可以在不同服务器中访问同样的信息. 对 ...
- linux下apache+php搭建配置记录
第1章 环境说明1.1 系统说明Centos 6.2 (最小化安装)1.2 软件说明httpd-2.4.2.tar.gzapr-util-1.4.1.tar.gzapr-1.4.6.tar.gzpc ...
- 给jquery easy-ui 添加右键菜单
版权声明:转自为EasyUI 的Tab 标签添加右键菜单
- 嵌入式驱动开发之sensor---sensor 图形传感器调试
图像传感器(image sensor)复位顺序 1. 硬件复位寄存器:2. 软件复位寄存器:3. 设置时钟寄存器:4. 设置PLL倍频:5. 设置分辨率:6. 设置窗口控制:7. 设置输出顺序:8. ...