Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8880349
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T20:08:58+00:00 2026-06-14T20:08:58+00:00

I am working with a binary search tree in Python 3.2.3 and here are

  • 0

I am working with a binary search tree in Python 3.2.3 and here are my two files (given from teacher) for creating the binary search tree.

But first I will explain the problem I am having. First I am reading in the file and attaching the new words to the BST with the movie titles as their values. However I am having problems on what to do with my else statement. I believe I need to attach that word that is already in the Binary search tree, Lets say “A” because it appears alot and attach it to another value of a movie title so that one key is attached to multiple values.

I don’t know how to go about it and it is where the else statement is in my Read function. Any help would be greatly appreciated.

Thank you

Tree Node:

class TreeNode:
   def __init__(self,key,val,left=None,right=None,
                                       parent=None):
      self.key = key
      self.payload = val
      self.leftChild = left
      self.rightChild = right
      self.parent = parent

   def hasLeftChild(self):
      return self.leftChild

   def hasRightChild(self):
      return self.rightChild

   def isLeftChild(self):
      return self.parent and \
             self.parent.leftChild == self

   def isRightChild(self):
      return self.parent and \
             self.parent.rightChild == self

   def isRoot(self):
      return not self.parent

   def isLeaf(self):
      return not (self.rightChild or self.leftChild)

   def hasAnyChildren(self):
      return self.rightChild or self.leftChild

   def hasBothChildren(self):
      return self.rightChild and self.leftChild

   def replaceNodeData(self,key,value,lc,rc):
      self.key = key
      self.payload = value
      self.leftChild = lc
      self.rightChild = rc
      if self.hasLeftChild():
          self.leftChild.parent = self
      if self.hasRightChild():
          self.rightChild.parent = self

   def __iter__(self):

      if self:
         if self.hasLeftChild():
              for elem in self.leftChild:
                 yield elem
         yield self.key
         if self.hasRightChild():
              for elem in self.rightChild:
                 yield elem

   def findSuccessor(self):
      succ = None
      if self.hasRightChild():
          succ = self.rightChild.findMin()
      else:
          if self.parent:
              if self.isLeftChild():
                  succ = self.parent
              else:
                  self.parent.rightChild = None
                  succ = self.parent.findSuccessor()
                  self.parent.rightChild = self
      return succ

   def findMin(self):
      current = self
      while current.hasLeftChild():
          current = current.leftChild
      return current

   def spliceOut(self):
      if self.isLeaf():
         if self.isLeftChild():
            self.parent.leftChild = None
         else:
            self.parent.rightChild = None
      elif self.hasAnyChildren():
         if self.hasLeftChild():
            if self.isLeftChild():
               self.parent.leftChild = self.leftChild
            else:
               self.parent.rightChild = self.leftChild
            self.leftChild.parent = self.parent
         else:
            if self.isLeftChild():
               self.parent.leftChild = self.rightChild
            else:
               self.parent.rightChild = self.rightChild
            self.rightChild.parent = self.parent

Binary Search Tree:

from tree_node import TreeNode

class BinarySearchTree:

    def __init__(self):
        self.root = None
        self.size = 0

    def length(self):
        return self.size

    def __len__(self):
        return self.size

    def __iter__(self):
        return self.root.__iter__()

    def __str__(self):
        """Returns a string representation of the tree
           rotated 90 degrees counter-clockwise"""

        def strHelper(root, level):
            resultStr = ""
            if root:
                resultStr += strHelper(root.rightChild, level+1)
                resultStr += "| " * level
                resultStr += str(root.key) + "\n"
                resultStr += strHelper(root.leftChild, level+1)                
            return resultStr


        return strHelper(self.root, 0)


    def __contains__(self,key):
        if self._get(key,self.root):
            return True
        else:
            return False

    def get(self,key):
        if self.root:
            res = self._get(key,self.root)
            if res:
                return res.payload
            else:
                return None
        else:
            return None

    def _get(self,key,currentNode):
        if not currentNode:
            return None
        elif currentNode.key == key:
            return currentNode
        elif key < currentNode.key:
            return self._get(key,currentNode.leftChild)
        else:
            return self._get(key,currentNode.rightChild)

    def __getitem__(self,key):
        return self.get(key) 

    def __setitem__(self,k,v):
        self.put(k,v)

    def put(self,key,val):
        if self.root:
            self._put(key,val,self.root)
        else:
            self.root = TreeNode(key,val)
        self.size = self.size + 1

    def _put(self,key,val,currentNode):
        if key < currentNode.key:
            if currentNode.hasLeftChild():
                self._put(key,val,currentNode.leftChild)
            else:
                currentNode.leftChild = TreeNode(key,val,
                                          parent=currentNode)
        else:
            if currentNode.hasRightChild():
                self._put(key,val,currentNode.rightChild)
            else:
                currentNode.rightChild = TreeNode(key,val,
                                          parent=currentNode)

    def delete(self,key):
      if self.size > 1:
          nodeToRemove = self._get(key,self.root)
          if nodeToRemove:
              self.remove(nodeToRemove)
              self.size = self.size-1
          else:
              raise KeyError('Error, key not in tree')
      elif self.size == 1 and self.root.key == key:
          self.root = None
          self.size = self.size - 1
      else:
          raise KeyError('Error, key not in tree')

    def __delitem__(self,key):
        self.delete(key)


    def remove(self,currentNode):
      if currentNode.isLeaf(): #leaf
        if currentNode == currentNode.parent.leftChild:
            currentNode.parent.leftChild = None
        else:
            currentNode.parent.rightChild = None
      elif currentNode.hasBothChildren(): #interior
        succ = currentNode.findSuccessor()
        succ.spliceOut()
        currentNode.key = succ.key
        currentNode.payload = succ.payload

      else: # this node has one child
        if currentNode.hasLeftChild():
          if currentNode.isLeftChild():
              currentNode.leftChild.parent = currentNode.parent
              currentNode.parent.leftChild = currentNode.leftChild
          elif currentNode.isRightChild():
              currentNode.leftChild.parent = currentNode.parent
              currentNode.parent.rightChild = currentNode.leftChild
          else:
              currentNode.replaceNodeData(currentNode.leftChild.key,
                                 currentNode.leftChild.payload,
                                 currentNode.leftChild.leftChild,
                                 currentNode.leftChild.rightChild)

        else:
          if currentNode.isLeftChild():
              currentNode.rightChild.parent = currentNode.parent
              currentNode.parent.leftChild = currentNode.rightChild
          elif currentNode.isRightChild():
              currentNode.rightChild.parent = currentNode.parent
              currentNode.parent.rightChild = currentNode.rightChild
          else:
              currentNode.replaceNodeData(currentNode.rightChild.key,
                                 currentNode.rightChild.payload,
                                 currentNode.rightChild.leftChild,
                                 currentNode.rightChild.rightChild)




def main():
    t = BinarySearchTree()
    t.put(5,5)
    t.put(3,3)
    t.put(8,8)
    t.put(10, 10)
    t.put(7,7)
    print(t)

    return t

if __name__ == "__main__": t = main()

My Code that I need help with:

from binary_search_tree import BinarySearchTree
from tree_node import TreeNode

MovieTree = BinarySearchTree()
MovieList = []

def Read(filename):
    file = open('movieData.txt')
    for line in file:
        line = line.strip()
        words = line.lower().split(' ')
        for word in words:
            if word not in MovieTree:
                MovieTree.put(word, [line])
            else:
                pass
                #attach word that is already in BST to another value
                # which is this case is a movie title.

def Main():
    Read('movieData.txt')
    #print(MovieTree)
    #print (MovieList)

Main()

And if needed here is a small sample of what I am reading in:

A Bad Day <2006>
A Baleia Branca - Uma Ideia de Deus <2007>
A Batalha das Flores no Campo Grande <1907>
A Bear Named Winnie <2004>
A Beautiful Daze <2008>
A Beautiful Mind <2001>
A Bell for Adano <1945>
A Better Place <1997>
A Big Hand for Sooty <1998>
A Big Hand for the Little Lady <1966>
A Big Mistake! <2009>
A Bill of Divorcement <1932> <1940>
A Bird in a Bonnet <1958>
A Bird in a Guilty Cage <1952>
A Bird in the Head <1946>
A Bit of Scarlet <1997>
A Black Widow <2009>
A Blind Bargain <1922>
A Blue Collapse <2008>
A Blue Note <2009>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T20:08:59+00:00Added an answer on June 14, 2026 at 8:08 pm

    If I understand the requirement correctly, I think you want MovieTree.get(word).append(line)

    The get(word) part returns a list of titles associated with that word (i.e. the payload from the matching word in the binary search tree), and .append(line) adds line onto the end of the list.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am working on a Binary search tree. So,here is the structure used for
I'm working with deleting nodes from a binary search tree and I keep getting
I'm trying to extract the minimum from a binary heap but it's not working.
I have been working on a simple binary search tree for a larger project.
Functions in scheme/racket. Working on a few functions using a binary search tree. I
I'm working on a function to find the height of a binary search tree.
I am trying to write a code, which, given a Binary Search Tree root
I'm working on creating a binary search algorithm in C that searches for a
So recently I have been working on phonebook project that uses Binary Search Tree.
I'm working on a project where I have to make a binary search tree

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.