class Node:
def __init__(self):
self.left = None
self.right = None
self.val = 0
# A utility function to insert a new node with the given key
def put(self,key):
if self is None:
self = key
else:
if self.val < key.val:
if self.right is None:
self.right = key
else:
insert(self.right, key)
else:
if self.left is None:
self.left = key
else:
insert(self.left, key)
# A utility function to search a given key in BST
def get(self,key):
# Base Cases: self is null or key is present at self
if self is None or self.val == key:
return self
# Key is greater than self's key
if self.val < key:
return search(self.right,key)
# Key is smaller than self's key
return search(self.left,key)
# A utility function to remove a given key in BST
def remove(self, key):
# Base Case
if self is None:
return self
# If the key to be deleted is smaller than the self's
# key then it lies in left subtree
if key < self.key:
self.left = deleteKey(self.left, key)
# If the kye to be delete is greater than the self's key
# then it lies in right subtree
elif(key > self.key):
self.right = deleteKey(self.right, key)
# If key is same as self's key, then this is the key
# to be deleted
else:
# Key with only one child or no child
if self.left is None :
temp = self.right
self = None
return temp
elif self.right is None :
temp = self.left
self = None
return temp
# Key with two children: Get the inorder successor
# (smallest in the right subtree)
temp = minValueKey(self.right)
# Copy the inorder successor's content to this key
self.key = temp.key
# Delete the inorder successor
self.right = deleteKey(self.right , temp.key)
return self
# The function prints all the keys in the given range
def range_size(self, a, b):
# Base Case
if self is None:
return
# Since the desired o/p is sorted, recurse for left
# subtree first. If self.data is greater than a, then
# only we can get o/p keys in left subtree
if a < self.data :
Print(self.left, a, b)
# If self's data lies in range, then prints self's data
if a <= self.data and b >= self.data:
print self.data,
# If self.data is smaller than b, then only we can get
# o/p keys in right subtree
if b > self.data:
Print(self.right, a, b)
Attention!!!!!!! I need python method!!!!!!!!! the part which need to edit is below: i nee...
I need to do a tree sort method but the treesortMethod is not working /****Binarytree class****\ package Tree; public class BinaryTree { private TreeNode root; // head of the list //constructor - create an empty binary tree public BinaryTree() { root = null; } //isEmpty() - return true if tree is empty, false otherwise public boolean isEmpty() { return (root == null); } //deleteTree() - remove all items from tree public void deleteList() { root =...
I need help to write this code in C++, I am struggling to find max node's parent's right child and max node's left child. Node* maxValueNode(Node* node) { Node* current = node; while (current && current->right != NULL) current = current->right; return current; } void deleteNode(BST* tree, Node* node, Node* parent) { //TODO - follow the lecture pseudocode to write the deleteNode function // - returns true if the value was deleted, false if not // - don't forget to...
Add printRang method to BST.java that, given a low key value, and high key value, print all records in a sorted order whose values fall between the two given keys. (Both low key and high key do not have to be a key on the list). BST.java import java.lang.Comparable; /** Binary Search Tree implementation for Dictionary ADT */ class BST<Key extends Comparable<? super Key>, E> implements Dictionary<Key, E> { private BSTNode<Key,E> root; // Root of the BST int nodecount; //...
Using the following implementation of Tree class Node { public int iData; // data item (key) public double dData; // data item public Node leftChild; // this node's left child public Node rightChild; // this node's right child public void displayNode() // display ourself { System.out.print('{'); System.out.print(iData); System.out.print(", "); System.out.print(dData); System.out.print("} "); } } // end class Node //------------------------------------------------------------------ import java.io.IOException; import java.util.Stack; public class Tree { private Node root; // first node of tree // ------------------------------------------------------------- public Tree() // constructor { root = null; }...
Removing Nodes from a Binary Tree in Java This section requires you to complete the following method within BinaryTree.java: public void remove(K key) { } The remove method should remove the key from the binary tree and modify the tree accordingly to maintain the Binary Search Tree Principle. If the key does not exist in the binary tree, no nodes should be removed. In case there is a non-null left child, it should take the place of the removed node....
Add a non-recursive inorder() method to class LinkedBinaryTree<E> to traverse binary tree. Test inorder() method. Implementation in Java #########LinkedBinary Tree class######### public class LinkedBinaryTree<E> extends AbstractBinaryTree<E> { //---------------- nested Node class ---------------- /** Nested static class for a binary tree node. */ protected static class Node<E> implements Position<E> { private E element; // an element stored at this node private Node<E> parent; // a reference to the parent node (if any) private Node<E> left; // a reference to the left...
PYTHON QUESTION... Building a Binary Tree with extended Binary Search Tree and AVL tree. Create a class called MyTree with the methods __init__(x), getLeft(), getRight(), getData(), insert(x) and getHeight(). Each child should itself be a MyTree object. The height of a leaf node should be zero. The insert(x) method should return the node that occupies the original node's position in the tree. Create a class called MyBST that extends MyTree. Override the method insert(x) to meet the definitions of a...
The code is in JAVA public class CheckBST { //method to implement public static boolean isValidBST(TreeNode root) { } public static void main(String[] args) { TreeNode a = new TreeNode(1); TreeNode b = new TreeNode(2); TreeNode c = new TreeNode(3); a.left = b; a.right = c; System.out.println(isValidBST(a)); TreeNode d = new TreeNode(2); TreeNode e = new TreeNode(1); TreeNode f = new TreeNode(3); d.left = e; d.right = f; System.out.println(isValidBST(d)); } } TreeNode.java class TreeNode { int val; TreeNode left; TreeNode...
Scenario We need to write a method that, given a key as an argument, returns the next in order key found in the binary search tree. If the key given as an argument is not found, the method should still return the next in order key. If the binary tree is empty or all the stored keys are smaller than the argument, then the return value should be empty. For example, using a collection of {10,13,52,67,68,83} stored in the binary...
3. [5 marks] Suppose T is a binary tree that stores an integer key value in each node. Assume the following notation/operations on a binary tree. » the key T.key is the root node's integer key value . the left child T.left is T's left subtree, which is possibly an empty tree (or null) the right child T.right is T's right subtree, which is possibly an empty tree (or null) (a) Write an efficient algorithm FINDMAxPrODuCT(T) in pseudocode that returns...