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 ~ % 

  • 相关阅读:
    电子书下载:Web开发新体验ASP.NET 3.5 MVC架构与实战
    电子书下载:Professional ASP.NET MVC 2
    电子书下载:Pro ASP.NET MVC2 Framework 2nd
    为Vmware硬盘减肥瘦身
    CKFinder 2.0.1破解版
    电子书下载:ExtJS4 Web Application Development Cookbook
    电子书下载:Beginning ASP.NET 2.0 and Databases
    Delphi中WebBrowser(或者EmbeddedWebBrowser)控件打开部分网站报“Invalid floating point operation”异常的解决方法
    电子书下载:Test Drive ASP.NET MVC
    电子书下载:Professional ASP.NET 2.0
  • 原文地址:https://www.cnblogs.com/sea-stream/p/13773450.html
Copyright © 2011-2022 走看看