用python实现搜索树_实现二进制搜索树(Python)

'''

Binary Search Tree is a binary tree(that is every node has two branches),

in which the values contained in the left subtree is always less than the

root of that subtree, and the values contained in the right subtree is

always greater than the value of the root of the right subtree.

For more information about binary search trees, refer to :

http://en.wikipedia.org/wiki/Binary_search_tree

'''#Only for use in Python 2.6.0a2 and laterfrom__future__importprint_functionclassNode:# Constructor to initialize data# If data is not given by user,its taken as Nonedef__init__(self,data=None,left=None,right=None):self.data=data

self.left=left

self.right=right# __str__ returns string equivalent of Objectdef__str__(self):return"Node[Data = %s]"%(self.data,)classBinarySearchTree:def__init__(self):self.root=None'''

While inserting values in a binary search tree, we first check

whether the value is greater than, lesser than or equal to the

root of the tree.

We initialize current node as the root.

If the value is greater than the current node value, then we know that

its right location will be in the right subtree. So we make the current

element as the right node.

If the value is lesser than the current node value, then we know that

its right location will be in the left subtree. So we make the current

element as the left node.

If the value is equal to the current node value, then we know that the

value is already contained in the tree and doesn't need to be reinserted.

So we break from the loop.

'''definsert(self,val):if(self.root==None):self.root=Node(val)else:current=self.rootwhile1:if(current.data>val):if(current.left==None):current.left=Node(val)breakelse:current=current.leftelif(current.data

In preorder traversal, we first print the current element, then

move on to the left subtree and finally to the right subree.

'''defpreorder(self,node):if(node==None):returnelse:print(node.data,end=" ")self.preorder(node.left)self.preorder(node.right)'''

In inorder traversal, we first move to the left subtree, then print

the current element and finally move to the right subtree.

'''#Important : Inorder traversal returns the elements in sorted form.definorder(self,node):if(node==None):returnelse:self.inorder(node.left)print(node.data,end=" ")self.inorder(node.right)'''

In postorder traversal, we first move to the left subtree, then to the

right subtree and finally print the current element.

'''defpostorder(self,node):if(node==None):returnelse:self.postorder(node.left)self.postorder(node.right)print(node.data,end=" ")tree=BinarySearchTree()tree.insert(1)tree.insert(9)tree.insert(4)tree.insert(3)tree.insert(5)tree.insert(7)tree.insert(10)tree.insert(0)print("Preorder Printing")tree.preorder(tree.root)print("\n\nInorder Printing")tree.inorder(tree.root)print("\n\nPostOrder Printing")tree.postorder(tree.root)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值