binary_search_tree

reference:https://github.com/EchoLLLiu/DataStructure/blob/master/ch04tree/BinarySearchTree.py

#-*- coding:utf-8 -*-
# @Author:zhangy
# @Time:2019-09-27 16:16
#reference:https://github.com/EchoLLLiu/DataStructure/blob/master/ch04tree/BinarySearchTree.py
class TreeNode:
    '''二叉搜索树节点的定义'''
    def __init__(self,val):
        self.val = val
        self.left = None
        self.right = None

class operation_tree:
    '''二叉搜索树操作'''
    def insert(self,root,val):
        '''二叉搜索树插入操作'''
        if root == None:
            root = TreeNode(val)
        elif val < root.val:
            root.left = self.insert(root.left,val)
        elif val >root.val:
            root.right = self.insert(root.right,val)
        return root

    def query(self,root,val):
        '''二叉搜索树查询操作'''
        if root == None:
            return False
        if root.val == val:
            return True
        elif root.val > val:
            return self.query(root.left,val)
        elif root.val < val:
            return self.query(root.right,val)

    def find_min(self,root):
        '''查找搜索二叉树中最小值的点'''
        if root.left:
            return self.find_min(root.left)
        else:
            return root

    def find_max(self,root):
        '''查找搜索二叉树中最大值的点'''
        if root.right:
            return self.find_max(root.right)
        else:
            return root

    def delNode(self,root,val):
        '''删除搜索二叉树中值为val的点'''
        if root == None:
            return
        if val < root.val:
            root.left = self.delNode(root.left,val)
        elif val > root.val:
            root.right = self.delNode(root.right,val)
        #当val==root.val时,分为四种情况,只有左子树或者只有右子树或左右子树都有或都没有
        else:
            if root.left and root.right:
                #左右子树都存在,则要找到右子树的最小点
                temp = self.find_min(root.right)
                root.val = temp.val
                #再把右子树中最小值节点删除
                root.right = self.delNode(root.right,temp.val)
            elif root.right == None and root.left == None:
                return None
            elif root.right == None:
                root = root.left
            elif root.left == None:
                root = root.right
        return root

    def print_tree(self,root):
        '''打印二叉搜索树(中序打印,有序数列)'''
        if root == None:
            return
        self.print_tree(root.left)
        print root.val
        self.print_tree(root.right)

	def max_deep(self,root):
        '''最大二叉树深'''
        if root == None:
            return 0
        elif not root.left and not root.right:
            return 1
        else:
            return 1+max(self.max_deep(root.right),self.max_deep(root.left))



if __name__ == '__main__':
    List = [17, 5, 35, 2, 11, 29, 38, 9, 16, 8]
    root = None
    op = operation_tree()
    for val in List:
        root = op.insert(root,val)
    print ("中序打印二叉树: ")
    op.print_tree(root)
    print '-------------分割线-----------------'
    print "根节点的值为:",root.val
    print '-------------分割线-----------------'
    print "树的最大值为:",op.find_max(root).val
    print '-------------分割线-----------------'
    print "树的最小值为:", op.find_min(root).val
    print '-------------分割线-----------------'
    print '查询树中值为5的节点:', op.query(root, 5)
    print '-------------分割线-----------------'
    print '查询树中值为100的节点:', op.query(root, 100)
    print '-------------分割线-----------------'
    print "删除树中值为16的节点"
    op.delNode(root,16)
    print ("中序打印二叉树: ")
    op.print_tree(root)
    print '-------------分割线-----------------'
    print "删除树中值为5的节点"
    op.delNode(root, 5)
    print ("中序打印二叉树: ")
    op.print_tree(root)
    print ("二叉树最大深度: ")
    m_deep=op.max_deep(root)
    print m_deep

输出结果如下:

C:\Users\Administrator\Anaconda3\envs\py2\python.exe "F:/ML2/算法导论/practice/12.2.2 binary_search_tree.py"
中序打印二叉树: 
2
5
8
9
11
16
17
29
35
38
-------------分割线-----------------
根节点的值为: 17
-------------分割线-----------------
树的最大值为: 38
-------------分割线-----------------
树的最小值为: 2
-------------分割线-----------------
查询树中值为5的节点: True
-------------分割线-----------------
查询树中值为100的节点: False
-------------分割线-----------------
删除树中值为16的节点
中序打印二叉树: 
2
5
8
9
11
17
29
35
38
-------------分割线-----------------
删除树中值为5的节点
中序打印二叉树: 
2
8
9
11
17
29
35
38
二叉树最大深度: 
4
Process finished with exit code 0

你可能感兴趣的:(树)