【本文链接】

http://www.cnblogs.com/hellogiser/p/query-min-max-successor-of-bst.html

【代码】

 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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
 
/*
    version: 1.0
    author: hellogiser
    blog: http://www.cnblogs.com/hellogiser
    date: 2014/9/18
*/

// 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)
;
}

//=================================================
// BST Tree  kmin
//=================================================
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);
}

//=================================================
// BST Tree  querying
//=================================================
BinaryTreeNode *Search_of_BST(BinaryTreeNode *root, int key)
{
    if (root == NULL)
        return NULL;
    if (key == root->value)
        return root;
    else if(key < root->value)
        return Search_of_BST(root->left, key);
    else
        return Search_of_BST(root->right, key);
}

BinaryTreeNode *Search_of_BST2(BinaryTreeNode *root, int key)
{
    BinaryTreeNode *node = root;
    while (node != NULL && key != node->value)
    {
        if (key < node->value)
            node = node->left;
        else
            node = node->right;
    }
    return node;
}

BinaryTreeNode *Min_of_BST(BinaryTreeNode *root)
{
    if (root == NULL)
        return NULL;
    BinaryTreeNode *node = root;
    while(node->left != NULL)
        node = node->left;
    return node;
}

BinaryTreeNode *Max_of_BST(BinaryTreeNode *root)
{
    if(root == NULL)
        return NULL;
    BinaryTreeNode *node = root;
    while(node->right != NULL)
        node = node->right;
    return node;
}

/*
x has right child ===> Min(x.right)           (case 1)
else px = x.parent                            (case 2)

if px.right == x ===> go up until px==null   (case 2.2)
else px.left ==x ===> px                     (case 2.1)
*/
BinaryTreeNode *Successor(BinaryTreeNode *x)
{
    if(x == NULL)
        return NULL;
    // case 1
    if (x->right != NULL)
        return Min_of_BST(x->right);
    // case 2
    BinaryTreeNode *px = x->parent;
    if(px == NULL)
        return NULL;
    // case 2.1
    if (px->left == x)
        return px;
    // case 2.2
    while(px != NULL && px->right == x)
    {
        x = px;
        px = px->parent;
    }
    return px;
}

/*
        px                 px
       /                     \
      x                       x
*/
/*
get all node size first

rank = leftsize(x)+1
px = x.parent
if px.right ==x ====> rank += leftsize(px)+1, go up
else rank += 0
*/
int Rank_of_BST(BinaryTreeNode *root, BinaryTreeNode *x)
{
    if(root == NULL || x == NULL)
        ;
    // get node size first
    node_size(root);

;
    // parent's left or right child ?
    BinaryTreeNode *px = x->parent;
    while(px != NULL)
    {
        if (px->right == x)
        {
            // px's right child
;
        }
        px = px->parent;
    }
    return rank;
}

71 Query Rank Min Max Successor of BST的更多相关文章

  1. [20180316]为什么不使用INDEX FULL SCAN (MIN/MAX).txt

    [20180316]为什么不使用INDEX FULL SCAN (MIN/MAX).txt --//链接:http://www.itpub.net/thread-2100456-1-1.html.自己 ...

  2. JS中Float类型加减乘除 修复 JQ 操作 radio、checkbox 、select LINQ to SQL:Where、Select/Distinct LINQ to SQL Count/Sum/Min/Max/Avg Join

    JS中Float类型加减乘除 修复   MXS&Vincene  ─╄OvЁ  &0000027─╄OvЁ  MXS&Vincene MXS&Vincene  ─╄Ov ...

  3. [LeetCode] Inorder Successor in BST 二叉搜索树中的中序后继节点

    Given a binary search tree and a node in it, find the in-order successor of that node in the BST. No ...

  4. 在一定[min,max]区间,生成n个不重复的随机数的封装函数

    引:生成一个[min,max]区间的一个随机数,随机数生成相关问题参考→链接 var ran=parseInt(Math.random()*(max-min+1)+min); //生成一个[min,m ...

  5. LINQ to SQL Count/Sum/Min/Max/Avg Join

    public class Linq { MXSICEDataContext Db = new MXSICEDataContext(); // LINQ to SQL // Count/Sum/Min/ ...

  6. 2.10 用最少次数寻找数组中的最大值和最小值[find min max of array]

    [本文链接] http://www.cnblogs.com/hellogiser/p/find-min-max-of-array.html [题目] 对于一个由N个整数组成的数组,需要比较多少次才能把 ...

  7. LINQ Count/Sum/Min/Max/Avg

    参考:http://www.cnblogs.com/peida/archive/2008/08/11/1263384.html Count/Sum/Min/Max/Avg用于统计数据,比如统计一些数据 ...

  8. LeetCode Inorder Successor in BST

    原题链接在这里:https://leetcode.com/problems/inorder-successor-in-bst/ Given a binary search tree and a nod ...

  9. 【转载】:【C++跨平台系列】解决STL的max()与numeric_limits::max()和VC6 min/max 宏冲突问题

    http://www.cnblogs.com/cvbnm/articles/1947743.html 多年以前,Microsoft 幹了一件比 #define N 3 還要蠢的蠢事,那就是在 < ...

随机推荐

  1. jQuery技术交流资料

    jQuery技术交流资料https://www.zybuluo.com/jikeytang/note/65371

  2. tuple内部方法

    代码: #tuple内部方法 ac=('a','r','6','d','a','b','b','e') print(dir(ac)) print(ac.count('a')) print(ac.ind ...

  3. BZOJ-3227 红黑树(tree) 树形DP

    个人认为比较好的(高端)树形DP,也有可能是人傻 3227: [Sdoi2008]红黑树(tree) Time Limit: 10 Sec Memory Limit: 128 MB Submit: 1 ...

  4. BZOJ-1002 轮状病毒 高精度加减+Kirchhoff矩阵数定理+递推

    1002: [FJOI2007]轮状病毒 Time Limit: 1 Sec Memory Limit: 162 MB Submit: 3543 Solved: 1953 [Submit][Statu ...

  5. BZOJ1207 [HNOI2004]打鼹鼠

    Description 鼹鼠是一种很喜欢挖洞的动物,但每过一定的时间,它还是喜欢 把头探出到地面上来透透气的.根据这个特点阿Q编写了一个打鼹鼠的游戏:在一个n*n的网格中,在某些时刻鼹鼠会在某一个网格 ...

  6. POJ2186 Popular Cows

    Description Every cow's dream is to become the most popular cow in the herd. In a herd of N (1 <= ...

  7. RedGate .NET Reflector注册问题(反注册)

    Reflector分为桌面版和VS集成版本,当我们使用注册机注册的时候如果注册了Standvard版本,那么我们的VS就不能集成查看,也不能Debug,那么这 显然不是我们想要的,我们会选择重新注册, ...

  8. POJ 1273 Drainage Ditches -dinic

    dinic版本 感觉dinic算法好帅,比Edmonds-Karp算法不知高到哪里去了 Description Every time it rains on Farmer John's fields, ...

  9. C#获取局域网中的所有正在使用的IP地址

    方法不是很好. using System; using System.Collections.Generic; using System.Linq; using System.Text; using ...

  10. 在cmd下编译一个简单的servlet时出现程序包javax.servlet不存在

    由于servlet和JSP不是Java平台JavaSE(标准版)的一部分,而是Java EE(企业版)的一部分,因此,必须告知编译器servlet的位置. 解决“软件包 javax.servlet不存 ...