数据结构 - 二叉查找树

#!/usr/bin/env python3.3
# -*- coding:utf-8 -*-
# Copyright 2013

'''
对任何节点X
1)其左子树中的关键字最大不大于key[X]
2)其右子树中的关键字最小不小于key[X]
'''

class Node:
    def __init__(self, key):
        self.key = key
        self.left = None
        self.right = None
        self.parent = None

    def __str__(self):
        return str(self.key)

    def successor(self):
        if self.right is not None:
            successor = self.right
            while successor.left is not None:
                successor = successor.left 
            return successor
        else:
            x = self
            successor = x.parent
            while successor is not None and successor.left == x:
                x = successor
                successor = successor.parent
            return successor

class BSTree:
    def __init__(self):
        self.root = None

    def insert(self, key):
        node = Node(key)
        if self.root is None:
            self.root = node
        else:
            root = self.root
            while root is not None:
                if key < root.key:
                    if root.left is not None:
                        root = root.left
                    else:
                        root.left = node
                        node.parent = root
                        break
                else:
                    if root.right is not None:
                        root = root.right
                    else:
                        root.right = node
                        node.parent = root
                        break

    def delete(self, key):
        node = self.search(key)
        if node is not None:
            if node.left is not None and node.right is not None:
                # 把要删除的节点替换为其后继节点
                successor = node.right
                while successor.left is not None:
                    successor = successor.left 
                node.data = successor.data
                node = successor
            if node.left is not None:
                # 要删除的节点没有右儿子
                node.left.parent = node.parent
                if node.parent is not None:
                    if node.parent.left == node:
                        node.parent.left = node.left
                    else:
                        node.parent.right = node.left
                else:
                    self.root = node.left
            elif node.right is not None:
                # 要删除的节点没有左儿子
                node.right.parent = node.parent
                if node.parent is not None:
                    if node.parent.left == node:
                        node.parent.left = node.right
                    else:
                        node.parent.right = node.right
                else:
                    self.root = node.right
            else:
                # 要删除的节点为叶子节点
                if node.parent is not None:
                    if node.parent.left == node:
                        node.parent.left = None
                    else:
                        node.parent.right = None
                else:
                    self.root = None

    def serach(self, key):
        node = self.root
        while node is not None:
            if key == node.key:
                return node
            elif key < node.key :
                node = node.left
            else:
                node = node.right

    def minimum(self):
        node = self.root
        while node.left is not None:
            node = node.left
        return node

    def maximum(self):
        node = self.root
        while node.right is not None:
            node = node.right
        return node

    def nodes(self):
        if self.root is not None:
            node_list = [self.root]
            while len(node_list) > 0:
                node = node_list.pop(0)
                yield node
                if node.left is not None:
                    node_list.append(node.left)
                if node.right is not None:
                    node_list.append(node.right)

def check_bstree(bst):
    if bst.root is not None:
        node_list = [bst.root]
        while len(node_list) > 0:
            node = node_list.pop(0)
            if node.left is not None:
                if node.key < node.left.key:
                    print('sort_error')
                    break
                node_list.append(node.left)
            if node.right is not None:
                if node.key > node.right.key:
                    print('sort_error')
                    break
                node_list.append(node.right)
        print('ok')
    else:
        print('empty')

 

posted @ 2013-03-31 16:19  Leung文  阅读(149)  评论(0编辑  收藏  举报