zoukankan      html  css  js  c++  java
  • python3 二叉树添加以及删除节点

    code

    class node:
        def __init__(self, data):
            self.data = data
            self.layer=None
            self.left_child = None
            self.right_child = None
            self.parent_node=None
    
        def add_left_node(self,data):
            if self.left_child == None:
                tmp_node=node(data)
                tmp_node.layer=self.layer+1
                tmp_node.parent_node=self
    
                self.left_child = tmp_node
            else:
                t = node(data)
                t.layer=self.layer+1
                t.parent_node=self
    
                t.left_child = self.left_child.left_child
                t.right_child = self.left_child.right_child
    
                self.left_child = t
    
        def add_right_node(self,data):
            if self.right_child == None:
                tmp_node=node(data)
                tmp_node.layer=self.layer+1
                tmp_node.parent_node=self
    
                self.right_child = tmp_node
            else:
                t = node(data)
                t.layer=self.layer+1
                t.parent_node=self
    
                t.right_child = self.right_child.right_child
                t.right_child = self.right_child.right_child
    
                self.right_child = t
    
        def remove_left_node(self):
            if self.left_child == None:
                pass
            else:
                self.left_child=None
    
        def remove_right_node(self):
            if self.right_child == None:
                pass
            else:
                self.right_child=None
    
    
    
    class BinaryTree:
        def __init__(self, root_node):
            self.root_node = root_node
            self.root_node.layer=1
    
        def set_root_node(self, tmp_node):
            tmp_node.left_child=self.root_node.left_child
            tmp_node.right_child=self.root_node.right_child
            tmp_node.layer=1
            self.root_node  = tmp_node
    
        def get_root_node(self):
            return self.root_node
    
        def show_tree(self):
            if(self.root_node):
                #to do
                pass
                
    #测试
    root_node=node("a")
    BinaryTree=BinaryTree(root_node)
    BinaryTree.root_node.add_left_node("b")
    BinaryTree.root_node.add_right_node("c")
    
    print("{}({})".format(BinaryTree.root_node.data,BinaryTree.root_node.layer))#a
    print("{}({})".format(BinaryTree.root_node.left_child.data,BinaryTree.root_node.left_child.layer))#b
    print("{}({})".format(BinaryTree.root_node.right_child.data,BinaryTree.root_node.right_child.layer))#c
    
    BinaryTree.set_root_node(node("d"))
    
    print("{}({})".format(BinaryTree.root_node.data,BinaryTree.root_node.layer))#d
    print("{}({})".format(BinaryTree.root_node.left_child.data,BinaryTree.root_node.left_child.layer))#b
    print("{}({})".format(BinaryTree.root_node.right_child.data,BinaryTree.root_node.right_child.layer))#c
    
    BinaryTree.root_node.left_child.add_left_node("e")
    
    print("{}({})".format(BinaryTree.root_node.left_child.left_child.data,BinaryTree.root_node.left_child.left_child.layer))#e
    
    print("{}({})".format(BinaryTree.root_node.left_child.left_child.parent_node.data,BinaryTree.root_node.left_child.left_child.parent_node.layer))#b

     outputs

    macname@MacdeMBP ~ % python3 test.py
    a(1)
    b(2)
    c(2)
    d(1)
    b(2)
    c(2)
    e(3)
    b(2)
    macname@MacdeMBP ~ % 

  • 相关阅读:
    Next Permutation
    Substring with Concatenation of All Words
    Divide Two Integers
    Remove Duplicates from Sorted Array
    3sum closest
    ThreadPoolExecutor参数与拒绝策略
    多线程情况下ArrayList 如何解决线性安全问题
    ArrayList扩容机制jdk1.8
    SpringCloud--工作流程(好文)
    Java面试——TCP与HTTP
  • 原文地址:https://www.cnblogs.com/sea-stream/p/13773450.html
Copyright © 2011-2022 走看看