How to traverse a binary search tree in alphabetical order python? [closed]

和自甴很熟 提交于 2020-05-24 06:08:21

问题


I need your help or if you can give me advice. I'm really struggling and some help would be perfect, so this is what I got so far;

import BST, TreeNode

class Bibliography:

def __init__(self):
    self.bibtree = BST()

def getReference(self,key):
    """Return the reference for the key, if it exists, otherwise None."""
    theValue = self.bibtree.retrieveKey(key,self.bibtree.root)
    if theValue == None:
        return None
    else:
        return theValue.payload

def addReference(self, key, value):
    """Add the reference represented by key and value.

    Assume the key does not exist in the bibliography.
    """
    self.bibtree.insertNode(key, value)

def removeReference(self, key):
    """Remove the reference with this key.

    Assume the key exists in the bibliography.
    """
    self.bibtree.deleteNode(key)

def outputBibliography(self):
    """Return a string with all references in alphabetical order.

    There must be an empty line after each reference
    """
    return self.traverse(self.bibtree.root)

def traverse(self, aNode):
    """Return a string with the references in the subtree rooted at aNode.

    The references should be ordered alphabetically,
    with an empty line after each reference
    and a space between each key and its value. See the test file.
    """
    if aNode:
      self.traverse(aNode.leftChild)
        return str(aNode.key, aNode.payload, end='\n\n')
      self.traverse(aNode.right)

When I do the test the below function is not working and need help.It returns it as a list in this bracket [ ] and I do not want this. I also want a blank line and this does not happen either. I'm not to sure what I'm doing wrong, if you can give me some advise this will be helpful.

def traverse(self, aNode):
    """Return a string with the references in the subtree rooted at aNode.

    The references should be ordered alphabetically,
    with an empty line after each reference
    and a space between each key and its value. See the test file.
    """
        res = []
        if aNode:
          res = self.traverse(aNode.leftChild)
          res.append(aNode.key + ' ' + aNode.payload + '\n\n')
          res = res + self.traverse(aNode.rightChild)
        return res

The output using this code is:

['Adams, A (1991) Loves football\n\n', 'Marlow, C (1996) Loves cricket\n\n', 'Smith, I (1994) Does not play sports\n\n']

And I want this output:

Adams, A (1991) Loves football

Marlow, C (1996) Loves cricket

Smith, I (1994) Does not play sports

回答1:


And you are concatenating lists anyways, as in res + self.traverse(aNode.rightChild). Ok, never mind my previous comments about this, you get O^2 there even with lists, because you are copying them all over. Just do this

def traverse(self, aNode):
    res = ""
    if aNode:
        res = self.traverse(aNode.leftChild)
        res += aNode.key + ' ' + aNode.payload + '\n\n'
        res += self.traverse(aNode.rightChild)
    return res

This ends up giving you an empty line after the last reference, so it is more literal implementation of what the assignment says: "... with an empty line after each reference ...". That join() would only insert the newlines between references, and not after the last one.




回答2:


You are almost there. Your traverse method produces a list of the desired lines. The only thing that is remaining is to turn that list into a single string where the lines are separated by '\n\n', that is, one '\n' to terminate the current line and another '\n' to give a blank line.

tmp = tree.traverse(tree.root)  # tmp now contains the list ['Adams, A (1991) Loves football', 'Marlow, C (1996) Loves cricket', ...
print('\n\n'.join(tmp))

This now prints the output in the form you want it.



来源:https://stackoverflow.com/questions/61573442/how-to-traverse-a-binary-search-tree-in-alphabetical-order-python

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!