【本文链接】

http://www.cnblogs.com/hellogiser/p/kmin-of-array-vs-kmin-of-bst.html

【分析】

  数组的Kmin算法和二叉搜索树的Kmin算法非常类似,其本质是找序列中的第K大或者第K小的元素,可以借鉴QuickSort的思想加以实现。

【Kmin_of_Array】

 C++ Code 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
 
// 70_Kmin_of_Array_and_BST.cpp : Defines the entry point for the console application.
//
/*
    version: 1.0
    author: hellogiser
    blog: http://www.cnblogs.com/hellogiser
    date: 2014/9/18
*/

#include "stdafx.h"
#include "iostream"
#include <ctime>
#include <algorithm>
using namespace std;

void print(int *a, int n)
{
    ; i < n; i++)
    {
        cout << a[i] << " ";
    }
    cout << endl;
}

int compare_less (const void *a, const void *b)
{
    return ( *(int *)a - * (int *)b );
}

//========================================================
//   kmin of array
//========================================================
void myswap(int &a, int &b)
{
    int t = a;
    a = b;
    b = t;
}

int partition1(int *a, int left, int right)
{
    // a[left,...p-1]<=a[p]<a[p+1,...right]
    int i = left;
    int j = right;
    int key = a[left];
    while(i < j)
    {
        while(a[i] <= key) i++;
        while(a[j] > key) j--;
        if (i < j)
        {
            myswap(a[i], a[j]);
        }
    }
    // left---j---i---right
    myswap(a[left], a[j]);
    return j;
}

int kmin(int *a, int left, int right, int k)
{
    if(left < = right)
    {
        int p = partition1(a, left, right);
        ;
        if (k == pk)
        {
            return a[p];
        }
        else if (k < pk)
        {
            , k);
        }
        else // k >pk
        {
            , right, k - pk);
        }
    }
}

int Kmin_of_Array(int *a, int n, int k)
{
    )
        ;
     || k > n)
        ;
    , k);
}

void test_base(int *a, int n, int k)
{
    print(a, n);
    qsort(a, n, sizeof(int), compare_less);
    print(a, n);
    cout << k << " min of array is: " << Kmin_of_Array(a, n, k) << endl;
    cout << "==============================\n";
}

void test_default()
{
    srand((unsigned int)time(NULL));
    ;
    int a[n];
    ; i < n; i++)
    {
        a[i] = rand() % ;
    }
    test_base(a, n, );
}

void test_main()
{
    test_default();
}

int _tmain(int argc, _TCHAR *argv[])
{
    test_main();
    ;
}
/*
37 30 22 10 22 63 6 44 36 41 43 75 54 77 9 99 3 13 28 27
3 6 9 10 13 22 22 27 28 30 36 37 41 43 44 54 63 75 77 99
3 min of array is: 9
==============================
*/

【Kmin_of_BST】

 C++ Code 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
 
//========================================================
//   kmin of bst
//========================================================
// binary tree node struct
struct BinaryTreeNode
{
    int value;
    BinaryTreeNode *parent; // for rank of bst
    BinaryTreeNode *left;
    BinaryTreeNode *right;
    int size; // for kmin of bst
    // x.size = x.left.size + x.right.size +1
};

int node_size(BinaryTreeNode *node)
{
    // get node size of node
    if (node == NULL)
        ;
    node->size = node_size(node->left) + node_size(node->right) + ;
    return node->size;
}

int left_size(BinaryTreeNode *node)
{
    // get left size of node in o(1)
;
}

BinaryTreeNode *kmin_bst(BinaryTreeNode *root, int k)
{
    if (root == NULL)
        return NULL;

; // get node rank first

if (k == pk)
    {
        return root;
    }
    else if (k < pk)
    {
        return kmin_bst(root->left, k);
    }
    else // k>pk
    {
        return kmin_bst(root->right, k - pk);
    }
}

BinaryTreeNode *Kmin_of_BST(BinaryTreeNode *root, int k)
{
    if (root == NULL)
        return NULL;
    // get node size of bst first
    int nodes = node_size(root);
     || k > nodes)
        return NULL;
    // use node size info to get kmin of bst
    return kmin_bst(root, k);
}

70 数组的Kmin算法和二叉搜索树的Kmin算法对比的更多相关文章

  1. lintcode: 把排序数组转换为高度最小的二叉搜索树

    题目: 把排序数组转换为高度最小的二叉搜索树 给一个排序数组(从小到大),将其转换为一棵高度最小的排序二叉树. 样例 给出数组 [1,2,3,4,5,6,7], 返回 4 / \ 2 6 / \ / ...

  2. lintcode.177 把排序数组转换为高度最小的二叉搜索树

    把排序数组转换为高度最小的二叉搜索树    描述 笔记 数据 评测 给一个排序数组(从小到大),将其转换为一棵高度最小的排序二叉树. 注意事项 There may exist multiple val ...

  3. lintcode_177_把排序数组转换为高度最小的二叉搜索树

    把排序数组转换为高度最小的二叉搜索树   描述 笔记 数据 评测 给一个排序数组(从小到大),将其转换为一棵高度最小的排序二叉树. 注意事项 There may exist multiple vali ...

  4. 「面试高频」二叉搜索树&双指针&贪心 算法题指北

    本文将覆盖 「字符串处理」 + 「动态规划」 方面的面试算法题,文中我将给出: 面试中的题目 解题的思路 特定问题的技巧和注意事项 考察的知识点及其概念 详细的代码和解析 开始之前,我们先看下会有哪些 ...

  5. HDU 3791 二叉搜索树 (数据结构与算法实验题 10.2 小明) BST

    传送门:http://acm.hdu.edu.cn/showproblem.php?pid=3791 中文题不说题意. 建立完二叉搜索树后进行前序遍历或者后序遍历判断是否一样就可以了. 跟这次的作业第 ...

  6. 看动画学算法之:二叉搜索树BST

    目录 简介 BST的基本性质 BST的构建 BST的搜索 BST的插入 BST的删除 简介 树是类似于链表的数据结构,和链表的线性结构不同的是,树是具有层次结构的非线性的数据结构. 树是由很多个节点组 ...

  7. 剑指offer-第四章解决面试题思路(判断一个数组是否为二叉搜索树的后序遍历序列)

    二叉搜索树:二叉搜索树根节点的左边都比根节点小,右边都比根节点大. 例题:输入一个数组,判断是否为二叉搜索树的后序遍历序列,如果是,返回true,如果不是,返回flase,假设没有重复的元素. 思路: ...

  8. 二叉搜索树 C语言实现

    1.二叉搜索树基本概念 二叉搜索树又称二叉排序树,它或者是一棵空树,或者是一棵具有如下特性的非空二叉树: (1)若它的左子树非空,则左子树上所有结点的关键字均小于根结点的关键字: (2)若它的右子树非 ...

  9. hdu 3999 The order of a Tree (二叉搜索树)

    /****************************************************************** 题目: The order of a Tree(hdu 3999 ...

随机推荐

  1. WCF调用时提示错误 "已尝试创建到达不支持 .Net 框架的服务的通道。可能遇到 HTTP 终结点"

    一个以前运行的很正常的项目,某天突然无法连接WCF构建的后台.使用WCFTestClient连接到服务是正常的,但是调用服务中的方式时就报出了以下错误: 已尝试创建到达不支持 .Net 框架的服务的通 ...

  2. JS建造者模式

    function getBeerById( id, callback){ _request('GET','URL'+id,function(res){ callback(res.responseTex ...

  3. 【URAL 1917】Titan Ruins: Deadly Accuracy(DP)

    题目 #include<cstdio> #include<algorithm> using namespace std; #define N 1005 int n, m, cn ...

  4. 【CodeForces 312B】BUPT 2015 newbie practice #3A Archer

    题 SmallR is an archer. SmallR is taking a match of archer with Zanoes. They try to shoot in the targ ...

  5. GPUImage学习

    1.GLProgram--加载vertex和fragment的shader. 好处是完全将shader模块的加载过程独立出来. 学习:每个函数处理一件事,且函数的粒度刚好 在glLinkProgram ...

  6. 14.Android之Layout布局学习

    Android布局主要有5种,接下来学习总结下. 1) 最常见的线性布局 LinearLayout 线性布局是Android布局中最简单的布局,也是最常用,最实用的布局. android:orient ...

  7. return view详解

    1.return View(); 返回值 类型:System.Web.Mvc.ViewResult将视图呈现给响应的 View() 结果. 注释 View() 类的此方法重载将返回一个具有空 View ...

  8. asp.net input怎么获取值

    前台: <input type="hidden" name="content" value="content"> 后台: Req ...

  9. HDU1242 Rescue

    Rescue Time Limit: 1000MS   Memory Limit: 32768KB   64bit IO Format: %I64d & %I64u Description A ...

  10. Laravel5.1 启动详解

    借鉴: Laravel所有请求的入口文件是:/public/index.php,代码如下 <?php /*|------------------------------------------- ...