Sort the following list of numbers using BST sort: 144, 151, 162, 70, 41, 120, 158, 124, 123, 93, 12, 23, 68.Display the numbers in the sorted order. Draw the tree at each step and give the step-by-step workout.
Question
Sort the following list of numbers using BST sort: 144, 151, 162, 70, 41, 120, 158, 124, 123, 93, 12, 23, 68.Display the numbers in the sorted order. Draw the tree at each step and give the step-by-step workout.
Solution
Sure, here is the step-by-step workout for sorting the given list of numbers using Binary Search Tree (BST) sort:
-
First, we start by creating a binary search tree. The first number in the list, 144, becomes the root of the tree.
-
Next, we insert the second number, 151. Since 151 is greater than 144, it becomes the right child of 144.
-
We then insert the third number, 162. Since 162 is greater than 144 and 151, it becomes the right child of 151.
-
The fourth number is 70. Since 70 is less than 144, it becomes the left child of 144.
-
The fifth number is 41. Since 41 is less than 144 and 70, it becomes the left child of 70.
-
We continue this process for the remaining numbers. The number 120 is less than 144 but greater than 70, so it becomes the right child of 70. The number 158 is less than 162 but greater than 151, so it becomes the left child of 162. The numbers 124, 123, and 93 are all less than 144 but greater than 120, so they are inserted to the right of 120 in that order. The numbers 12 and 23 are less than 70 and 41, so they are inserted to the left of 41 in that order. The number 68 is less than 70 but greater than 41, so it becomes the right child of 41.
-
Once all the numbers are inserted, we perform an in-order traversal of the tree to get the sorted list of numbers. In an in-order traversal, we first visit the left child, then the parent, and finally the right child.
-
The in-order traversal gives us the sorted list of numbers: 12, 23, 41, 68, 70, 93, 120, 123, 124, 144, 151, 158, 162.
Please note that drawing the tree at each step is not possible in this text-based format. However, you can easily draw it on a piece of paper by following the steps above.
Similar Questions
Given the sequence of integers: '90, 71, 110, 103, 111, 69, 83, 70, 81, 84, 51, 104, 99', draw a BST and the select the correct sequence of the Pre-order traversal after adding a new node with the value '99' to the BST. (You may need to draw a tree to solve this) Question 28Select one: 111, 104, 99, 103, 110, 84, 83, 81, 71, 70, 69, 51, 90 90, 71, 69, 70, 51, 83, 81, 84, 110, 103, 99, 111, 014 90, 51, 111, 104, 99, 103, 110, 84, 83, 81, 71, 70, 69 90, 71, 69, 51, 70, 83, 81, 84, 110, 103, 99, 104, 111
Sort the following number using quick sort and heap sort.10, 15, 98, 78, 115, 124, 42, 20, 28, 118, 160, 054. With suitable example explain various types of tree traversal methods.
Level Order of TreeGiven an array of unique elements, construct a Binary Search Tree and print the Level Order of the tree.Input FormatFirst line of each test case contains N - number of nodes in the BST. The next line contains N unique integers - value of the nodes.ConstraintsPrint the Level Order of the Binary Search Tree, separate each level by newline.Output Format1 <= N <= 10000 <= ar[i] <= 10000Sample input6 10 5 15 2 7 12Sample output10 5 15 2 7 12
Find preorder traversal of height balanced BSTGiven a sorted array. Convert it into a Height balanced Binary Search Tree (BST). Find the preorder traversal of height balanced BST. If there exist many such balanced BST consider the tree whose preorder is lexicographically smallest.Height balanced BST means a binary tree in which the depth of the left subtree and the right subtree of every node never differ by more than 1.Sample input25 1 4 5 7 8 12 14 15 19 20 22 25 26 27 28 29 33 34 37 40 41 42 43 47 48Sample outputoutput=26 12 5 1 4 7 8 19 14 15 22 20 25 37 2
Which traversal method is used to get the elements of a BST in non-decreasing order?
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.