这是目前难度最高的一个作业,主要难点在于树的平衡,树的平衡依赖于调试输出的图形化,也就是输出二叉树的实现,二叉树的输出技巧性比较强,一般人很难直接想到控制台可以打印二叉树。后面的测试结果显示本文实现的AVLTree 4次战胜std::map。
C++数据结构与算法夯实基础作业列表
平衡部分的调试最好用很少很少量的数据,把各种情况跑一遍,因为一旦不考虑测试集的大小,容易给自己添麻烦。测试集大了之后,调试起来很不方便,哪怕是有输出工具的帮忙。
本作业主要考察:二叉树的平衡、平衡的时机、树的迭代器、模板容器、与二叉搜索树的性能比较
需求如下:
实现一个平衡二叉树
by inserting one record at a time to the tree. It then provides the following menu:
1. Search for a record
2. Insert a record
3. Delete a record
4. List all records
5. Exit
类设计与示例类图:
AVLTree内部直接创建了一个BinarySearchTree,只是多了对BinarySearchTree的再平衡操作;平衡的动作就是AVLTree的成员函数;两种Tree公用TreeNode类型,其中TreeNode的height成员只有AVLTree会使用;两种Tree公用TreeIterator;
部分示例接口与main函数,编程实现代码通过main函数的功能测试:
AVLTree.h
#pragma once
BinarySearchTree.h
#ifndef _BINARY_TREE_H_
Entry.h
#pragma once
Timer.h
#pragma once
TreeIterator.h
#pragma once
TreeNode.h
#pragma once
main.cpp
#include
功能与输出:
Please input menu:
0 : Draw the tree.
1 : Search a record.
2 : Insert a record.
3 : Delete a record.
4 : List all reacords.
5 : Exit.
6 : Compare AVLT with BST use [insert/ find/ delete].
0
The tree is(in-order):
---(06115, 105, "Yuba, CA")
---(06113, 438, "Yolo, CA")
---(06111, 1130, "Ventura, CA")
---(06109, 3, "Tuolumne, CA")
---(06107, 577, "Tulare, CA")
---(06105, 0, "Trinity, CA")
---(06103, 25, "Tehama, CA")
---(06101, 172, "Sutter, CA")
---(06099, 576, "Stanislaus, CA")
---(06097, 655, "Sonoma, CA")
---(06095, 570, "Solano, CA")
---(06093, 4, "Siskiyou, CA")
---(06091, 0, "Sierra, CA")
---(06089, 29, "Shasta, CA")
---(06087, 373, "Santa Cruz, CA")
---(06085, 5889, "Santa Clara, CA")
---(06083, 721, "Santa Barbara, CA")
---(06081, 1743, "San Mateo, CA")
---(06079, 171, "San Luis Obispo, CA")
---(06077, 795, "San Joaquin, CA")
---(06075, 2039, "San Francisco, CA")
---(06073, 5351, "San Diego, CA")
---(06071, 1920, "San Bernardino, CA")
---(06069, 94, "San Benito, CA")
---(06067, 1809, "Sacramento, CA")
---(06065, 1784, "Riverside, CA")
---(06063, 0, "Plumas, CA")
---(06061, 162, "Placer, CA")
---(06059, 6214, "Orange, CA")
---(06057, 26, "Nevada, CA")
---(06055, 225, "Napa, CA")
---(06053, 1122, "Monterey, CA")
---(06051, 19, "Mono, CA")
06049, 0, "Modoc, CA"
---(06047, 341, "Merced, CA")
---(06045, 102, "Mendocino, CA")
---(06043, 1, "Mariposa, CA")
---(06041, 399, "Marin, CA")
---(06039, 221, "Madera, CA")
---(06037, 22851, "Los Angeles, CA")
---(06035, 1, "Lassen, CA")
---(06033, 32, "Lake, CA")
---(06031, 205, "Kings, CA")
---(06029, 875, "Kern, CA")
---(06027, 8, "Inyo, CA")
---(06025, 295, "Imperial, CA")
---(06023, 42, "Humboldt, CA")
---(06021, 36, "Glenn, CA")
---(06019, 1242, "Fresno, CA")
---(06017, 90, "El Dorado, CA")
---(06015, 19, "Del Norte, CA")
---(06013, 1372, "Contra Costa, CA")
---(06011, 60, "Colusa, CA")
---(06009, 1, "Calaveras, CA")
---(06007, 150, "Butte, CA")
---(06005, 1, "Amador, CA")
---(06003, 0, "Alpine, CA")
---(06001, 3648, "Alameda, CA")
Please input menu:
0 : Draw the tree.
1 : Search a record.
2 : Insert a record.
3 : Delete a record.
4 : List all reacords.
5 : Exit.
6 : Compare AVLT with BST use [insert/ find/ delete].
1
Please input the code of the state:
06105
Find : 06105, 0, "Trinity, CA"
time elasped:0
Please input menu:
0 : Draw the tree.
1 : Search a record.
2 : Insert a record.
3 : Delete a record.
4 : List all reacords.
5 : Exit.
6 : Compare AVLT with BST use [insert/ find/ delete].
6
Please input the scale number for test:100000
CompareBSTAndAVLTWithSortedData:
Insert 100000 times to AVLTree cost 30 milliseconds,
Insert 100000 times to std::map cost 19 milliseconds,
Insert 100000 times to BST cost 27367 milliseconds,
Search 100000 times from AVLTree cost 5 milliseconds,
Search 100000 times from std::map cost 9 milliseconds,
Search 100000 times from BST cost 26900 milliseconds,
Delete 100000 times from AVLTree cost 7 milliseconds,
Delete 100000 times from std::map cost 8 milliseconds,
Delete 100000 times from BST cost 6 milliseconds,
CompareBSTAndAVLTWithRandomData:
Insert 100000 times to AVLTree cost 49 milliseconds,
Insert 100000 times to std::map cost 42 milliseconds,
Insert 100000 times to BST cost 33 milliseconds,
Search 100000 times from AVLTree cost 24 milliseconds,
Search 100000 times from std::map cost 30 milliseconds,
Search 100000 times from BST cost 24 milliseconds,
Delete 100000 times from AVLTree cost 30 milliseconds,
Delete 100000 times from std::map cost 37 milliseconds,
Delete 100000 times from BST cost 32 milliseconds,
The compare result have written to compare.txt
Please input menu:
0 : Draw the tree.
1 : Search a record.
2 : Insert a record.
3 : Delete a record.
4 : List all reacords.
5 : Exit.
6 : Compare AVLT with BST use [insert/ find/ delete].
0
The tree is(in-order):
---(06115, 105, "Yuba, CA")
---(06113, 438, "Yolo, CA")
---(06111, 1130, "Ventura, CA")
---(06109, 3, "Tuolumne, CA")
---(06107, 577, "Tulare, CA")
---(06105, 0, "Trinity, CA")
---(06103, 25, "Tehama, CA")
---(06101, 172, "Sutter, CA")
---(06099, 576, "Stanislaus, CA")
---(06097, 655, "Sonoma, CA")
---(06095, 570, "Solano, CA")
---(06093, 4, "Siskiyou, CA")
---(06091, 0, "Sierra, CA")
---(06089, 29, "Shasta, CA")
---(06087, 373, "Santa Cruz, CA")
---(06085, 5889, "Santa Clara, CA")
---(06083, 721, "Santa Barbara, CA")
---(06081, 1743, "San Mateo, CA")
---(06079, 171, "San Luis Obispo, CA")
---(06077, 795, "San Joaquin, CA")
---(06075, 2039, "San Francisco, CA")
---(06073, 5351, "San Diego, CA")
---(06071, 1920, "San Bernardino, CA")
---(06069, 94, "San Benito, CA")
---(06067, 1809, "Sacramento, CA")
---(06065, 1784, "Riverside, CA")
---(06063, 0, "Plumas, CA")
---(06061, 162, "Placer, CA")
---(06059, 6214, "Orange, CA")
---(06057, 26, "Nevada, CA")
---(06055, 225, "Napa, CA")
---(06053, 1122, "Monterey, CA")
---(06051, 19, "Mono, CA")
06049, 0, "Modoc, CA"
---(06047, 341, "Merced, CA")
---(06045, 102, "Mendocino, CA")
---(06043, 1, "Mariposa, CA")
---(06041, 399, "Marin, CA")
---(06039, 221, "Madera, CA")
---(06037, 22851, "Los Angeles, CA")
---(06035, 1, "Lassen, CA")
---(06033, 32, "Lake, CA")
---(06031, 205, "Kings, CA")
---(06029, 875, "Kern, CA")
---(06027, 8, "Inyo, CA")
---(06025, 295, "Imperial, CA")
---(06023, 42, "Humboldt, CA")
---(06021, 36, "Glenn, CA")
---(06019, 1242, "Fresno, CA")
---(06017, 90, "El Dorado, CA")
---(06015, 19, "Del Norte, CA")
---(06013, 1372, "Contra Costa, CA")
---(06011, 60, "Colusa, CA")
---(06009, 1, "Calaveras, CA")
---(06007, 150, "Butte, CA")
---(06005, 1, "Amador, CA")
---(06003, 0, "Alpine, CA")
---(06001, 3648, "Alameda, CA")
Please input menu:
0 : Draw the tree.
1 : Search a record.
2 : Insert a record.
3 : Delete a record.
4 : List all reacords.
5 : Exit.
6 : Compare AVLT with BST use [insert/ find/ delete].
4
List all records( see also in file ListAllRecordsData.txt):
06001, 3648, "Alameda, CA"
06003, 0, "Alpine, CA"
06005, 1, "Amador, CA"
06007, 150, "Butte, CA"
06009, 1, "Calaveras, CA"
06011, 60, "Colusa, CA"
06013, 1372, "Contra Costa, CA"
06015, 19, "Del Norte, CA"
06017, 90, "El Dorado, CA"
06019, 1242, "Fresno, CA"
06021, 36, "Glenn, CA"
06023, 42, "Humboldt, CA"
06025, 295, "Imperial, CA"
06027, 8, "Inyo, CA"
06029, 875, "Kern, CA"
06031, 205, "Kings, CA"
06033, 32, "Lake, CA"
06035, 1, "Lassen, CA"
06037, 22851, "Los Angeles, CA"
06039, 221, "Madera, CA"
06041, 399, "Marin, CA"
06043, 1, "Mariposa, CA"
06045, 102, "Mendocino, CA"
06047, 341, "Merced, CA"
06049, 0, "Modoc, CA"
06051, 19, "Mono, CA"
06053, 1122, "Monterey, CA"
06055, 225, "Napa, CA"
06057, 26, "Nevada, CA"
06059, 6214, "Orange, CA"
06061, 162, "Placer, CA"
06063, 0, "Plumas, CA"
06065, 1784, "Riverside, CA"
06067, 1809, "Sacramento, CA"
06069, 94, "San Benito, CA"
06071, 1920, "San Bernardino, CA"
06073, 5351, "San Diego, CA"
06075, 2039, "San Francisco, CA"
06077, 795, "San Joaquin, CA"
06079, 171, "San Luis Obispo, CA"
06081, 1743, "San Mateo, CA"
06083, 721, "Santa Barbara, CA"
06085, 5889, "Santa Clara, CA"
06087, 373, "Santa Cruz, CA"
06089, 29, "Shasta, CA"
06091, 0, "Sierra, CA"
06093, 4, "Siskiyou, CA"
06095, 570, "Solano, CA"
06097, 655, "Sonoma, CA"
06099, 576, "Stanislaus, CA"
06101, 172, "Sutter, CA"
06103, 25, "Tehama, CA"
06105, 0, "Trinity, CA"
06107, 577, "Tulare, CA"
06109, 3, "Tuolumne, CA"
06111, 1130, "Ventura, CA"
06113, 438, "Yolo, CA"
06115, 105, "Yuba, CA"
Please input menu:
0 : Draw the tree.
1 : Search a record.
2 : Insert a record.
3 : Delete a record.
4 : List all reacords.
5 : Exit.
6 : Compare AVLT with BST use [insert/ find/ delete].
测试结果说明:
1 可以看出二叉搜索树在插入排序数据后退化为单链表,在Insert和Search的时候耗时非常高。平衡二叉树不存在这个问题,对于排序数据和随机数据都一样支持的很好。
2 从测试结果可以看出AVLTree整体上和std::map一个水平,四次用时超越std::map