Python论坛  - 讨论区

标题:[python-chinese] 关于binary tree

2006年07月06日 星期四 22:34

linda.s samrobertsmith at gmail.com
Thu Jul 6 22:34:34 HKT 2006

我找到如下关于binary tree的代码,运行正常,但是小妹我怎么也不能理解binary tree在数据结构里的好处,特别对python.
# A binary ordered tree example

class CNode:
    left , right, data = None, None, 0

    def __init__(self, data):
        # initializes the data members
        self.left = None
        self.right = None
        self.data = data

class CBOrdTree:
    def __init__(self):
        # initializes the root member
        self.root = None

    def addNode(self, data):
        # creates a new node and returns it
        return CNode(data)

    def insert(self, root, data):
        # inserts a new data
        if root == None:
            # it there isn't any data
            # adds it and returns
            return self.addNode(data)
        else:
            # enters into the tree
            if data <= root.data:
                # if the data is less than the stored one
                # goes into the left-sub-tree
                root.left = self.insert(root.left, data)
            else:
                # processes the right-sub-tree
                root.right = self.insert(root.right, data)
            return root

    def lookup(self, root, target):
        # looks for a value into the tree
        if root == None:
            return 0
        else:
            # if it has found it...
            if target == root.data:
                return 1
            else:
                if target < root.data:
                    # left side
                    return self.lookup(root.left, target)
                else:
                    # right side
                    return self.lookup(root.right, target)

    def minValue(self, root):
        # goes down into the left
        # arm and returns the last value
        while(root.left != None):
            root = root.left
        return root.data

    def maxDepth(self, root):
        if root == None:
            return 0
        else:
            # computes the two depths
            ldepth = self.maxDepth(root.left)
            rdepth = self.maxDepth(root.right)
            # returns the appropriate depth
            return max(ldepth, rdepth) + 1

    def size(self, root):
        if root == None:
            return 0
        else:
            return self.size(root.left) + 1 + self.size(root.right)

    def printTree(self, root):
        # prints the tree path
        if root == None:
            pass
        else:
            self.printTree(root.left)
            print root.data,
            self.printTree(root.right)

    def printRevTree(self, root):
        # prints the tree path in reverse
        # order
        if root == None:
            pass
        else:
            self.printRevTree(root.right)
            print root.data,
            self.printRevTree(root.left)

if __name__ == "__main__":
    # create the binary tree
    BTree = CBOrdTree()
    # add the root node
    root = BTree.addNode(0)
    # ask the user to insert values
    for i in range(0, 5):
        data = int(raw_input("insert the node value nr %d: " % i))
        # insert values
        BTree.insert(root, data)
    print

    BTree.printTree(root)
    print
    BTree.printRevTree(root)
    print
    data = int(raw_input("insert a value to find: "))
    if BTree.lookup(root, data):
        print "found"
    else:
        print "not found"

    print BTree.minValue(root)
    print BTree.maxDepth(root)
    print BTree.size(root)

[导入自Mailman归档:http://www.zeuux.org/pipermail/zeuux-python]

2006年07月07日 星期五 09:43

limodou limodou at gmail.com
Fri Jul 7 09:43:12 HKT 2006

On 7/6/06, linda. s <samrobertsmith at gmail.com> wrote:
> 我找到如下关于binary tree的代码,运行正常,但是小妹我怎么也不能理解binary tree在数据结构里的好处,特别对python.
> # A binary ordered tree example
>
> class CNode:
>     left , right, data = None, None, 0
>
>     def __init__(self, data):
>         # initializes the data members
>         self.left = None
>         self.right = None
>         self.data = data
>
> class CBOrdTree:
>     def __init__(self):
>         # initializes the root member
>         self.root = None
>
>     def addNode(self, data):
>         # creates a new node and returns it
>         return CNode(data)
>
>     def insert(self, root, data):
>         # inserts a new data
>         if root == None:
>             # it there isn't any data
>             # adds it and returns
>             return self.addNode(data)
>         else:
>             # enters into the tree
>             if data <= root.data:
>                 # if the data is less than the stored one
>                 # goes into the left-sub-tree
>                 root.left = self.insert(root.left, data)
>             else:
>                 # processes the right-sub-tree
>                 root.right = self.insert(root.right, data)
>             return root
>
>     def lookup(self, root, target):
>         # looks for a value into the tree
>         if root == None:
>             return 0
>         else:
>             # if it has found it...
>             if target == root.data:
>                 return 1
>             else:
>                 if target < root.data:
>                     # left side
>                     return self.lookup(root.left, target)
>                 else:
>                     # right side
>                     return self.lookup(root.right, target)
>
>     def minValue(self, root):
>         # goes down into the left
>         # arm and returns the last value
>         while(root.left != None):
>             root = root.left
>         return root.data
>
>     def maxDepth(self, root):
>         if root == None:
>             return 0
>         else:
>             # computes the two depths
>             ldepth = self.maxDepth(root.left)
>             rdepth = self.maxDepth(root.right)
>             # returns the appropriate depth
>             return max(ldepth, rdepth) + 1
>
>     def size(self, root):
>         if root == None:
>             return 0
>         else:
>             return self.size(root.left) + 1 + self.size(root.right)
>
>     def printTree(self, root):
>         # prints the tree path
>         if root == None:
>             pass
>         else:
>             self.printTree(root.left)
>             print root.data,
>             self.printTree(root.right)
>
>     def printRevTree(self, root):
>         # prints the tree path in reverse
>         # order
>         if root == None:
>             pass
>         else:
>             self.printRevTree(root.right)
>             print root.data,
>             self.printRevTree(root.left)
>
> if __name__ == "__main__":
>     # create the binary tree
>     BTree = CBOrdTree()
>     # add the root node
>     root = BTree.addNode(0)
>     # ask the user to insert values
>     for i in range(0, 5):
>         data = int(raw_input("insert the node value nr %d: " % i))
>         # insert values
>         BTree.insert(root, data)
>     print
>
>     BTree.printTree(root)
>     print
>     BTree.printRevTree(root)
>     print
>     data = int(raw_input("insert a value to find: "))
>     if BTree.lookup(root, data):
>         print "found"
>     else:
>         print "not found"
>
>     print BTree.minValue(root)
>     print BTree.maxDepth(root)
>     print BTree.size(root)
>
查询速度快。


-- 
I like python!
My Blog: http://www.donews.net/limodou
My Django Site: http://www.djangocn.org
NewEdit Maillist: http://groups.google.com/group/NewEdit

[导入自Mailman归档:http://www.zeuux.org/pipermail/zeuux-python]

2006年07月08日 星期六 01:05

WhiteFox wyh817 at gmail.com
Sat Jul 8 01:05:35 HKT 2006

有些地方用2叉树很方便,你要遇到那样的项目才会体会到

[导入自Mailman归档:http://www.zeuux.org/pipermail/zeuux-python]

2006年07月08日 星期六 19:58

swordsp sparas2006 at gmail.com
Sat Jul 8 19:58:54 HKT 2006

数据结构和算法是抽象于语言之上的,与你使用什么语言没有关系,只与你面对的问题有关系。
不同的数据结构实际上就是不同的数学模型,有些模型只是问题本身的直观抽象,还有些则是为了提高效率而特别设计的等价模型,二叉排序树偏向后者。
你应该看一下数据结构的教材。

On 7/6/06, linda. s <samrobertsmith at gmail.com> wrote:
>
> 我找到如下关于binary tree的代码,运行正常,但是小妹我怎么也不能理解binary tree在数据结构里的好处,特别对python.
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.exoweb.net/pipermail/python-chinese/attachments/20060708/ed6cfb5b/attachment.html

[导入自Mailman归档:http://www.zeuux.org/pipermail/zeuux-python]

如下红色区域有误,请重新填写。

    你的回复:

    请 登录 后回复。还没有在Zeuux哲思注册吗?现在 注册 !

    Zeuux © 2025

    京ICP备05028076号