Problem 2677 --Binary search trees - Binary Search Tree III

2677: Binary search trees - Binary Search Tree III

"
Time Limit $12$ 秒/Second(s) Memory Limit $512$ 兆字节/Megabyte(s)
提交总数 $0$ 正确数量 $1$
裁判形式 标准裁判/Standard Judge 我的状态 尚未尝试
难度 分类标签 STL

Binary Search Tree III 

Write a program which performs the following operations to a binary search tree T by adding delete operation to B: Binary Search Tree II. 

  insert k: Insert a node containing k as key into T. 

  find k: Report whether T has a node containing k. 

  delete k: Delete a node containing k. 

  print: Print the keys of the binary search tree by inorder tree walk and preorder tree walk respectively. 

The operation delete kk for deleting a given node zz containing key kk from TT can be implemented by an algorithm which considers the following cases: 

1.If z has no children, we modify its parent z.p to replace z with NIL as its child (delete z). 

2.If z has only a single child, we "splice out" z by making a new link between its child and its parent. 

3.If z has two children, we splice out z's successor y and replace z's key with y's key.

In the first line, the number of operations mm is given. In the following m lines, operations represented by insert k, find k, delete k or print are given.

For each find k operation, print "yes" if T has a node containing k, "no" if not. 

In addition, for each print operation, print a list of keys obtained by inorder tree walk and preorder tree walk in a line respectively. Put a space character before each key

18
insert 8
insert 2
insert 3
insert 7
insert 22
insert 1
find 1
find 2
find 3
find 4
find 5
find 6
find 7
find 8
print
delete 3
delete 7
print
yes
yes
yes
no
no
no
yes
yes
 1 2 3 7 8 22
 8 2 1 3 7 22
 1 2 8 22
 8 2 1 22

The number of operations ≤500,000

The number of print operations ≤10. 

−2,000,000,000≤key≤2,000,000,000 

The height of the binary tree does not exceed 100 if you employ the above pseudo code. 

The keys in the binary search tree are all different.

推荐代码 查看2677 所有题解 上传题解视频得图灵币

本题记录 用 户(点击查看用户) 运行号(点击购买题解) 时 间
算法最快[$1376 $ms] 忆一九 808230 2022-02-01 21:35:08
内存最少[$20832 $KB] 忆一九 808230 2022-02-01 21:35:08
第一AC 忆一九 808230 2022-02-01 21:35:08
第一挑战 王天驰 807855 2022-01-21 13:15:10

赛题来源/所属竞赛 会津大学《挑战数据结构与算法》 挑战数据结构与算法

竞赛编号 竞赛名称 竞赛时间 访问比赛