C++数据结构与算法实现(目录)
答案在此:二叉查找树(binary search tree)(答案)
写在前面
部分内容参《算法导论》
基本接口实现
1 删除
删除值为value的第一个节点
删除叶子节点1
删除叶子节点1
(3)删除叶子节点1
删除有两个孩子的节点z
分成下面几个步骤进行:
1 找到z的后继,y是z的后继。这时候可以确定y是不可能有左孩子的。
2 删除y,让y的右孩子x取代自己的位置。删除只有一个孩子的节点上面已经讨论过。
3 让y的值覆盖z的值。
待实现代码
#pragma once
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
正确输出
line:725 Pass
line:726 Pass
line:727 Pass
line:733 Pass
line:734 Pass
line:735 Pass
line:741 Pass
line:742 Pass
line:743 Pass
line:749 Pass
line:750 Pass
line:751 Pass
line:758 Pass
line:759 Pass
line:760 Pass
line:767 Pass
line:768 Pass
line:769 Pass
line:776 Pass
line:777 Pass
line:778 Pass
line:785 Pass
line:786 Pass
line:787 Pass
line:794 Pass
line:796 Pass
line:797 Pass
line:804 Pass
line:806 Pass
line:807 Pass
line:814 Pass
line:816 Pass
line:817 Pass
line:818 Pass
line:825 Pass
line:827 Pass
line:828 Pass
line:829 Pass
line:836 Pass
line:837 Pass
line:838 Pass
line:845 Pass
line:846 Pass
line:847 Pass
line:854 Pass
line:855 Pass
line:862 Pass
line:864 Pass
line:865 Pass
line:871 Pass
3
--2
--1
3
--1
line:875 Pass
line:876 Pass
line:877 Pass
line:878 Pass
line:885 Pass
line:886 Pass
line:888 Pass
line:889 Pass
line:891 Pass
line:892 Pass
line:893 Pass
--10
--9
--8
--7
--6
5
--4
--3
--2
--1
-------------------
--10
--9
--8
--7
--6
5
--4
--3
--2
-------------------
line:903 Pass
--10
--9
--8
--7
--6
5
--4
--3
-------------------
line:906 Pass
line:908 Pass
--10
--9
--8
--7
--6
5
--4
-------------------
--10
--9
--8
--7
--6
5
-------------------
line:912 Pass
10
--9
--8
--7
--6
-------------------
line:915 Pass
10
--9
--8
--7
-------------------
line:918 Pass
10
--9
--8
-------------------
line:921 Pass
10
--9
-------------------
line:924 Pass
10
-------------------
line:927 Pass
-------------------
line:930 Pass
--10
--9
--8
--7
--6
5
--4
--3
--2
--1
-------------------
--9
--8
--7
--6
5
--4
--3
--2
--1
-------------------
line:940 Pass
--9
--7
--6
5
--4
--3
--2
--1
-------------------
line:943 Pass
--10
--9
--8
--7
--6
5
--4
--3
--2
--1
-------------------
--10
--9
--8
--7
6
--4
--3
--2
--1
-------------------
line:953 Pass
line:960 Pass
line:962 Pass
line:964 Pass
line:966 Pass
line:968 Pass
line:970 Pass
line:971 Pass
line:973 Pass
line:975 Pass
line:977 Pass
line:979 Pass
line:981 Pass
line:982 Pass
line:989 Pass
tree:
15
--14
--12
tree2:
15
--14
--12
line:993 Pass
line:1047 Pass
line:1048 Pass
line:1049 Pass
line:1050 Pass
test_tree:
--41
--40
--39
--38
--37
--36
--35
--34
--24
--23
--22
--21
--20
--19
--18
--17
--16
15
--14
--13
--12
--0
tree size : 22
tree max length between node 14
print_in_order_nonrecursive : 0 12 13 14 15 16 17 18 19 20 21 22 23 24 34 35 36 37 38 39 40 41
print_element_order by order: 0 12 13 14 15 16 17 18 19 20 21 22 23 24 34 35 36 37 38 39 40 41
print_post_order_nonrecursive : 0 13 14 12 17 18 19 20 21 22 24 23 41 40 39 38 37 36 35 34 16 15
print_pre_order_nonrecursive : 15 12 0 14 13 16 34 23 22 21 20 19 18 17 24 35 36 37 38 39 40 41
min element : 0
max element : 41
test_tree:
15
--14
--12
tree size : 3
tree max length between node 2
print_in_order_nonrecursive : 12 14 15
print_element_order by order: 12 14 15
print_post_order_nonrecursive : 14 12 15
print_pre_order_nonrecursive : 15 12 14
min element : 12
max element : 15
tree:
--41
--40
--39
--38
--37
--36
--35
--34
--24
--23
--22
--21
--20
--19
--18
--17
--16
15
--14
--13
--12
--0
tree2:
--41
--40
--39
--38
--37
--36
--35
--34
--24
--23
--22
--21
--20
--19
--18
--17
--16
15
--14
--13
--12
--0
line:1000 Pass
line:1001 Pass
line:1004 Pass
line:1005 Pass
12 14 15 Memory leak report:
No memory leak.