71 Query Rank Min Max Successor of BST

【本文链接】

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)
        return 0;
    node->size = node_size(node->left) + node_size(node->right) + 1;
    return node->size;
}

int left_size(BinaryTreeNode *node)
{
    // get left size of node in o(1)
    return node->left != NULL ? node->left->size : 0;
}

//=================================================
// BST Tree  kmin
//=================================================
BinaryTreeNode *kmin_bst(BinaryTreeNode *root, int k)
{
    if (root == NULL)
        return NULL;

int pk = left_size(root) + 1; // 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);
    if (k < 1 || 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)
        return -1;
    // get node size first
    node_size(root);

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

时间: 2024-08-04 18:17:30

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

LINQ to SQL语句之Select/Distinct和Count/Sum/Min/Max/Avg (转)

Select/Distinct操作符 适用场景:o(∩_∩)o- 查询呗. 说明:和SQL命令中的select作用相似但位置不同,查询表达式中的select及所接子句是放在表达式最后并把子句中的变量也就是结果返回回来:延迟. Select/Distinct操作包括9种形式,分别为简单用法. 匿名类型形式.条件形式.指定类型形式.筛选形式.整形类型形式. 嵌套类型形式.本地方法调用形式.Distinct形式. 1.简单用法: 这个示例返回仅含客户联系人姓名的序列. var q = from c i

[Lintcode] Inorder Successor in BST

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. Example Given tree = [2,1] and node = 1: 2 / 1 return node 2. Given tree = [2,1,3] and node = 2: 2 / 1 3 return node 3. Note If

linqtosql(GroupBy/Having/Count/Sum/Min/Max/Avg操作符)

Group By/Having操作符 适用场景:分组数据,为我们查找数据缩小范围. 说明:分配并返回对传入参数进行分组操作后的可枚举对象.分组:延迟 1.简单形式:var q = from p in db.Products group p by p.CategoryID into g select g; 语句描述:使用Group By按CategoryID划分产品. 说明:from p in db.Products 表示从表中将产品对象取出来.group p by p.CategoryID in

Google interview question: count bounded slices(min/max queue)

Question: A Slice of an array said to be a Bounded slice if Max(SliceArray)-Min(SliceArray)<=K. If Array [3,5,6,7,3] and K=2 provided .. the number of bounded slice is 9, first slice (0,0) in the array Min(0,0)=3 Max(0,0)=3 Max-Min<=K result 0<=2

LINQ to SQL语句(3)之Count/Sum/Min/Max/Avg

1.简单形式: 得到数据库中客户 的数量: var q = db.Customers.Count(); 2.带条件形 式: 得到数据库中未断货产品的数量: var q = db.Products.Count(p => !p.Discontinued); LongCount 说明 :返回集合中的元素个数,返回LONG类型:不延迟.对于元素个数较多的集合可 视情况可以选用LongCount来统计元素个数,它返回long类型,比较精确.生成 SQL语句为:SELECT COUNT_BIG(*) FRO

LINQ Count/Sum/Min/Max/Avg

参考:http://www.cnblogs.com/peida/archive/2008/08/11/1263384.html Count/Sum/Min/Max/Avg用于统计数据,比如统计一些数据的个数,求和,最小值,最大值,平均数. 1.Count:返回集合中的元素个数,返回INT类型:不延迟.生成SQL语句为:SELECT COUNT(*) FROM 描述:获得数据库中顾客的数量 语句: var q =                 ndc.Customers.Count(); 对应S

LINQ to SQL语句之Select/Distinct和Count/Sum/Min/Max/Avg

上一篇讲述了LINQ,顺便说了一下Where操作,这篇开始我们继续说LINQ to SQL语句,目的让大家从语句的角度了解LINQ,LINQ包括LINQ to Objects.LINQ to DataSets.LINQ to SQL.LINQ to Entities.LINQ to XML,但是相对来说LINQ to SQL在我们程序中使用最多,毕竟所有的数据都要在数据库运行着各种操作.所以先来学习LINQ to SQL,其它的都差不多了,那么就从Select说起吧,这个在编写程序中也最为常用.

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

http://www.cnblogs.com/cvbnm/articles/1947743.html 多年以前,Microsoft 幹了一件比 #define N 3 還要蠢的蠢事,那就是在 <windows.h> 放入了 min/max 這兩個宏命令(macros). #define max(a,b)            (((a) > (b)) ? (a) : (b)) 因此,我們沒辦法好好地使用 C++ Standard 的 min/max 這兩個 algorithms,也沒有辦

动态规划——min/max的单调性优化总结

一般形式: $max\{min(ax+by+c,dF(x)+eG(y)+f)\},其中F(x)和G(y)是单调函数.$ 或 $min\{max(ax+by+c,dF(x)+eG(y)+f)\},其中F(x)和G(y)是单调函数.$ (以下用第一种形式讨论) (1)dF(x)随ax的增大而增大,eG(y)随by的增大而增大. ax和by均取最大值. (2)dF(x)随ax的增大而增大,eG(y)随by的增大而减小. ax一定取最大值,ax和dF(x)变成常数. 此时变成: $H(y)=max\{m