Вы находитесь на странице: 1из 7

Moises Bernal

CST 370
2/21/18

Final Exam Report

----------------------------------------------------------- ALGORITHMS ------------------------------------------------------------

PART 1: Pseudo Code for getBalancedBST() and sortedToBalancedBST()

1. // Algorithm getBalancedBST(arr, size)


2. // Input: array arr and integer size
3. // Output: Returns a balanced BST from an unsorted array of integers
4.
5. arr ← mergeSort(arr) // Sort the array using mergeSort
6.
7. // Create a BST using the sortedToBalancedBST function
8. balancedBST ← sortedToBalancedBST()
9. return balancedBST

1. // Algorithm sortedToBalancedBST(sortedArray, newBST, start, end)


2. // Input: array sortedArray, BST pointer newBST, and two integers start/end
3. // Output: Constructs a balanced binary search tree from a sorted array
4.
5. if (start > end)
6. return
7. int mid ← (start + end) / 2
8. newBST->insert(sortedArray[mid])
9. sortedToBalancedBST() // From start to mid - 1
10. sortedToBalancedBST() // From mid + 1 to end

PART 3: Pseudo Code for LeafHeights()

1. // Algorithm LeafHeights(out, height, subtreeRoot)


2. // Input: ostream out, integer height, and BinNode pointer subtreeRoot
3. // Output: Calculates and prints the heights of all leaf nodes
4.
5. height++
6. if (subtreeRoot = 0)
7. return
8. if (subtreeRoot->left = 0 && subtreeRoot->right = 0)
9. out: subtreeRoot->data and height // display node data and height
10. return
11. LeafHeights() // For subtreeRoot->left
12. LeafHeights() // For subtreeRoot->right
Moises Bernal
CST 370
2/21/18

PART 4: Pseudo Code for NewSearch()

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)----------------------------------------------------

TEST FOR BALANCED BST

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

TEST FOR NOT BALANCED BST

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---------------------------------------------------------

-------------------------------------- PART 1: Time Complexity of getBalancedBST() ------------------------------------

The function getBalancedBST() calls two functions, mergeSort() and then sortedToBalancedBST().

Since 𝑇𝑀𝑒𝑟𝑔𝑒𝑆𝑜𝑟𝑡 (𝑛) ∈ 𝑂(𝑛 log 2 𝑛) and 𝑇𝑠𝑜𝑟𝑡𝑒𝑑𝑇𝑜𝐵𝑎𝑙𝑎𝑛𝑐𝑒𝑑𝐵𝑆𝑇 (𝑛) ∈ 𝑂(𝑛 log 2 𝑛),

𝑇𝑔𝑒𝑡𝐵𝑎𝑙𝑎𝑛𝑐𝑒𝑑𝐵𝑆𝑇 (𝑛) = 𝑐1 𝑛 log 2 𝑛 + 𝑐2 𝑛 log 2 𝑛 for constants 𝑐1 and 𝑐2 .

= (𝑐1 + 𝑐2 )𝑛 log 2 𝑛
= 𝐶𝑛 log 2 𝑛 where 𝐶 = 𝑐1 + 𝑐2

Since 𝑇𝑔𝑒𝑡𝐵𝑎𝑙𝑎𝑛𝑐𝑒𝑑𝐵𝑆𝑇 (𝑛) = 𝐶𝑛 log 2 𝑛 for constant 𝐶, 𝑻𝒈𝒆𝒕𝑩𝒂𝒍𝒂𝒏𝒄𝒆𝒅𝑩𝑺𝑻 (𝒏) ∈ 𝑶(𝒏 𝐥𝐨𝐠 𝟐 𝒏).

Time complexities for mergeSort and sortedToBalancedBST below:

Time Complexity of sortedToBalancedBST()

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.

Since 𝑇𝑝𝑟𝑒𝑂𝑟𝑑𝑒𝑟 (𝑛) ∈ 𝑂(𝑛),

𝑇𝑠𝑜𝑟𝑡𝑒𝑑𝑇𝑜𝐵𝑎𝑙𝑎𝑛𝑐𝑒𝑑𝐵𝑆𝑇 (𝑛) = 𝑐1 𝑐2 𝑛 log 2 𝑛 for constants 𝑐1 and 𝑐2 .

= 𝐶𝑛 log 2 𝑛 where 𝐶 = 𝑐1 𝑐2 .

Since 𝑇𝑠𝑜𝑟𝑡𝑒𝑑𝑇𝑜𝐵𝑎𝑙𝑎𝑛𝑐𝑒𝑑𝐵𝑆𝑇 (𝑛) = 𝐶𝑛 log 2 𝑛 for constant 𝐶, 𝑻𝒔𝒐𝒓𝒕𝒆𝒅𝑻𝒐𝑩𝒂𝒍𝒂𝒏𝒄𝒆𝒅𝑩𝑺𝑻 (𝒏) ∈ 𝑶(𝒏 𝐥𝐨𝐠 𝟐 𝒏).

Time Complexity of mergeSort()


𝑛 𝑛
𝑇𝑚𝑒𝑟𝑔𝑒𝑆𝑜𝑟𝑡 (𝑛) = 𝑇𝑚𝑒𝑟𝑔𝑒𝑆𝑜𝑟𝑡 (2 ) + 𝑇𝑚𝑒𝑟𝑔𝑒𝑆𝑜𝑟𝑡 (2 ) + 𝑛 where 𝑛 is the nodes in the tree.

𝑛
𝑇𝑚𝑒𝑟𝑔𝑒𝑆𝑜𝑟𝑡 (𝑛) = 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

𝑇𝑚𝑒𝑟𝑔𝑒𝑆𝑜𝑟𝑡 (𝑛) = 𝑛𝑇𝑚𝑒𝑟𝑔𝑒𝑆𝑜𝑟𝑡 (1) + 𝑛 log 2 𝑛

= 𝑛𝐶 + 𝑛 log 2 𝑛 for some constant 𝐶

≤ 𝑛 log 2 𝑛 + 𝑛 log 2 𝑛 for 𝑛 > 𝑑 where 𝑑 is some integer

= 2𝑛 log 2 𝑛
Since 𝑇𝑚𝑒𝑟𝑔𝑒𝑆𝑜𝑟𝑡 (𝑛) ≤ 2𝑛 log 2 𝑛 and 2 is an integer, 𝑻𝒎𝒆𝒓𝒈𝒆𝑺𝒐𝒓𝒕 (𝒏) ∈ 𝑶(𝒏 𝐥𝐨𝐠 𝟐 𝒏).

------------------------------------------ PART 2: Time Complexity of inOrder() ------------------------------------------

𝑇𝑖𝑛𝑂𝑟𝑑𝑒𝑟 (𝑛) = 𝑇𝑖𝑛𝑂𝑟𝑑𝑒𝑟 (𝑘) + 𝑇𝑖𝑛𝑂𝑟𝑑𝑒𝑟 (𝑛 − 𝑘 − 1) + 𝑐 where constant 𝑐 𝜖 ℤ , 𝑘 is the nodes on one size
of root, and 𝑛 is the nodes in the tree.

Suppose the worst case (skewed tree):

𝑇𝑖𝑛𝑂𝑟𝑑𝑒𝑟 (𝑛) = 𝑇𝑖𝑛𝑂𝑟𝑑𝑒𝑟 (0) + 𝑇𝑖𝑛𝑂𝑟𝑑𝑒𝑟 (𝑛 − 1) + 𝑐0 for 𝑘 = 0

= 2𝑇(0) + 𝑇𝑖𝑛𝑂𝑟𝑑𝑒𝑟 (𝑛 − 2) + (𝑐0 + 𝑐1 ) for 𝑘 = 1

= 3𝑇(0) + 𝑇𝑖𝑛𝑂𝑟𝑑𝑒𝑟 (𝑛 − 3) + (𝑐0 + 𝑐1 + 𝑐2 ) for 𝑘 = 2


= 𝑖𝑇𝑖𝑛𝑂𝑟𝑑𝑒𝑟 (0) + 𝑇𝑖𝑛𝑂𝑟𝑑𝑒𝑟 (𝑛 − 𝑖) + (𝑐0 + 𝑐1 + ⋯ + 𝑐𝑖 ) for 𝑘 = 𝑖

≤ 𝑖𝑇𝑖𝑛𝑂𝑟𝑑𝑒𝑟 (0) + 𝑇𝑖𝑛𝑂𝑟𝑑𝑒𝑟 (𝑛 − 𝑖) + 𝑖𝐶 for some constant 𝐶 𝜖 ℤ

≤ 𝑖𝑇𝑖𝑛𝑂𝑟𝑑𝑒𝑟 (0) + 𝑇𝑖𝑛𝑂𝑟𝑑𝑒𝑟 (𝑛 − 𝑖) + 𝑖𝐶 for some constant 𝐶 𝜖 ℤ

Let 𝑛 − 𝑖 = 0, then 𝑛 = 𝑖.

By substitution,

𝑇𝑖𝑛𝑂𝑟𝑑𝑒𝑟 (𝑛) ≤ 𝑛𝑇𝑖𝑛𝑂𝑟𝑑𝑒𝑟 (0) + 𝑇𝑖𝑛𝑂𝑟𝑑𝑒𝑟 (0) + 𝑛𝐶


= 𝑛𝑑 + 𝑑 + 𝑛𝐶 for some constant 𝑑 𝜖 ℤ

≤ 𝑛𝑑 + 𝑛𝑑 + 𝑛𝐶
= 𝑛(𝑑 + 𝑑 + 𝐶)
= 𝑛t where 𝑡 = 2𝑑 + 𝐶

Since 𝑇𝑖𝑛𝑂𝑟𝑑𝑒𝑟 (𝑛) ≤ 𝑛𝑡 for some constant 𝑡 𝜖 ℤ, 𝑻𝒊𝒏𝑶𝒓𝒅𝒆𝒓 (𝒏) ∈ 𝑶(𝒏).

∗ 𝑻𝒑𝒓𝒆𝑶𝒓𝒅𝒆𝒓 (𝒏) and 𝑻𝒑𝒐𝒔𝒕𝑶𝒓𝒅𝒆𝒓 (𝒏) have the same time complexity.
Moises Bernal
CST 370
2/21/18

----------------------------------------- PART 3: Time Complexity of LeafHeights() ---------------------------------------

The function LeafHeights() traverses every node in preorder and performs 𝑐 calculations at each step,
where 𝑐 is a constant.

Since 𝑇𝑝𝑟𝑒𝑂𝑟𝑑𝑒𝑟 (𝑛) ∈ 𝑂(𝑛),

𝑇𝑝𝑟𝑒𝑂𝑟𝑑𝑒𝑟 (𝑛) = 𝑛𝑡 for some constant 𝑡.

So, 𝑇𝐿𝑒𝑎𝑓𝐻𝑒𝑖𝑔ℎ𝑡𝑠 (𝑛) = 𝑐𝑇𝑝𝑟𝑒𝑂𝑟𝑑𝑒𝑟 (𝑛) = 𝑛𝑡𝑐 for some constant 𝑡.

Let 𝐶 = 𝑡𝑐. Then, 𝑇𝐿𝑒𝑎𝑓𝐻𝑒𝑖𝑔ℎ𝑡𝑠 (𝑛) = 𝑛𝐶 for some constant 𝐶.

So, 𝑻𝑳𝒆𝒂𝒇𝑯𝒆𝒊𝒈𝒉𝒕𝒔 (𝒏) ∈ 𝑶(𝒏).

----------------------------------------- PART 4: Time Complexity of NewSearch() ----------------------------------------

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).

In this case, the function NewSearch() will run the first


loop (ℎ = 𝑛 − 1) times to get to the bottom right node of
the left subtree, where ℎ is the height of the tree and 𝑛 is
the number of nodes in the tree, performing constant
𝑐1 operations. Then, in the second loop, it will move up
the tree (𝑛 − 2) times performing constant 𝑐2 operations.
Then, in the third loop, it will move up the tree (1) time
performing constant 𝑐3 operations. Outside of the loop it
will perform constant 𝑐4 operations.

𝑇𝑁𝑒𝑤𝑆𝑒𝑎𝑟𝑐ℎ (𝑛) = (𝑛 − 1)𝑐1 + (𝑛 − 2)𝑐2 + 𝑐3 + 𝑐4


where 𝑛 is the nodes in the tree and 𝑐1 , 𝑐2 , 𝑐3 , 𝑐4 are constants. Let constant 𝐶 > 𝑐1 , 𝑐2 , 𝑐3 , 𝑐4 . Then,

𝑇𝑁𝑒𝑤𝑆𝑒𝑎𝑟𝑐ℎ (𝑛) ≤ (𝑛 − 1)𝐶 + (𝑛 − 2)𝐶 + 2𝐶


< 𝑛𝐶 + 𝑛𝐶 + 𝑛𝐶 for 𝑛 > 2

= 3𝐶𝑛
= 𝑛𝑡 where constant 𝑡 = 3𝐶.

Since 𝑇𝑁𝑒𝑤𝑆𝑒𝑎𝑟𝑐ℎ (𝑛) ≤ 𝑛𝑡 for some constant 𝑡 𝜖 ℤ, 𝑻𝑵𝒆𝒘𝑺𝒆𝒂𝒓𝒄𝒉 (𝒏) ∈ 𝑶(𝒏).

Вам также может понравиться