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
(20 points) Suppose you are given a binary search tree T of n nodes (as discussed...
C++ ONLY Threaded Binary Search Tree Since a binary search tree with N nodes has N + 1 NULL pointers, half the space allocated in a binary search tree for pointer information is wasted. Suppose that if a node has a NULL left child, we make its left child pointer link to its inorder predecessor, and if a node has a NULL right child, we make its right child pointer link to its inorder successor. This is known as a...
B-Tree is a self-balancing search tree. The B-tree is a generalization of a binary search tree in that a node can have more than two children. For example, in a 2-3 B-tree, each internal node may have 2 or 3 child nodes. Perform insert and delete simulation for each given number below by using the concept of 2-3 B-Tree! a. INSERT: +800, +565, +200, +580, +510, +525, +550, +650, +600, +700, +500, +300, +750, +590, +530 b. DELETE: -565, -510,...
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 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 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...
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...
You are given the root of a Binary Search Tree. Print the leaf elements of the tree starting from right to left. We have defined the following node C++ Node class for you: class Node { public: int name; Node* left = NULL; Node* right = NULL; }; Function to code: void printLeaves(Node* root); The first input in test cases are nodes of a tree which are inserted in that order. You don't need to implement insert. You have access...
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 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 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...