加权二叉树的实现与单元测试(python)
加权二叉树,每个节点存储权值(非负整数)。我们定义节点的inbalance作为它的左右子树的和权值的绝对差。一个空子树的权值为0。
我们的实现应该支持以下操作:
•更新节点权值。
•插入新节点。
•移动根节点的子树。
•找到最不平衡的节点,即最inbalance的节点。
因此需要维护一个树,其中树中的每个节点都持有一个权重值作为它的key
“inbalance”的value等同于此节点左右子树的求和权值之差。
Example:
Tree:
Imbalance:
Move subtree
您还必须支持move_subtree(node_a, node_b, left_child)函数,其中以node_a为根的子树被生成为node_b的一个子树。Left_child是一个布尔值,决定node_a是node_b的左子节点还是它的右子节点。如果node_b已经有一个子节点作为node_a的指定位置,那么函数应该什么都不做。
你必须在移动子树之后,确保所有节点的inbalance属性都是正确的。你可以假设node_b不在node_a的子树中,因此你不需要检查这个。
Example:
move_subtree(D,C,false):
Imbalance:
Find Max Imbalance
应该努力使实现尽可能高效。请注意,我们不要使用低效的方法,显式地测试实现的效率实现可能导致Ed超时。
你需要实现以下功能:
node.py
• init
• add_left_child, add_right_child
• update_weight
tree.py
• put
• move_subtree
• find_max_imbalance
(注意,如果需要,可以向类中添加额外的函数和变量,因此请放心修改和扩展这些函数,只要保留现有的函数签名和变量不变。)
Code
node.py
这个文件保存关于树中节点的所有信息。
Properties
Functions
init(weight)
•初始化节点属性。
add_left_child (child_node)
•将子节点添加为该节点的左子节点,如果该节点已经有一个左孩子结点,则不做任何操作。
•运行计算更新inbalance。
add_right_child (child_node)
•将子节点添加为该节点的右子节点,如果该节点已经存在右孩子,则不做任何操作
•运行计算更新不平衡。
is_external ()
•检查节点是否为叶节点。
get_left_child()(或node.left_child)
•返回该节点的左子节点。
get_right_child()(或node.right_child)
•返回该节点的右子节点。
update_weight(weight)
•设置节点的权重。
•运行计算更新不平衡。
get_imbalance()(或node.imbalance)
•返回该节点的不平衡状态。
tree.py
主树文件,保存与树和节点的所有交互。
Properties
root *Node 树的根节点
Functions
put(node, child, left_child)
•将子节点添加为左子节点或右子节点(取决于left_child)
如果节点B也有子节点,则没有。
move_subtree (node_a node_b left_child)
•移动节点A成为节点B的左子节点或右子节点(取决于left_child),
如果节点B也有子节点,则不执行任何操作。
•运行计算更新不平衡。
find_max_imbalance ()
•返回树的最大不平衡。
测试
我们在此存储库的tests目录中为您提供了一些测试用例。我们
将使用python的unittest包提供的单元测试。
运行测试
从基目录(包含node.py和tree.py的目录)运行
Python -m unittest -v tests/test_sample_tree.py tests/test_sample_node.py
或者,通过以下方式运行所有测试:
Python -m unittest -vv
测试代码如下:
from Node import Nodeimport unittestdef assert_equal(got, expected, msg):"""Simple assert helper"""assert expected == got, \"[{}] Expected: {}, got: {}".format(msg, expected, got)class SampleNodeTestCases(unittest.TestCase):"""Testing functionality of the Node class"""def setUp(self):"""Set up the tree to be used throughout the testThis is the tree given in the sampleA(5)/ \C(2) D(8)/B(10)"""self.A = Node(5)self.B = Node(10)self.C = Node(2)self.D = Node(8)self.C.add_left_child(self.B)self.A.add_left_child(self.C)self.A.add_right_child(self.D)'''def test_is_external(self):"""Test that the sample tree has been correctly classified"""assert_equal(self.A.is_external(), False, "A is not external")assert_equal(self.B.is_external(), True, "B is external")assert_equal(self.C.is_external(), False, "C is not external")assert_equal(self.D.is_external(), True, "D is external")def test_get_left_child(self):"""Test that the sample tree returns the correct left child"""assert_equal(self.A.get_left_child(), self.C, "A's left child is C")assert_equal(self.C.get_left_child(), self.B, "C's left child is B")assert_equal(self.D.get_left_child(), None, "D has no left child")assert_equal(self.B.get_left_child(), None, "B has no left child")def test_get_right_child(self):"""Test that the sample tree returns the correct right child"""assert_equal(self.A.get_right_child(), self.D, "A's right child is D")assert_equal(self.C.get_right_child(), None, "C has no right child")assert_equal(self.D.get_right_child(), None, "D has no right child")assert_equal(self.B.get_right_child(), None, "B has no right child")
'''def test_get_imbalance(self):"""Test that the sample tree returns the correct imbalance"""assert_equal(self.A.get_imbalance(), 4, "A has an imbalance of 4")assert_equal(self.C.get_imbalance(), 10, "C has an imbalance of 10")assert_equal(self.D.get_imbalance(), 0, "D has no imbalance")assert_equal(self.B.get_imbalance(), 0, "B has no imbalance")def test_update_weight(self):"""Test that the sample tree updates the weight correctly"""self.A.update_weight(10)assert_equal(self.A.get_imbalance(), 4, "A has an imbalance of 4")assert_equal(self.C.get_imbalance(), 10, "C has an imbalance of 10")assert_equal(self.D.get_imbalance(), 0, "D has no imbalance")assert_equal(self.B.get_imbalance(), 0, "B has no imbalance")self.B.update_weight(3)assert_equal(self.A.get_imbalance(), 3, "A has an imbalance of 3")assert_equal(self.C.get_imbalance(), 3, "C has an imbalance of 3")assert_equal(self.D.get_imbalance(), 0, "D has no imbalance")assert_equal(self.B.get_imbalance(), 0, "B has no imbalance")"""Final Tree:A(10)/ \C(2) D(8)/B(3)"""def test_propagate_imbalance(self):"""Test that the sample tree propagates the imbalance correctly when adding children"""self.D.add_right_child(Node(7))"""Tree:A(5)/ \C(2) D(8)/ \B(10) E(7)"""assert_equal(self.A.get_imbalance(), 3, "A has an imbalance of 3")assert_equal(self.C.get_imbalance(), 10, "C has an imbalance of 10")assert_equal(self.D.get_imbalance(), 7, "D has an imbalance of 7")assert_equal(self.B.get_imbalance(), 0, "B has no imbalance")assert_equal(self.D.get_right_child().get_imbalance(),0, "E has no imbalance")
from Node import Node
from Tree import Treeimport unittestdef assert_equal(got, expected, msg):"""Simple assert helper"""assert expected == got, \"[{}] Expected: {}, got: {}".format(msg, expected, got)class SampleTreeTestCases(unittest.TestCase):"""Testing functionality of the Tree class"""def setUp(self):"""Set up the tree to be used throughout the testThis is the tree given in the sampleA(5)/ \C(2) D(8)/B(10)"""self.A = Node(5)self.tree = Tree(self.A)self.B = Node(10)self.C = Node(2)self.D = Node(8)self.tree.put(self.A, self.C, True)self.tree.put(self.A, self.D, False)self.tree.put(self.C, self.B, True)def test_construction(self):"""Test that the sample tree has been correctly constructed"""assert_equal(self.A.is_external(), False, "A is not external")assert_equal(self.B.is_external(), True, "B is external")assert_equal(self.C.is_external(), False, "C is not external")assert_equal(self.D.is_external(), True, "D is external")assert_equal(self.A.get_imbalance(), 4, "A has an imbalance of 4")assert_equal(self.C.get_imbalance(), 10, "C has an imbalance of 10")assert_equal(self.D.get_imbalance(), 0, "D has no imbalance")assert_equal(self.B.get_imbalance(), 0, "B has no imbalance")def test_put(self):"""Test that the sample tree puts nodes correctly"""E = Node(7)self.tree.put(self.C, E, False)"""A(5)1/ \C(2) D(8)/ \B(10) E(7)"""assert_equal(self.A.is_external(), False, "A is not external")assert_equal(self.B.is_external(), True, "B is external")assert_equal(self.C.is_external(), False, "C is not external")assert_equal(self.D.is_external(), True, "D is external")assert_equal(E.is_external(), True, "E is external")assert_equal(self.A.get_imbalance(), 11, "A has an imbalance of 11")assert_equal(self.C.get_imbalance(), 3, "C has an imbalance of 3")assert_equal(self.D.get_imbalance(), 0, "D has no imbalance")assert_equal(self.B.get_imbalance(), 0, "B has no imbalance")assert_equal(E.get_imbalance(), 0, "E has no imbalance")def test_find_max_imbalance(self):"""Test that the sample tree finds the correct node with the maximum imbalance"""assert_equal(self.tree.find_max_imbalance(), 10,"C has the maximum imbalance with value 10")
测试成功图
nodetest
treetest
树的建立(只是我的版本,你们可以写不同的)
Node.py
#Node classclass Node:weight: intimbalance: int# These are the defined properties as described above#Initialises the properties of the node.def __init__(self, weight):"""The constructor for the Node class.:param weight: If no input value."""self.weight = weight# the subtree total weightself.sum = weightself.left_child = Noneself.right_child = Noneself.parent = Noneself.imbalance = 0#Adds the child node as the left child of the node, does nothing if the node already has a left child.def add_left_child(self,child_node):if self.left_child is not None:return Falseelse:self.left_child = child_nodechild_node.parent = self#Runs calculations for updating the imbalance.self.parent_imbalance()return True#Adds the child node as the right child of the node, does nothing if the node already has a right child.def add_right_child(self,child_node):if self.right_child is not None:return Falseelse:self.right_child = child_nodechild_node.parent = self# Runs calculations for updating the imbalance.self.parent_imbalance()return True# Checks if the node is a leaf.def is_external(self):if self.right_child is None and self.left_child is None:return Trueelse:return Falsedef get_left_child(self):return self.left_childdef get_right_child(self):return self.right_childdef update_weight(self,weight):self.sum +=(weight-self.weight)self.weight = weightself.parent_imbalance()def get_imbalance(self) -> int:return self.imbalancedef parent_imbalance(self):p = selfif p is None:returnwhile p.parent is not None:fun.reimbalance(p)p = p.parentfun.reimbalance(p) #rootclass fun:def reimbalance(rot: Node) -> int:if rot.left_child is None and rot.right_child is None:rot.imbalance = 0rot.sum = rot.weightreturn rot.sumif rot.left_child is not None and rot.right_child is None:#fun.reimbalance(rot.left_child)rot.imbalance = rot.left_child.sumrot.sum = rot.weight + rot.left_child.sumreturn rot.sumif rot.left_child is None and rot.right_child is not None:#fun.reimbalance(rot.right_child)rot.imbalance = rot.right_child.sumrot.sum = rot.weight + rot.right_child.sumreturn rot.suml_sum = fun.reimbalance(rot.left_child)r_sum = fun.reimbalance(rot.right_child)rot.sum = l_sum + r_sum + rot.weightrot.imbalance = abs(l_sum - r_sum)return rot.sum
Tree.py
from Node import Node
"""
Tree
----------This class represents the Binary TreeEach Tree consists of the following properties:- root: The root of the TreeThe class also supports the following functions:- put(node, child, left_child): Adds child to the given node as the left or right child depending on the value of left_child- move_subtree(node_a, node_b, left_child): Move node_a to the left or right child of node_b depending on the value of left_child- find_max_imbalance(): Finds the node with the maximum imbalance in the tree
"""class Tree():# These are the defined properties as described aboveroot: Nodeglobal mm = -1def __init__(self, root: Node = None) -> None:"""The constructor for the Tree class.:param root: The root node of the Tree."""self.root = rootself.m = -1def put(self, node: Node, child: Node, left_child: bool) -> None:"""You are guranteed that the given node is not already part of the tree:param node: The node to add the child to.:param child: The child to add to the node.:param left_child: True if the child should be added to the left child, False otherwise."""if left_child is True:if node.get_left_child() is None:node.add_left_child(child)#node.left_child.update_weight(child.weight)node.left_child.parent = nodeelse:returnelse:if node.get_right_child() is None:node.add_right_child(child)#node.right_child.update_weight(child.weight)node.right_child.parent = nodeelse:return# TODO Add the child to the node as the left or right child depending on the value of left_childdef move_subtree(self, node_a: Node, node_b: Node, left_child: bool) -> None:"""Moves the subtree rooted at node_a to the left or right child of node_b depending on the value of left_child.If node_b already has a child at the indicated position, this function should do nothingYou can safely assume that node_b is not descendent of node_a.:param node_a: The root of the subtree to move.:param node_b: The node to add the subtree to.:param left_child: True if the subtree should be added to the left child, False otherwise."""a = p = node_bif not node_b.root: #if root == null , directly insertnode_b = node_areturnelif left_child is True:if node_b.get_left_child() is None:node_b.add_left_child(node_a)node_b.left_child.parent = node_ba.parent_imbalance()else:returnelse:if node_b.get_right_child() is None:node_b.add_right_child(node_a)node_b.right_child.parent = node_bp.parent_imbalance()else:return# TODO Move the subtree rooted at node_a to the left or right child of node_bdef find_max_imbalance(self) -> int:"""Finds the node with the maximum imbalance in the tree.:return: The node with the maximum imbalance."""return c.findmax(self.root)# TODO Find the node with the maximum imbalanceclass c():def findmax(rot: Node) -> int:global mcompare = rot.get_imbalance()m = compare if m < compare else mif rot.left_child is not None:left = c.findmax(rot.get_left_child())m = left if m < left else mif rot.right_child is not None:right = c.findmax(rot.get_right_child())m = right if m < right else mreturn m
加权二叉树的实现与单元测试(python)相关推荐
- 数据结构之图:加权无向图与寻找最小生成树,Python——27
加权无向图与prim算法和Kruskal算法寻找最小生成树 加权无向图的介绍 引入 加权无向图是一种为每条边关联一 个权重值或 是成本的图模型.这种图能够自然地表示许多应用.在一副航空图中,边表示航线 ...
- 机器学习笔记——2 简单线性模型及局部加权线性模型的基本原理和python实现(参数估计的两个基本角度:几何直观和概率直观。函数最值问题的两大基本算法:梯度方法与迭代方法)
简单线性模型及局部加权线性模型的基本原理和python实现(参数估计的两个基本角度:几何直观和概率直观.函数最值问题的两大基本算法:梯度方法与迭代方法) 线性模型是什么? 线性模型是监督学习的各类学习 ...
- python二叉树遍历算法_分享python实现的二叉树定义与遍历
这篇文章主要介绍了python实现的二叉树定义与遍历算法,结合具体实例形式分析了基于Python定义的二叉树及其常用遍历操作实现技巧,需要的朋友可以参考下 本文实例讲述了python实现的二叉树定义与 ...
- 二叉树的一些leetcode题目+python(c++)
二叉树考点主要有: 1.三种遍历方式,以及构造二叉树等: 2.求深度,最长直径,最长路径,公共祖先等等; 3.合并二叉树,翻转二叉树,判断平衡性,对称性等; 4.从前序与中序构造二叉树,中序与后序构造 ...
- python二叉树广度遍历_黄哥Python: 二叉树的广度优先搜索的二种方法
特别提示,转行的朋友,2019年不学习数据结构和算法,不刷Leetcode 等面试题库,是找不到程序员工作或者说找不到好的工作.黄哥:黄哥Python:提醒要转行当程序员的朋友,学习要分先后主次zh ...
- 判断某数组是不是二叉树的前序遍历序列 python递归
code class Solution:def VerifySquenceOfBST(self, sequence):# write code hereif len(sequence) <= 0 ...
- LeetCode 426. Convert Binary Search Tree to Sorted Doubly Linked List--转换二叉树为双向链表--Java,C++,Python解法
题目地址:Convert Binary Search Tree to Sorted Doubly Linked List - LeetCode Convert a BST to a sorted ci ...
- 二叉树的后序遍历Python解法
给你一棵二叉树的根节点 root ,返回其节点值的 后序遍历 . 输入:root = [1,null,2,3] 输出:[3,2,1] 解析: 和前序遍历一样,递归,换个位置即可. # Definiti ...
- LeetCode —— 257. 二叉树的所有路径(Python)
给定一个二叉树,返回所有从根节点到叶子节点的路径. 说明: 叶子节点是指没有子节点的节点. 示例: -------- 解题思路: (1)用变量string记录从根结点到当前结点经过的结点路径. (2) ...
- lintcode 7. 二叉树的序列化和反序列化 Python代码
'''7. 二叉树的序列化和反序列化 描述 设计一个算法,并编写代码来序列化和反序列化二叉树.将树写入一个文件被称为"序列化",读取文件后重建同样的二叉树被称为"反序列化 ...
最新文章
- python 线性回归_用Python实现线性回归算法
- AtCoder AGC035F Two Histograms (组合计数、容斥原理)
- 前端学习(568):元素定高 容器定高 为什么不能居中
- django给mysql配主从_django中的mysql主从读写分离:一、配置mysql主从分离
- 使用SpringBoot+JPA报错Incorrect syntax near 'hibernate_sequence'
- 052、JVM实战总结:从测试到上线:如何分析JVM运行状况及合理优化?
- HALCON: 本地程序函数(.hdev或.dev)、HDevelop函数文件或外部函数(.hdvp)及库函数(.hdpl)使用详解
- 用bitbucket积累代码
- 新颖的自我介绍_有哪些非常有创意的自我介绍?
- word之中快速插入已有公式的几种方法
- Excel工作表保护忘记密码解决方法(.xls和.xlsx)
- spring-data-redis 实现用户登录次数限制以及冻结时间重试机制
- js reduce 累加数组里对象某个属性的和 NaN
- 龚胤全云栖大会_2018杭州云栖大会-大会嘉宾
- php获取其他网站的cookie,php获取(curl) 带有cookie的网页数据采集方法
- 电机变频器测量现场,电机效率过百?
- 计算机网络基础-五层因特网协议栈
- javax.net.ssl.SSLException: hostname in certificate didn't match:
- 宝安区2021年高考成绩查询入口,宝安区2021年学位申请房屋锁定网上查询说明(附入口)...
- 液晶显示器护眼桌面设置(又称润眼桌面)——把桌面设置成为浅绿色的