施肥

shajara的Python项目详细描述


沙贾拉

**Shajara**在阿拉伯语中是树的意思。 这是一个操纵树木的项目。 有一棵树,有一个节点,还有一个节点处理器。

ProjectLicensePyPIDownloadsPython versionDocumentation StatusTravisCodecovCodeFactorcodebeat badgeCode Climate

说明

树有一个节点代表它的根。 每个节点都有一个标签、一个值和许多子节点。 将标记从父对象到子对象的弧。在

创建一棵树

使用shajara API

fromshajaraimportTree,Node#create a tree with a root labeled "a"t=Tree(Node(label="a"))# add childrend "b", "c" and "d" to "a"t.add_child("ab",Node(label="b")).add_child("ac",Node(label="c")).add_child("ad",Node(label="d"))# go to child "b" and add to it children "e" and "f"t.select_child("ab").add_child("be",Node(label="e")).add_child("bf",Node(label="f"))# go up to "a", go to child "d" and add to it children "g" and "h"t.up().select_child("ad").add_child("dg",Node(label="g")).add_child("dh",Node(label="h"))

使用dict表示法

^{pr2}$

想象一棵树

Graphviz树的点描述可以使用GraphvizProcessor生成

fromshajara.plotimportgraphviz_processor# create the tree t using one of the methods above...#process the tree using graphviz_processor, which returns a DOT description (string)graph=t.process(processor=graphviz_processor)# save the description in a file with extension .dotf=open("graphviz.dot","w")f.write(graph)f.close()

二叉搜索树(不平衡)

创建不平衡的二进制搜索树

fromshajaraimportTree,Nodefromshajara.search.binaryimportbinary_addervalues=[5,9,2,11,3,7,2]labels=["five","nine","two","eleven","three","seven","two_again"]t=Tree()foriinrange(len(values)):binary_adder.set_parameters(Node(value=values[i],label=labels[i]))t.process(processor=binary_adder)

搜索值

fromshajara.search.binaryimportbinary_searcher# create a tree t with a binary creator...search=[4,5,8,10,12]foriinsearch:binary_searcher.set_parameters(value=i)rel,node=t.process(processor=binary_searcher)ifrel=="=":print(str(i)+" is "+node.label)elifrel=="<":print(str(i)+" not found. It must be after "+str(node.value))else:print(str(i)+" not found. It must be before "+str(node.value))

结果是:

搜索最大值和最小值

fromshajara.search.binaryimportbinary_opti_searcher# create a tree t with a binary creator...binary_opti_searcher.set_parameters(search="min")min_node=t.process(processor=binary_opti_searcher)print("The minimum is "+str(min_node.value))binary_opti_searcher.set_parameters(search="max")max_node=t.process(processor=binary_opti_searcher)print("The maximum is "+str(max_node.value))

结果是:

特里亚

创建trie

fromshajara.search.trieimporttrie_adderstrings=["to","ten","inn","in","tea","A"]t=Tree()forstringinstrings:trie_adder.set_parameters(string)t.process(processor=trie_adder)

在trie中搜索单词

fromshajara.search.trieimporttrie_searcher# create a trie t...search=["tell","tea","inner"]forstringinsearch:trie_searcher.set_parameters(string)found,node=t.process(processor=trie_searcher)is_word=""ifnotnode.value:is_word=" (not in dictionary)"print("searching: "+string+", found: "+found+is_word)

结果是:

许可证

麻省理工学院执照

版权所有(c)2020 Abdelkrime Aries

欢迎加入QQ群-->: 979659372 Python中文网_新手群

推荐PyPI第三方库


热门话题
java计数步骤和操作   maven通过将第一个子类/jar添加到第二个子类路径,为一个子类创建jar,并为另一个子类执行goal exec:java   java JavaFX无法在ChoiceBox上调用setOnAction()?   swing使用Java,试图用图像显示JButton的可滚动列表,但显示的是文本而不是按钮   java无法读取跨行突出显示的确切文本   继承无法理解Java中super的功能   通过创建接口的模拟对象进行Java单元测试   活动布局中的java手指绘图?   java以看似随机的间隔获得NullPointerException,不知道为什么   java JButton位置不起作用   非序列化Java对象能否存储在mySQL BLob列中?   java如何使这个删除测试独立?   java Thymeleaf,无法访问该参数   java Solr CoreAware FilterFactory   java日志存储标准数据写入文件   socket上的java传递错误   java如何删除文件?   java Android有键盘小部件吗?   java方法,该方法将点对象作为参数,并根据其是否在直线段内返回true或false