Question
attention!!!!!!! I need python method!!!!!!!!!
Week 3: Working with a BST TODO: Implement a Binary Search Tree (Class Name: RangesizeTree) Choose one language fromJava or P
Descendants to the LEFT are LESS THAN OR EQUAL TO ( ) the nodes value Descendants to the RIGHT are STRICTLY GREATER THANthe
Python The interface for the Range Search Tree is in tree_interface.py (You dont need to modify this). The file you want to
Example Executions: put 5 5 put 3 5 put 6 5 put 3 5
put 4 5 put 7 5 1 size 4 7: 4
get 3: [Node<3> (Parent-3), Node<3> (Parent-5)] 5 6 remove 3 5
the part which need to edit is below: i need python one!!!!!!!!!

import node import tree_interface class RangeSizeTree (tree interface.RSTreeInterface) Range Size Tree Implements the Range S
def get sef, k): Get the node (s) with key k E.g. 1 I 1 2 6 get(1) returns array of both 1 (parent 1),1 (parent 3) NOTE: orde
def remove(selt, k): Removes the value from the tree. Note: with duplicates, find the FIRST DEEPEST OCCURRENCE E.g. Remove
def range_size(self, a, b) Calculates the size between two keys (Inclusive!) e-g. range size(1, 1)1 e.g. #2 5 7 1 5 8 10 rang
the part below is interface for the range search tree which don’t need to modify it.

a +Node java ф range-size,tree.py · 쇼 RangesizeTreejava LANGUA.GE ф node.py 2 Simple Binary Tree Node to be used in the Binar
a+Node java ф range-size-tree.py 4. RangeSizeTree.java LANGUAGE node.py 34 def increment subtree (self): 35 36 37 38 39 40 de
+ Node.java ф range-size-tree.py RangesizeTree.java LANGUAGE ф node.py 57 58 def get_right (self): 59 60 61 62 63 64 65 66 de
80 81 def set_right (self, r): 82 83 84 85 86 87 88 def set parent (self, p): 89 90 91 92 93 Set the right child of the node.
Week 3: Working with a BST TODO: Implement a Binary Search Tree (Class Name: RangesizeTree) Choose one language fromJava or Python. Iin both languages,there is an empty main function in the Range Size Tree file. The main function is not tested, however, it is provided for you to run your own testing. NOTE: Please uncomment the language you are using in the LANGUAGE file. The Range Size Tree supports this functionality put (k) -put the key K into the tree (duplicate keys are allowed). remove (k) remove the lowest occurrence of the key K get(k) - return the nodes that have the key K size (a, b count the number of nodes between a and b. o NOTE: a and b may not be in order; may not be a key etc o If a lowest tree node, you should still count from this. o Similarly if b > highest tree node, still count between this. Important notes about implementation!!! Descendants to the LEFT are LESS THAN OR EQUAL TO ( 7 param a: A key to search between :param b: A key to search between. return: Number of nodes between the two keys pass
a +Node java ф range-size,tree.py · 쇼 RangesizeTreejava LANGUA.GE ф node.py 2 Simple Binary Tree Node to be used in the Binary Tree. 6 class Node 8 Simple node in the binary tree 10 11 def init.. (self, key) 12 13 14 15 16 17 18 def get,key (self) 19 20 21 self..key key self. subtree size 1 self"-left = None self..right None self._parent None Returns the value of the node's key return: the node key return self._key 23 24 25
a+Node java ф range-size-tree.py 4. RangeSizeTree.java LANGUAGE node.py 34 def increment subtree (self): 35 36 37 38 39 40 def decrement subtree (self): 41 42 43 Increments the size of the subtree self.-subtree-size ++ Decrement the size of the subtree if self._subtree size1: 45 46 47 48 49 50 def getleft (self) 51 52 53 54 return self. subtree size1 Get the left child :return: Left child node return self._left 56
+ Node.java ф range-size-tree.py RangesizeTree.java LANGUAGE ф node.py 57 58 def get_right (self): 59 60 61 62 63 64 65 66 def get parent (self): 67 68 69 70 71 72 73 Get the right child return: Right child node return self..right Get the parent of the node return: The parent return self._parent 74 def set left(self, ): 75 76 Set the left child of the node. self..left 1 78 79 86
80 81 def set_right (self, r): 82 83 84 85 86 87 88 def set parent (self, p): 89 90 91 92 93 Set the right child of the node. self..right r Set the parent. self._parent p
0 0
Add a comment Improve this question Transcribed image text
Answer #1

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)

Add a comment
Know the answer?
Add Answer to:
Attention!!!!!!! I need python method!!!!!!!!! the part which need to edit is below: i nee...
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
  • I need to do a tree sort method but the treesortMethod is not working /****Binarytree class****\...

    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&#39...

    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...

    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...

    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...

    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...

    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...

    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...

    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...

    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...

    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...

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