码迷,mamicode.com
首页 > 编程语言 > 详细

二叉树(python)

时间:2019-07-10 23:15:18      阅读:246      评论:0      收藏:0      [点我收藏+]

标签:from   queue   ever   import   order   使用   code   class   param   

# -*- coding: utf-8 -*-


from collections import deque

class Queue(object):  
    def __init__(self):
        self._items = deque()

    def append(self, value):
        return self._items.append(value)

    def pop(self):
        return self._items.popleft()

    def empty(self):
        return len(self._items) == 0


class Stack(object):
    def __init__(self):
        self._items = deque()

    def push(self, value):
        return self._items.append(value)

    def pop(self):
        return self._items.pop()

    def empty(self):
        return len(self._items) == 0


class BinTreeNode(object):
    def __init__(self, data, left=None, right=None):
        self.data, self.left, self.right = data, left, right


class BinTree(object):
    def __init__(self, root=None):
        self.root = root

    @classmethod
    def build_from(cls, node_list):
        """build_from

        :param node_list: {‘data‘: ‘A‘, ‘left‘: None, ‘right‘: None, ‘is_root‘: False}
        """
        node_dict = {}
        for node_data in node_list:
            data = node_data[data]
            node_dict[data] = BinTreeNode(data)
        for node_data in node_list:
            data = node_data[data]
            node = node_dict[data]
            if node_data[is_root]:
                root = node
            node.left = node_dict.get(node_data[left])
            node.right = node_dict.get(node_data[right])
        return cls(root)

    def preorder_trav(self, subtree):
        if subtree is not None:
            print(subtree.data)
            self.preorder_trav(subtree.left)
            self.preorder_trav(subtree.right)

    def preorder_trav_use_stack(self, subtree):
        s = Stack()
        if subtree:
            s.push(subtree)
            while not s.empty():
                top_node = s.pop()
                print(top_node.data)    
                if top_node.right:
                    s.push(top_node.right)
                if top_node.left:
                    s.push(top_node.left)

    def inorder_trav(self, subtree):
        if subtree is not None:
            self.inorder_trav(subtree.left)
            print(subtree.data)
            self.inorder_trav(subtree.right)

    def yield_inorder(self, subtree):  
        if subtree:
            yield from self.inorder(subtree.left)
            yield subtree.val
            yield from self.inorder(subtree.right)

    def reverse(self, subtree):
        if subtree is not None:
            subtree.left, subtree.right = subtree.right, subtree.left
            self.reverse(subtree.left)
            self.reverse(subtree.right)

    def layer_trav(self, subtree):
        cur_nodes = [subtree]
        next_nodes = []
        while cur_nodes or next_nodes:
            for node in cur_nodes:
                print(node.data)
                if node.left:
                    next_nodes.append(node.left)
                if node.right:
                    next_nodes.append(node.right)
            cur_nodes = next_nodes  
            next_nodes = []

    def layer_trav_use_queue(self, subtree):
        q = Queue()
        q.append(subtree)
        while not q.empty():
            cur_node = q.pop()
            print(cur_node.data)
            if cur_node.left:
                q.append(cur_node.left)
            if cur_node.right:
                q.append(cur_node.right)


node_list = [
    {data: A, left: B, right: C, is_root: True},
    {data: B, left: D, right: E, is_root: False},
    {data: D, left: None, right: None, is_root: False},
    {data: E, left: H, right: None, is_root: False},
    {data: H, left: None, right: None, is_root: False},
    {data: C, left: F, right: G, is_root: False},
    {data: F, left: None, right: None, is_root: False},
    {data: G, left: I, right: J, is_root: False},
    {data: I, left: None, right: None, is_root: False},
    {data: J, left: None, right: None, is_root: False},
]


btree = BinTree.build_from(node_list)
print(====先序遍历=====)
btree.preorder_trav(btree.root)

print(====使用 stack 实现先序遍历=====)
btree.preorder_trav_use_stack(btree.root)

print(====层序遍历=====)
btree.layer_trav(btree.root)
print(====用队列层序遍历=====)
btree.layer_trav_use_queue(btree.root)

btree.reverse(btree.root)
print(====反转之后的结果=====)
btree.preorder_trav(btree.root)

 

二叉树(python)

标签:from   queue   ever   import   order   使用   code   class   param   

原文地址:https://www.cnblogs.com/muzinan110/p/11167036.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!