|
| 1 | +#! /usr/bin/env python |
| 2 | +#coding:utf-8 |
| 3 | + |
| 4 | +# The code is from:http://code.activestate.com/recipes/286239-binary-ordered-tree/ |
| 5 | +# A binary ordered tree example |
| 6 | + |
| 7 | +class CNode: |
| 8 | + left , right, data = None, None, 0 |
| 9 | + |
| 10 | + def __init__(self, data): |
| 11 | + # initializes the data members |
| 12 | + self.left = None |
| 13 | + self.right = None |
| 14 | + self.data = data |
| 15 | + |
| 16 | + |
| 17 | +class CBOrdTree: |
| 18 | + def __init__(self): |
| 19 | + # initializes the root member |
| 20 | + self.root = None |
| 21 | + |
| 22 | + def addNode(self, data): |
| 23 | + # creates a new node and returns it |
| 24 | + return CNode(data) |
| 25 | + |
| 26 | + def insert(self, root, data): |
| 27 | + # inserts a new data |
| 28 | + if root == None: |
| 29 | + # it there isn't any data |
| 30 | + # adds it and returns |
| 31 | + return self.addNode(data) |
| 32 | + else: |
| 33 | + # enters into the tree |
| 34 | + if data <= root.data: |
| 35 | + # if the data is less than the stored one |
| 36 | + # goes into the left-sub-tree |
| 37 | + root.left = self.insert(root.left, data) |
| 38 | + else: |
| 39 | + # processes the right-sub-tree |
| 40 | + root.right = self.insert(root.right, data) |
| 41 | + return root |
| 42 | + |
| 43 | + def lookup(self, root, target): |
| 44 | + # looks for a value into the tree |
| 45 | + if root == None: |
| 46 | + return 0 |
| 47 | + else: |
| 48 | + # if it has found it... |
| 49 | + if target == root.data: |
| 50 | + return 1 |
| 51 | + else: |
| 52 | + if target < root.data: |
| 53 | + # left side |
| 54 | + return self.lookup(root.left, target) |
| 55 | + else: |
| 56 | + # right side |
| 57 | + return self.lookup(root.right, target) |
| 58 | + |
| 59 | + def minValue(self, root): |
| 60 | + # goes down into the left |
| 61 | + # arm and returns the last value |
| 62 | + while(root.left != None): |
| 63 | + root = root.left |
| 64 | + return root.data |
| 65 | + |
| 66 | + def maxDepth(self, root): |
| 67 | + if root == None: |
| 68 | + return 0 |
| 69 | + else: |
| 70 | + # computes the two depths |
| 71 | + ldepth = self.maxDepth(root.left) |
| 72 | + rdepth = self.maxDepth(root.right) |
| 73 | + # returns the appropriate depth |
| 74 | + return max(ldepth, rdepth) + 1 |
| 75 | + |
| 76 | + def size(self, root): |
| 77 | + if root == None: |
| 78 | + return 0 |
| 79 | + else: |
| 80 | + return self.size(root.left) + 1 + self.size(root.right) |
| 81 | + |
| 82 | + def printTree(self, root): |
| 83 | + # prints the tree path |
| 84 | + if root == None: |
| 85 | + pass |
| 86 | + else: |
| 87 | + self.printTree(root.left) |
| 88 | + print root.data, |
| 89 | + self.printTree(root.right) |
| 90 | + |
| 91 | + def printRevTree(self, root): |
| 92 | + # prints the tree path in reverse |
| 93 | + # order |
| 94 | + if root == None: |
| 95 | + pass |
| 96 | + else: |
| 97 | + self.printRevTree(root.right) |
| 98 | + print root.data, |
| 99 | + self.printRevTree(root.left) |
| 100 | + |
| 101 | +if __name__ == "__main__": |
| 102 | + # create the binary tree |
| 103 | + BTree = CBOrdTree() |
| 104 | + # add the root node |
| 105 | + root = BTree.addNode(0) |
| 106 | + # ask the user to insert values |
| 107 | + for i in range(0, 5): |
| 108 | + data = int(raw_input("insert the node value nr %d: " % i)) |
| 109 | + # insert values |
| 110 | + BTree.insert(root, data) |
| 111 | + print |
| 112 | + |
| 113 | + BTree.printTree(root) |
| 114 | + print |
| 115 | + BTree.printRevTree(root) |
| 116 | + print |
| 117 | + data = int(raw_input("insert a value to find: ")) |
| 118 | + if BTree.lookup(root, data): |
| 119 | + print "found" |
| 120 | + else: |
| 121 | + print "not found" |
| 122 | + |
| 123 | + print BTree.minValue(root) |
| 124 | + print BTree.maxDepth(root) |
| 125 | + print BTree.size(root) |
0 commit comments