This week we had our midterm exam, and we spent some
lecture time doing some
review.The new things we learnt this week is the
Binary Search Tree. To simplify, I will call
it BST and the Binary Tree BT.
The only difference between BST and BT is that there is a
rule in BST : left child <node<
right child. Since three is an order
among nodes in BST, it will be an extra time for a BST to
keep its property
when doing insert and delete. Also, based on the property, we can exactly
know
whether it goes left or right. Through if statement, we can control the
direction of
Recursive steps.
For example, I would show the way I write method on BST.
def insert (node, data):
return_node = node
if not node :
return_node = BTNode(data)
elif data < node.data:
node.left = insert(node.left, data)
elif data > node.data:
node.right = insert(node.right, data)
return return_node
Above is just my way of write method insert, and there may also be some other ways. The
most vital thing is that we create BST to search data more conveniently, so we should not
make the method very complicated.
没有评论:
发表评论