Summary of Abstract Data Type
In week 6, we learnt about abstract data Type -- Binary Search Tree:
class BTNode:
'''Binary Tree node.'''
def __init__(self, data, left=None, right=None):
''' (BTNode, object, BTNode, BTNode) -> NoneType
Create BTNode (self) with data and children left and right.
'''
self.data, self.left, self.right = data, left, right
def __eq__(self, other):
''' (BTNode, object) -> bool
Return whether BTNode (self) is equivalent to other.
>>> BTNode(7).__eq__('seven')
False
>>> b1 = BTNode(7, BTNode(5))
>>> b1.__eq__(BTNode(7, BTNode(5), None))
True
'''
return (type(self) == type(other) and
self.data == other.data and
(self.left, self.right) == (other.left, other.right))
The structure of BST is interesting:
The the root has two child, left node is less than the root, and the right node is larger than the root. The nodes that have child is called node.data, and child is called node.child. hight of the tree in the above picture is 4, since there are four level. There is also a recursive definition since the subtree is preordered.
The cod of the tree is write in the structure of: node.data, node.left, node. right. In node.left and node.right there could also contain a node.data structure.
Anyway, I only learnt about the basic things about tree, but it is an interesting type of abstract data type.

没有评论:
发表评论