/
it_100_011.cpp
615 lines (501 loc) · 11.9 KB
/
it_100_011.cpp
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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
/*
2014.3.14
关于二叉树和BST(Binary Search Tree)的基本操作和常见问题解决,
目前主要应用了递归:
非递归方式暂时没有给出
2014.3.19:
添加了二叉查找树的删除函数
题目:输入一颗二元查找树,将该树转换为它的镜像,即在转换后的二元查找树中,
左子树的结点都大于右子树的结点。用递归和循环两种方法完成树的镜像转换。
例如输入:
8
/ \
6 10
/\ /\
5 7 9 11
输出:
8
/ \
10 6
/\ /\
11 9 7 5
定义二元查找树的结点为:
struct BSTreeNode // a node in the binary search tree (BST)
{
int m_nValue; // value of node
BSTreeNode *m_pLeft; // left child of node
BSTreeNode *m_pRight; // right child of node
};
递归和循环的函数原型为:
void mirror_recursively(tree_node* root);
void mirror_iteratively(tree_node* root)
*/
#include <iostream>
#include <cassert>
#include <climits>
struct tree_node
{
tree_node()
{
data = 0;
left = right = NULL;
}
tree_node(int init_data){
data = init_data;
left = right = NULL;
}
int data; //as Key
struct tree_node* left;
struct tree_node* right;
};
bool search(tree_node* T, int key)
{
/*
Given a binary tree, return true if a node
with the target data is found in the tree. Recurs
down the tree, chooses the left or right
branch by comparing the target to each node.
*/
// 1. Base case == empty tree;
// in that case , the target is not found so return false
if(T == NULL)
return false;
else
{
//2. see if found here
if(key == T->data) return true;
else
{
//3.otherwise recur down the correct subtree
if(key < T->data) return search(T->left, key);
else return search(T->right, key);
}
}
}
void insert(tree_node* & T, int data)
{
tree_node* add_node = new tree_node(data);
if(T == NULL)
{
std::cout<<"insert as Ptr is NULL::::"<< data<<std::endl;
T = add_node;
}
else if(data <= T->data)
insert(T->left,data);
else
insert(T->right,data);
}
void create_tree(tree_node* & T_root, int ary[], int n)
{
for(int i = 0; i < n ; ++i)
insert(T_root, ary[i]);
}
int size(tree_node * T)
{
/*
compute the number of nodes in a tree
*/
if(T!=NULL)
{
int l_size = size(T->left);
int r_size = size(T->right);
return l_size + r_size +1;
}
else
return 0;
}
int max_depth(tree_node* T)
{
/*
Compute the "max depth" of a tree -- the number of nodes along
the longest path from the root node down to the farthest leaf node
*/
if(T!=NULL)
{
int l_max_depth = max_depth(T->left);
int r_max_depth = max_depth(T->right);
if(l_max_depth >= r_max_depth)
return l_max_depth + 1;
else
return r_max_depth + 1;
}
else
return 0;
}
int max_value(tree_node* T)
{
/*
given a non-empty binary search tree,
return the minimum data value found in taht tree.
Note that the entire tree does not need to be searched
*/
assert(T!=NULL);
while(T->right!=NULL)
T= T->right;
return T->data;
}
int min_value(tree_node* T)
{
assert(T!=NULL);
while(T->left!=NULL)
T = T->left;
return T->data;
}
void inorder_traverse(tree_node* T) // preorder_traverse and postorder_traverse
{
if(T!=NULL)
{
inorder_traverse(T->left);
std::cout<<T->data<<" ";
inorder_traverse(T->right);
}
}
bool has_path_sum(tree_node* T, int sum)
{
/*
Given a tree and a sum, return true if there is a path from the root
down to a leaf, such that adding up all the values along the path
equals the given sum.
Strategy: subtract the node value from the sum when recurring down,
and check to see if the sum is 0 when you run out of tree.
*/
bool l_has = false;
bool r_has = false;
if(T!=NULL)
{
if((T->data == sum) &&(T->left == NULL) &&(T->right == NULL))
return true;
l_has = has_path_sum(T->left, sum - T->data);
r_has = has_path_sum(T->right, sum - T->data);
}
return l_has || r_has;
}
void print_each_path(int path[], int path_len)
{
for(int i = 0; i < path_len; ++i)
std::cout<<path[i]<<" ";
std::cout<<std::endl;
}
void print_paths_recursive(tree_node* T, int path[], int path_len)
{
/* path_len is the position of T->data;*/
/*
Recursive helper function -- given a node, and an array containing
the path from the root node up to but not including this node,
print out all the root-leaf paths.
*/
if(T == NULL) return;
//append this node to the path array
path[path_len] = T->data;
path_len++;
// it's a leaf , so print the path that led to here
if((T->left == NULL)&&(T->right == NULL)) // get leaf node
print_each_path(path, path_len);
else
{
//otherwise try both subtrees
print_paths_recursive(T->left, path, path_len);
print_paths_recursive(T->right, path, path_len);
}
}
void print_paths(tree_node* T)
{
/*
given a binary tree, print out all of its root-to-leaf
paths, one per line. Uses a recursive helper to do the work
*/
int tree_size = size(T);
int* path = new int[tree_size];
print_paths_recursive(T, path, 0);
delete [] path;
}
void mirror(tree_node* T)
{
/*
Change a tree so that the roles of the
left and right pointers are swapped at every node.
So the tree...
4
/ \
2 5
/ \
1 3
is changed to...
4
/ \
5 2
/ \
3 1
*/
if(T!=NULL)
{
mirror(T->left);
mirror(T->right);
tree_node* swap_node = T->left;
T->left = T->right;
T->right = swap_node;
}
}
void double_tree(tree_node* T)
{
/*
For each node in a binary search tree,
create a new duplicate node, and insert
the duplicate as the left child of the original node.
The resulting tree should still be a binary search tree.
So the tree...
2
/ \
1 3
Is changed to...
2
/ \
2 3
/ /
1 3
/
1
*/
if(T!=NULL)
{
tree_node * l_change_node = T -> left;
tree_node * r_change_node = T ->right;
tree_node * add_node = new tree_node(T->data);
add_node -> left = T->left;
T->left = add_node;
double_tree(l_change_node);
double_tree(r_change_node);
}
}
bool same_trees(tree_node* a, tree_node* b)
{
/*
Given two trees, return true if they are
structurally identical.
*/
if(a==NULL && b==NULL)
return true;
else if(a!=NULL && b!=NULL)
{
return (
a->data == b->data &&
same_trees(a->left, b->left)&&
same_trees(a->right, b->right)
);
}
else
return false;
}
int count_trees(int num_keys)
{
int sum = 0;
if(num_keys == 1)
sum = 1;
else
{
sum = 2*count_trees(num_keys-1);
for(int i = 2; i<=num_keys-1;++i)
{
sum+= count_trees(i-1);
sum+= count_trees(num_keys-i);
if(i == 2 || i==num_keys-1) sum-=1;
}
}
return sum;
}
int count_trees_2(int numKeys)
{
/*
For the key values 1...numKeys, how many structurally unique
binary search trees are possible that store those keys.
Strategy: consider that each value could be the root.
Recursively find the size of the left and right subtrees.
*/
if (numKeys <=1)
return(1);
else
{
// there will be one value at the root, with whatever remains
// on the left and right each forming their own subtrees.
// Iterate through all the values that could be the root...
int sum = 0;
int left, right, root;
for (root=1; root<=numKeys; root++) {
left = count_trees_2(root - 1);
right = count_trees_2(numKeys - root);
// number of possible trees with this root == left*right
sum += left*right;
}
return(sum);
}
}
bool is_bst1(tree_node* T)
{
/*
Returns true if a binary tree is a binary search tree.
*/
if(T!=NULL)
{
bool is_leaf = (T->left ==NULL) && (T->right == NULL);
if(is_leaf)
return true;
if(max_value(T) <= T->data && T->right !=NULL)
return false;
if(min_value(T) > T->data && T->left!=NULL)
return false;
if(!is_bst1(T->left))
return false;
if(!is_bst1(T->right))
return false;
return true;
}
else
return true;
}
bool is_bst_recursive(tree_node* T, int min, int max)
{
/*
Returns true if the given tree is a BST and its
values are >= min and <= max.
*/
if(T!=NULL)
{
if(!(min<=T->data && max>=T->data))
return false;
if(!is_bst_recursive(T->right, T->data+1, max))
return false;
if(!is_bst_recursive(T->left, min, T->data))
return false;
return true;
}
else
return true;
}
bool is_bst2(tree_node* T)
{
return is_bst_recursive(T,INT_MIN,INT_MAX);
}
tree_node* delete_node(tree_node* T, int key)
{
/*
INPUT:
tree which a node(node key is 'key') will deleted from
RETURN:
a tree_node which has deleted the 'key' node;
*/
if(T == NULL)
return NULL;
tree_node* current;
if(key < T->data)
T->left = delete_node(T->left, key); // delete from left child
else if(key > T->data)
T->right = delete_node(T->right, key); // delete from right child
else
{
if(T->left == NULL) // left is NULL, link the T's right child as T's root
{
current = T->right;
delete T;
T = current;
}
else if(T->right == NULL)
{
current = T->left;
delete T;
T = current;
}
else // two children
{
current = T->right;
tree_node* parent = NULL;
while(current->left!=NULL)
{
parent = current;
current = current->left;
}
T->data = current ->data;
if(parent != NULL)
parent->left = delete_node(parent->left,parent->left->data);
else
T->right = delete_node(T->right,T->right->data);
}
}
return T;
}
void mirror_recursively(tree_node* root)
{
if(root)
{
mirror_recursively(root->left);
mirror_recursively(root->right);
tree_node* swap_node = root->left;
root->left = root->right;
root->right = swap_node;
}
}
#include <stack>
void mirror_iteratively(tree_node* root)
{
if(root == NULL)
return;
std::stack<tree_node*> tree_stack;
tree_stack.push(root);
while(tree_stack.size())
{
tree_node * top_node = tree_stack.top();
tree_stack.pop();
if(top_node->left != NULL)
tree_stack.push(top_node->left);
if(top_node->right != NULL)
tree_stack.push(top_node->right);
tree_node* swap_node = top_node->left;
top_node->left = top_node->right;
top_node->right = swap_node;
}
}
int main()
{
int ary[10] = {9,23,45,1,2,5,21,16,78,43};
int ary2[10] = {9,23,45,31,2,5,21,16,78,43};
int ary3[3] = {2,1,3};
//int ary[3] = {2,1,3};
tree_node* tree = NULL;
tree_node* tree2 = NULL;
tree_node* tree3 = NULL;
create_tree(tree, ary, 10);
create_tree(tree2, ary2, 10);
create_tree(tree3, ary3, 3);
inorder_traverse(tree);
std::cout<<std::endl;
mirror_recursively(tree);
inorder_traverse(tree);
std::cout<<std::endl;
mirror_iteratively(tree);
inorder_traverse(tree);
std::cout<<std::endl;
// print_paths(tree);
// std::cout<<std::endl;
// tree = delete_node(tree,95);
// print_paths(tree);
// std::cout<<std::endl;
/*
std::cout<<"Size of tree::"<<size(tree)<<std::endl;
std::cout<<"max depth of tree::"<<max_depth(tree)<<std::endl;
std::cout<<"max value of tree::"<<max_value(tree)<<std::endl;
inorder_traverse(tree);
std::cout<<std::endl;
std::cout<<"sum 69 has ? ::"<<(has_path_sum(tree, 69)?"Yes":"No")<<std::endl;
print_paths(tree);
double_tree(tree);
inorder_traverse(tree);
std::cout<<std::endl;
std::cout<<"Are two trees same::"<<(same_trees(tree, tree2)?"Yes":"No")<<std::endl;
std::cout<<"Is tree a bst:::"<<(is_bst2(tree)?"Yes" : "No")<<std::endl;
mirror(tree);
std::cout<<"After mirror operation:: Is tree a bst::"<<(is_bst2(tree)?"Yes" : "No")<<std::endl;
mirror(tree);
print_paths(tree);
inorder_traverse(tree);
std::cout<<"5 num_keys :: trees number:: "<<count_trees(5)<<std::endl;*/
return 0;
}