Question

(20 points) Suppose you are given a binary search tree T of n nodes (as discussed in class. each node v has v.left, v.right,
0 0
Add a comment Improve this question Transcribed image text
Answer #1

The implementation is as follows,

class Node:

    def __init__(self, data):

        self.key = data

        self.left = None

        self.right = None

def KSmallestUsingMorris(root, k):

    count = 0

  

    ksmall = -9999999999

    curr = root

  

    while curr != None:

if curr.left == None:

            count += 1

if count == k:

                ksmall = curr.key

curr = curr.right

        else:

pre = curr.left

            while (pre.right != None and

                   pre.right != curr):

                pre = pre.right

if pre.right == None:

                pre.right = curr

                curr = curr.left

            else:

                pre.right = None

  

                count += 1

                if count == k:

                    ksmall = curr.key

          

                curr = curr.right

    return ksmall

### So far we have implemented the ranking part of the BST and the return value is the kth smallest elements if you need to have +1 to it it's totally fine to get the rank instead of the index of the Elem in a sorted form which is required in the question

def insert(node, key):

    if node == None:

        return Node(key)

    if key < node.key:

        node.left = insert(node.left, key)

    elif key > node.key:

        node.right = insert(node.right, key)

    return node

The complexity here is O(h) for a very simple reason that if you want to check the worse case we'll be moving till the end of the tree that is to the leaf node to insert but the problem is regardless of the result of the comparison, we'll get the same result in complexity that is either it goes to the left side or the right we'll go a level down and that can happen in any scenario only 'h' times, which is the height of the tree.

Exactly same analogy works for search and delete as well.

def search(root,key):

    if root is None or root.val == key:

        return root

    if root.val < key:

        return search(root.right,key)

    return search(root.left,key)

def deletion(root, key):

    if root == None :

        return None

    if root.left == None and root.right == None:

        if root.key == key :

            return None

        else :

            return root

    key_node = None

    q = []

    q.append(root)

    while(len(q)):

        temp = q.pop(0)

        if temp.data == key:

            key_node = temp

        if temp.left:

            q.append(temp.left)

        if temp.right:

            q.append(temp.right)

    if key_node :

        x = temp.data

        deleteDeepest(root,temp)

        key_node.data = x

    return root

Hope it helps, if need explaination ask in comments

Add a comment
Know the answer?
Add Answer to:
(20 points) Suppose you are given a binary search tree T of n nodes (as discussed...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Suppose you are given a balanced binary search tree with 15 nodes in it, containing the even numb...

    Suppose you are given a balanced binary search tree with 15 nodes in it, containing the even numbers from 2 to 30 inclusive. (a) (5 points) Draw this tree. (b) (3 points) Explain how you would check if the number 18 is in this tree, and state the number of operations this would take. (c) (2 points) Explain how you would insert the number 27 into this tree, and state the number of operations this should take.

  • IN JAVA 2 A Binary Search Tree The goal of this lab is to gain familiarity...

    IN JAVA 2 A Binary Search Tree The goal of this lab is to gain familiarity with simple binary search trees. 1. Begin this lab by implementing a simple class that represents a "node” in a binary search tree, as follows. public class MyTreeNode<t extends Comparable<T>> { public T data; public MyTreeNode<T> leftchild; public MyTreeNode<T> rightChild; public MyTreeNode<T> parent; 2. Have the second member of your pair type in the code for the simple binary search tree interface. public interface...

  • (true/false) All nodes in the right subtree of a node must be smaller in value than...

    (true/false) All nodes in the right subtree of a node must be smaller in value than their parent (true/false) Each node in a binary search tree may contain zero, one, or two children nodes. (true/false) It is possible to recursively process a binary search tree to print all the data in a binary search tree in sorted order. (true/false) The value of a parent must be less than all its children. (true/false) All nodes in the right subtree of a...

  • Beginning with an empty binary search tree, what binary search tree is formed when you add the following letters in the order given?

    Trees-related questionsBeginning with an empty binary search tree, what binary search tree is formed when you add the following letters in the order given? J, N, B, A, W, E, TRepresent the following binary tree with an array  What is the result of adding 3 and 4 to the 2-3 tree shown below?Why does a node in a red-black tree require less memory than a node in a 2-3-4 tree?Why can’t a Red-Black Tree have a black child node with exactly...

  • Insert the following values in the given order into a Binary Search Tree and use the...

    Insert the following values in the given order into a Binary Search Tree and use the resulting BST in the next 5 questions. 15 8 3 6 23 9 11 10 20 13 5 9. What is the height of the resulting Binary Search Tree? 10. What is the depth of the node that stores the value 11? 11. Is there a path from the node storing the value 15 to the node storing the value 5? If so, show...

  • 8. Given the BST below, show the BST that would result after inserting the key of value 180 if sp...

    8. Given the BST below, show the BST that would result after inserting the key of value 180 if splaying is performed starting at the node that was inserted. 100 50 150 40 60 200 30 9. A nice property of splay trees is that each of Find, Insert and Delete takes O(logn) time. TrueFalse? 10. The keys of value N, N-1, N-2... 4, 3, 2, 1 are inserted in this order in a splay tree. What is the final...

  • 1 Binary Search Trees (25 points) Consider the binary tree as shown in Figure 1. 9...

    1 Binary Search Trees (25 points) Consider the binary tree as shown in Figure 1. 9 5 15 10 17 8 Figure 1: Binary Tree: The letter next to each node (e.g., a, b) denotes the tree node, and the number inside each node is the key. 1.1 Correctness (10 points) Is this binary tree a valid binary search tree? In other words, does it satisfy the binary search tree property? If not, which node(s) violates the binary search tree...

  • AVL Tree is a self-balancing Binary Search Tree (BST) where the difference between heights of left...

    AVL Tree is a self-balancing Binary Search Tree (BST) where the difference between heights of left and right subtrees cannot be more than one for all nodes. Perform insert and delete simulation for each given number below by using the concept of AVL Tree! a. INSERT: +300, +500, +700, +600, +650, +550, +525, +510, +580, +200, +565, +800 b. DELETE: -525, -500, -510, -650, -700 *you are obligated to use predecessor (left subtree's right-most child) for the replacement process

  • A binary search tree includes n nodes and has an height h. Check all that applies...

    A binary search tree includes n nodes and has an height h. Check all that applies about the space complexity of TREE-MINIMUMX) TREE-MINIMUM () 1 while x. left NIL 2 3 return x x x.left O it is e (lg n) ■ it is 0(h). D it is e (1) ■ It is in place ■ it is Θ (n) A binary search tree includes n nodes and has an height h. Check all that applies about the space complexity...

  • Java eclipse question. Given a binary search tree -------------M ---------G ---------N ------D----- H ---B----F How woul...

    Java eclipse question. Given a binary search tree -------------M ---------G ---------N ------D----- H ---B----F How would you find the farthest from the root and most right side of the node? So, in this case, farthest nodes are B and F with height of 4 But the most right side of the node is F Therefore answer is F. I have //Will call helper method. public T findRightmostLowest(){ int lv = height();//This is the maxium height of the tree(4 in this...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT