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 8116097
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T03:42:35+00:00 2026-06-06T03:42:35+00:00

I am quite a beginner to Python and trying to get my head around

  • 0

I am quite a beginner to Python and trying to get my head around generators and specifically using the yield statement. Playing around by writing some classic Tree class, that stores keys and data.

#!/usr/bin/env python3

class Tree:
    def __init__(self, key, data):
        "Create a new Tree object with empty L & R subtrees."
        self.key = key
        # store passed data
        self.data = data
        self.left = self.right = None

    def insert(self, key, data):
        "Insert a new element and data into the tree in the correct position."
        if key < self.key:
            if self.left:
                self.left.insert(key,data)
            else:
                self.left = Tree(key, data)
        elif key > self.key:
            if self.right:
                self.right.insert(key, data)
            else:
                self.right = Tree(key, data)
        else:
            raise ValueError("Attempt to insert duplicate value")

    def walk(self):
        "Generate the keys and data from the tree in sorted order."
        if self.left:
            for n in self.left.walk():
                yield n
        # change output to include data
        yield self.key,self.data
        if self.right:
            for n in self.right.walk():
                yield n

This works quite nicely so far. Now I am trying to implement a find() function that walks the tree and returns the data of a key found.

def find(self, key):
    if self.left:
        for n in self.left.find(key):
            yield n

    if self.right:
        for n in self.right.find(key):
            yield n

    if self.key == key:
        yield self.data

The function works – but I want to raise a KeyError if the key is nowhere to be found in the tree. I tried to wrap my head around it, but I don’t see a (simple) way to do this when using the yield statements. Specifically, I don’t seem to be able to come up with a way to actually know when the tree has been completely walked and still the key hasn’t been found.

Thanks in advance!

  • 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-06T03:42:36+00:00Added an answer on June 6, 2026 at 3:42 am

    I notice that find doesn’t use the fact that the tree is sorted. How about this implementation:

    def find(self, key):
        if key == self.key:
            return self.data
        if key < self.key and self.left:
            return self.left.find(key)
        if key > self.key and self.right:
            return self.right.find(key)
        raise KeyError("No such thing")
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Python beginner here. I've looked around and found similar questions but can't quite cobble
I can't quite get my head around this particular problem, and I'm hoping you
I am using NHibernate for my project. i am quite a beginner at working
I'm quite new to threading in Python and have a couple of beginner questions.
What I'm trying to do is quite simple but as a beginner I'm getting
I am a python beginner and I am trying to average two NumPy 2D
I've been an avid learner of the Python language for quite some time. Having
i'm quite a beginner in C# , i tried to write a program that
I am a beginner to ruby on rails.But i am quite comfortable with php,
I'm a beginner at Andengine, but I have read quite a lot of tutorials

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.