简体   繁体   中英

Binary Search Tree operations

I am trying to implement Binary Search Tree operations in python. As of now, I have written some code to add nodes to this search tree (sorted). Here's what I've in my code:

class TreeNode:

    def __init__(self, data):
        self.data = data
        self.lLink = None
        self.rLink = None

class BinaryTree:

    def __init__(self):
        self.root = None

    def AddNode(self, data):
        if self.root is None:
            self.root = TreeNode(data)
        else:
            if data < self.root.data:
                if self.root.lLink is None:
                    self.root.lLink = TreeNode(data)
                else:
                    AddNode(self.root.lLink, data)
            else:
                if self.root.rLink is None:
                    self.root.rLink = TreeNode(data)
                else:
                    AddNode(self.root.rLink, data)

    def InOrder(self, head):
        if self.root.lLink is not None:
            InOrder(self.root.lLink)
        print self.root.data,
        if self.root.rLink is not None:
            InOrder(self.root.rLink)

    myTree = BinaryTree()
    myTree.AddNode(15)
    myTree.AddNode(18)
    myTree.AddNode(14)

How do I test if my AddNode() method is correct? I know the algorithm but just to be sure. What I was thinking of is to create an InOrder() method and try to print elements through this InOrder traversal. As a result, my data added to the tree should be displayed in sorted order. If it is displayed in sorted order, I'll be sure that both my AddNode() and InOrder() methods are correct.

Your BinaryTree class is faulty, changing the order of insertions to

myTree.AddNode(14)
myTree.AddNode(18)
myTree.AddNode(15)

raises an error - NameError: global name 'AddNode' is not defined .

This is because in the lines, AddNode(self.root.rLink, data) and AddNode(self.root.lLink, data) you seem to be calling the AddNode function on instances of TreeNode which is not possible. I fixed up some of the errors in your code and it should work great now.

class TreeNode:
    def __init__(self, data):
        self.data = data
        self.lLink = None
        self.rLink = None

class BinaryTree:
    def __init__(self):
        self.root = None

    def AddNode(self, data):
        if self.root is None:
            self.root = TreeNode(data)
        else:
            self.AddHelper(data, self.root)

    def AddHelper(self, data, startingPoint):
        if data < startingPoint.data:
            if startingPoint.lLink is None:
                startingPoint.lLink = TreeNode(data)
            else:
                self.AddHelper(data, startingPoint.lLink)
        else:
            if startingPoint.rLink is None:
                startingPoint.rLink = TreeNode(data)
            else:
                self.AddHelper(data, startingPoint.rLink)

    def InOrder(self):
        self.InOrderHelper(self.root)

    def InOrderHelper(self, startingPoint):
        if startingPoint is None:
            return
        self.InOrderHelper(startingPoint.lLink)
        print startingPoint.data,
        self.InOrderHelper(startingPoint.rLink)

Output Test :

>>> myTree = BinaryTree()
>>> myTree.AddNode(14)
>>> myTree.AddNode(18)
>>> myTree.AddNode(15)
>>> myTree.InOrder()
14 15 18

Inserting can be a little tricky, especially because the function is a part of the tree itself. So, you call the insert function on the tree, but specifying a starting point. This defaults to root, so you can leave the argument when you call the function.

Also, I think you are a little unclear about how self works in a function. You cannot pass it as an argument to the function, which is what it seems you have done.

class TreeNode:
    def __init__(self, data):
        self.data = data
        self.rLink = None
        self.lLink = None

class BinaryTree:
    def __init__(self):
        self.root = None

    def AddNode(self, data, node=None):
        if not node :
            node = self.root
        if self.root is None:
            self.root = TreeNode(data)
        else:
            if data < node.data:
                if node.lLink is None:
                    node.lLink = TreeNode(data)
                else:
                    self.AddNode(data, self.root.lLink)
            else:
                if node.rLink is None:
                    node.rLink = TreeNode(data)
                else:
                    self.AddNode(data, self.root.rLink)

    def InOrder(self, head):
        if head.lLink is not None:
            self.InOrder(head.lLink)
        print head.data,
        if head.rLink is not None:
            self.InOrder(head.rLink)

myTree = BinaryTree()
myTree.AddNode(14)
myTree.AddNode(15)
myTree.AddNode(18)
myTree.InOrder(myTree.root)

Testing the insert function with an in-order traversal is the best approach.

This should work. You are not going down the tree if you use self.root.lLink every time. Optionally, you could write one more line of code to check if the output is indeed in ascending order.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM