Final Report
Final Report
CST 370
2/21/18
1. // Algorithm NewSearch(num)
2. // Input: integer num
3. // Output: Returns value if found, else return the smallest greater
4. // number or 0 (with message) if no larger value found.
5.
6. currBinNode ← myRoot
7. tempParent ← currBinNode
8. bool found ← false
9.
10. // Go down the tree to find equal value or until it reaches a null node
11. while (found = false && currBinNode ≠ 0)
12. tempParent ← currBinNode
13. if (num < currBinNode->data) // descend left
14. currBinNode ← currBinNode->left
15. else if (currBinNode->data < num) // descend right
16. currBinNode ← currBinNode->right
17. else // item found
18. found ← true
19.
20. // Return if found or if num if less than all values
21. if (found || tempParent->data > num)
22. return tempParent->data
23.
24. currBinNode ← tempParent
25. if (currBinNode->right ≠ 0)
26. currBinNode ← currBinNode->right
27.
28. // Go left until next left node is null
29. while (currBinNode->left ≠ 0)
30. currBinNode ← currBinNode->left
31. return currBinNode->data
32.
33. if (currBinNode->parent = 0)
34. cout: "*No greater than or equal value found*"
35. return 0
36.
37. // Go up in the tree until a right parent is found
38. while (currBinNode->parent->data < currBinNode->data)
39. currBinNode ← currBinNode->parent
40. if (currBinNode->parent = NULL)
41. cout: "no value greater than or equal"
42. return 0
43. return currBinNode->parent->data
Moises Bernal
CST 370
2/21/18
---------------------------------------------------TESTING (SCREENSHOTS)----------------------------------------------------
This screenshot shows the test on a balanced BST. The blue is when the getBalancedBST() was called.
The height of leaf nodes did not differ by more than 1 (as seen on red), so a balanced BST was correctly
created. The orange correctly shows the output of the balanced BST traversed in order "1 4 6 7 10 11 13
20 22 27 40 42 100". The red correctly shows the height values (3,3,3,3,3,3) of the corresponding leaf
nodes (4,7,11,22,40,100). The green shows a test for NewSearch() with correct outputs (1, 100, 1, 13,
0[*No greater than or equal value found*]) of corresponding searches (1,100,0,12,200).
Moises Bernal
CST 370
2/21/18
This screenshot shows the test on a BST that is not balanced. The orange correctly shows the output of
the BST traversed in order "2 3 4 6 10 20 22 35 44 70 150". The red correctly shows the height values
(3,5,4,2,2) of the corresponding leaf nodes (2,6,20,35,70). The green shows a test for NewSearch() with
correct outputs (2, 20, 150, 0[*No greater than or equal value found*]) of corresponding searches
(1,11,150,151).
Moises Bernal
CST 370
2/21/18
--------------------------------------------------------TIME COMPLEXITY---------------------------------------------------------
The function getBalancedBST() calls two functions, mergeSort() and then sortedToBalancedBST().
Since 𝑇𝑀𝑒𝑟𝑔𝑒𝑆𝑜𝑟𝑡 (𝑛) ∈ 𝑂(𝑛 log 2 𝑛) and 𝑇𝑠𝑜𝑟𝑡𝑒𝑑𝑇𝑜𝐵𝑎𝑙𝑎𝑛𝑐𝑒𝑑𝐵𝑆𝑇 (𝑛) ∈ 𝑂(𝑛 log 2 𝑛),
= (𝑐1 + 𝑐2 )𝑛 log 2 𝑛
= 𝐶𝑛 log 2 𝑛 where 𝐶 = 𝑐1 + 𝑐2
Since 𝑇𝑔𝑒𝑡𝐵𝑎𝑙𝑎𝑛𝑐𝑒𝑑𝐵𝑆𝑇 (𝑛) = 𝐶𝑛 log 2 𝑛 for constant 𝐶, 𝑻𝒈𝒆𝒕𝑩𝒂𝒍𝒂𝒏𝒄𝒆𝒅𝑩𝑺𝑻 (𝒏) ∈ 𝑶(𝒏 𝐥𝐨𝐠 𝟐 𝒏).
The function sortedToBalancedBST() traverses every node in preorder and performs 𝑐(log 2 𝑛)
calculations (since it calls insert and 𝑇𝑖𝑛𝑠𝑒𝑟𝑡 (𝑛) ∈ 𝑂(log 2 𝑛)) each time, where 𝑐 is a constant.
= 𝐶𝑛 log 2 𝑛 where 𝐶 = 𝑐1 𝑐2 .
Since 𝑇𝑠𝑜𝑟𝑡𝑒𝑑𝑇𝑜𝐵𝑎𝑙𝑎𝑛𝑐𝑒𝑑𝐵𝑆𝑇 (𝑛) = 𝐶𝑛 log 2 𝑛 for constant 𝐶, 𝑻𝒔𝒐𝒓𝒕𝒆𝒅𝑻𝒐𝑩𝒂𝒍𝒂𝒏𝒄𝒆𝒅𝑩𝑺𝑻 (𝒏) ∈ 𝑶(𝒏 𝐥𝐨𝐠 𝟐 𝒏).
𝑛
𝑇𝑚𝑒𝑟𝑔𝑒𝑆𝑜𝑟𝑡 (𝑛) = 2𝑇𝑚𝑒𝑟𝑔𝑒𝑆𝑜𝑟𝑡 (2 ) + 𝑛
𝑛
= 22 𝑇𝑚𝑒𝑟𝑔𝑒𝑆𝑜𝑟𝑡 (22 ) + 2𝑛
𝑛
= 23 𝑇𝑚𝑒𝑟𝑔𝑒𝑆𝑜𝑟𝑡 (23 ) + 3𝑛
⋮
𝑛
= 2𝑖 𝑇𝑚𝑒𝑟𝑔𝑒𝑆𝑜𝑟𝑡 (2𝑖) + 𝑖𝑛
𝑛
Let 2𝑖 = 1, then 𝑛 = 2𝑖 ⇒ 𝑖 = log 2 𝑛.
By substitution,
Moises Bernal
CST 370
2/21/18
= 2𝑛 log 2 𝑛
Since 𝑇𝑚𝑒𝑟𝑔𝑒𝑆𝑜𝑟𝑡 (𝑛) ≤ 2𝑛 log 2 𝑛 and 2 is an integer, 𝑻𝒎𝒆𝒓𝒈𝒆𝑺𝒐𝒓𝒕 (𝒏) ∈ 𝑶(𝒏 𝐥𝐨𝐠 𝟐 𝒏).
𝑇𝑖𝑛𝑂𝑟𝑑𝑒𝑟 (𝑛) = 𝑇𝑖𝑛𝑂𝑟𝑑𝑒𝑟 (𝑘) + 𝑇𝑖𝑛𝑂𝑟𝑑𝑒𝑟 (𝑛 − 𝑘 − 1) + 𝑐 where constant 𝑐 𝜖 ℤ , 𝑘 is the nodes on one size
of root, and 𝑛 is the nodes in the tree.
⋮
= 𝑖𝑇𝑖𝑛𝑂𝑟𝑑𝑒𝑟 (0) + 𝑇𝑖𝑛𝑂𝑟𝑑𝑒𝑟 (𝑛 − 𝑖) + (𝑐0 + 𝑐1 + ⋯ + 𝑐𝑖 ) for 𝑘 = 𝑖
Let 𝑛 − 𝑖 = 0, then 𝑛 = 𝑖.
By substitution,
≤ 𝑛𝑑 + 𝑛𝑑 + 𝑛𝐶
= 𝑛(𝑑 + 𝑑 + 𝐶)
= 𝑛t where 𝑡 = 2𝑑 + 𝐶
∗ 𝑻𝒑𝒓𝒆𝑶𝒓𝒅𝒆𝒓 (𝒏) and 𝑻𝒑𝒐𝒔𝒕𝑶𝒓𝒅𝒆𝒓 (𝒏) have the same time complexity.
Moises Bernal
CST 370
2/21/18
The function LeafHeights() traverses every node in preorder and performs 𝑐 calculations at each step,
where 𝑐 is a constant.
In the worst case, the BST is a tree in which the head has no right node and left subtree is right skewed
with search value greater than every node value except root and not equal to root (see image below).
= 3𝐶𝑛
= 𝑛𝑡 where constant 𝑡 = 3𝐶.