problemscpp
A collection of my answers to algorithm problems in c++.
函数 | 变量
acwing::acwing3786 命名空间参考

  1. 二叉排序树
更多...

函数

int get_pre (TreeNode *root, int x)
 
int get_suc (TreeNode *root, int x)
 
void insert (TreeNode *&root, int x)
 
int main (istream &cin, ostream &cout)
 
void remove (TreeNode *&root, int x)
 
 TEST (acwing3786, case1)
 
 TEST (acwing3786, case2)
 

变量

const int INF = 2e9
 
TreeNoderoot = nullptr
 

详细描述

  1. 二叉排序树

函数说明

◆ get_pre()

int acwing::acwing3786::get_pre ( TreeNode root,
int  x 
)

在文件 acwing408.cpp508 行定义.

508 {
509 if(!root)
510 return -INF;
511 if(root->val >= x)
512 return get_pre(root->left, x);
513 else
514 return max(root->val, get_pre(root->right, x));
515 }
int get_pre(TreeNode *root, int x)
Definition: acwing408.cpp:508
TreeNode * right
Definition: acwing408.h:24
TreeNode * left
Definition: acwing408.h:23

引用了 get_pre(), INF, acwing::TreeNode::left, acwing::TreeNode::right, root , 以及 acwing::TreeNode::val.

被这些函数引用 get_pre() , 以及 main().

◆ get_suc()

int acwing::acwing3786::get_suc ( TreeNode root,
int  x 
)

在文件 acwing408.cpp518 行定义.

518 {
519 if(!root)
520 return INF;
521 if(root->val <= x)
522 return get_suc(root->right, x);
523 else
524 return min(root->val, get_suc(root->left, x));
525 }
int get_suc(TreeNode *root, int x)
Definition: acwing408.cpp:518

引用了 get_suc(), INF, acwing::TreeNode::left, acwing::TreeNode::right, root , 以及 acwing::TreeNode::val.

被这些函数引用 get_suc() , 以及 main().

◆ insert()

void acwing::acwing3786::insert ( TreeNode *&  root,
int  x 
)

在文件 acwing408.cpp460 行定义.

460 {
461 /*
462 1. 递归找到x的待插入的位置
463 2. 如果x < 当前root就递归到左子树,反之,递归到右子树。
464 */
465 if(!root)
466 root = new TreeNode(x);
467 // 如果发现数值相同的话就判断一下;
468 if(root->val == x)
469 return;
470 else if(x < root->val)
471 insert(root->left, x);
472 else
473 insert(root->right, x);
474 }
void insert(TreeNode *&root, int x)
Definition: acwing408.cpp:460

引用了 insert(), acwing::TreeNode::left, acwing::TreeNode::right, root , 以及 acwing::TreeNode::val.

被这些函数引用 insert() , 以及 main().

◆ main()

int acwing::acwing3786::main ( istream &  cin,
ostream &  cout 
)

在文件 acwing408.cpp441 行定义.

441 {
442 int n;
443 cin >> n;
444 while(n--) {
445 int t, x;
446 cin >> t >> x;
447 if(t == 1)
448 insert(root, x);
449 else if(t == 2)
450 remove(root, x);
451 else if(t == 3)
452 cout << get_pre(root, x) << endl;
453 else
454 cout << get_suc(root, x) << endl;
455 }
456 return 0;
457 }
void remove(TreeNode *&root, int x)
Definition: acwing408.cpp:476

引用了 get_pre(), get_suc(), insert(), remove() , 以及 root.

被这些函数引用 TEST().

◆ remove()

void acwing::acwing3786::remove ( TreeNode *&  root,
int  x 
)

在文件 acwing408.cpp476 行定义.

476 {
477 /*
478 1. 待删除的结点只有左子树。立即可推出,左子树上的结点都是小于待删除的结点的,我们只需要把待删除结点删了然后左子树接上待删除结点的父节点就可以了。
479 2. 待删除的结点只有右子树。立即可推出,右子树上的结点都是大于待删除的结点的,我们只需要把待删除结点删了然后右子树接上待删除结点的父节点就可以了。
480 3. 待删除的结点既有左子树又有右子树。这个比上两个情况麻烦一点,但也不麻烦,需要读者理解的是,怎么能删除结点后还不改变中序遍历的结果,并且操作代价最小,显而易见,我们根据待删除结点的左子树可以得到最右下角的最后结点满足$<x$并且最接近x的结点,把这个结点覆盖待删除的结点,然后把这个结点删除,就完成了我们的操作。
481 */
482
483 // 如果不存在直接return
484 if(!root)
485 return;
486 if(x < root->val)
487 remove(root->left, x);
488 else if(x > root->val)
489 remove(root->right, x);
490 else {
491 if(!root->left && !root->right)
492 root = NULL;
493 else if(!root->left)
494 root = root->right;
495 else if(!root->right)
496 root = root->left;
497 else {
498 auto p = root->left;
499 while(p->right)
500 p = p->right;
501 root->val = p->val;
502 remove(root->left, p->val);
503 }
504 }
505 }

引用了 acwing::TreeNode::left, remove(), acwing::TreeNode::right, root , 以及 acwing::TreeNode::val.

被这些函数引用 main(), remove(), leetcode::stone_game_ix::Solution::stoneGameIX() , 以及 leetcode::strong_password_checker::Solution::strongPasswordChecker().

◆ TEST() [1/2]

acwing::acwing3786::TEST ( acwing3786  ,
case1   
)

在文件 acwing408_test.cpp969 行定义.

969 {
970 istringstream in("6\n"
971 "1 1\n"
972 "1 3\n"
973 "1 5\n"
974 "3 4\n"
975 "2 3\n"
976 "4 2");
977 auto out = ostringstream();
978 main(in, out);
979 const auto ans = out.str();
980 ASSERT_EQ("3\n"
981 "5\n",
982 ans);
983 }
int main(int argc, char **argv)
Definition: main.cpp:5

引用了 main().

◆ TEST() [2/2]

acwing::acwing3786::TEST ( acwing3786  ,
case2   
)

在文件 acwing408_test.cpp985 行定义.

985 {
986 istringstream in("20\n"
987 "1 -9\n"
988 "1 5\n"
989 "2 5\n"
990 "2 -9\n"
991 "1 -5\n"
992 "1 -4\n"
993 "4 -19\n"
994 "1 -10\n"
995 "2 -4\n"
996 "2 -10\n"
997 "1 -14\n"
998 "4 -17\n"
999 "4 -10\n"
1000 "2 -14\n"
1001 "1 -12\n"
1002 "1 -3\n"
1003 "3 -8\n"
1004 "1 -7\n"
1005 "4 -18\n"
1006 "1 1");
1007 auto out = ostringstream();
1008 main(in, out);
1009 const auto ans = out.str();
1010 ASSERT_EQ("-5\n"
1011 "-14\n"
1012 "-5\n"
1013 "-12\n"
1014 "-12\n",
1015 ans);
1016 }

引用了 main().

变量说明

◆ INF

const int acwing::acwing3786::INF = 2e9

在文件 acwing408.cpp439 行定义.

被这些函数引用 get_pre() , 以及 get_suc().

◆ root

TreeNode* acwing::acwing3786::root = nullptr