Sie sind auf Seite 1von 2

Part

1 :



This screenshot shows that after the balanceBST() function is called, it prints that it has been
successfully created into a balanced binary search tree. This function takes an array, a start and
an end and it recursively splits the array into subtrees, making sure to end up with a balanced
bst. The time complexity of this algorithm is O(n).

Part 2:



This screenshot shows that after the inOrder() function is called, it prints the contents of the
balanced BST after performing an in order traversal. In Order is a recursive function that checks
to make sure that the node is not null and then prints all of the contents on the left, the root,
and then the right.


Part 3:



This screenshot shows that after the leafHeights function is called it prints each of the leaves in
the BST as well as their heights in the BST. This function is also recursive, and takes the pointer
to the BST location and the height as the arguments, and then checks to see if there are nodes
on both the right and left, and if there are none, it concludes that the node is a leaf. This
function has a time complexity of O(n), where n is nodes in the BST.
Part 4 :



This screenshot shows that when the newSearch function is called and 0 is searched for, it
returns 1.



This screenshot shows that when the newSearch function is called and 8 is searched for, it is
not found.



This screenshot shows that when the newSearch function is called and an element that is in the
BST exists, it properly prints that out.

newSearch takes a BST location pointer and an integer as the arguments. The integer is passed
in from the user and it is also recursive, checking to see if the node is a leaf and whether or not
the node is less than what the user input. If the note is greater than or equal to the user’s input
and it’s left node is null, then it will return the current node. If the node is greater than or equal
to the user’s input and the left node’s contents are less than the user’s input, it will also return
the current node. This function has a time complexity of O(h), where h is the height of the tree.

Das könnte Ihnen auch gefallen