Shajara
Description
A tree has one node which represents its root. Each node has a label, a value and many children. The arc from the parent to the child is labeled.
Create a tree
Using shajara API
from shajara import Tree, 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"))
Using a dict representation
from shajara.create import GenerateProcessor
from shajara import Tree
# the tree representation : the arcs must be labeled
rep = {
"label": "a",
"children": {
"ab": {
"label": "b",
"children": {
"be": {"label": "e"},
"bf": {"label": "f"}
}
},
"ac": {"label": "c"},
"ad": {"label": "d"}
}
}
# Create a generator to this representation
generator = GenerateProcessor(rep)
#create an ampty tree (acrually the root exists without a label or a value)
t = Tree()
#fill the tree using the generator
t.process(processor=generator)
Visualize a tree
DOT description of a tree for Graphviz can be generated using GraphvizProcessor
from shajara.plot import graphviz_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 .dot
f = open("graphviz.dot", "w")
f.write(graph)
f.close()
Binary search trees (not balanced)
Create unbalanced binary search trees
from shajara import Tree, Node
from shajara.search.binary import binary_adder
values = [5, 9, 2, 11, 3, 7, 2]
labels = ["five", "nine", "two", "eleven", "three", "seven", "two_again"]
t = Tree()
for i in range(len(values)):
binary_adder.set_parameters(Node(value=values[i], label=labels[i]))
t.process(processor=binary_adder)
Search a value
from shajara.search.binary import binary_searcher
# create a tree t with a binary creator
...
search = [4, 5, 8, 10, 12]
for i in search:
binary_searcher.set_parameters(value=i)
rel, node = t.process(processor=binary_searcher)
if rel == "=" :
print (str(i) + " is " + node.label)
elif rel =="<" :
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))
The result :
Search the max and the min
from shajara.search.binary import binary_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))
The result :
Trie
Create a trie
from shajara.search.trie import trie_adder
strings = ["to", "ten", "inn", "in", "tea", "A"]
t = Tree()
for string in strings:
trie_adder.set_parameters(string)
t.process(processor=trie_adder)
Search a word in the trie
from shajara.search.trie import trie_searcher
# create a trie t
...
search = ["tell", "tea", "inner"]
for string in search:
trie_searcher.set_parameters(string)
found, node = t.process(processor=trie_searcher)
is_word = ""
if not node.value:
is_word = " (not in dictionary)"
print("searching: " + string + ", found: " + found + is_word)
The result :
License
MIT License
Copyright (c) 2020 Abdelkrime Aries
Metadata
Release files for shajara 0.2.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| shajara-0.2.0.tar.gz | 5.6 kB | Without provenance | Details for shajara-0.2.0.tar.gz |
Release files / shajara-0.2.0.tar.gz
| Download URL | shajara-0.2.0.tar.gz |
|---|---|
| Size | 5.6 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
d9677fd3a11a290a1110053272ac526eec833eb686c4c9f5084e9627b3940af6
|
|
BLAKE2b-256 checksum How to use checksums |
ddcad80258595e3f70ca532fd4a7b300fc57fa51b663ef381e6e540cc2f907f6
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/46.1.3 requests-toolbelt/0.9.1 tqdm/4.45.0 CPython/3.6.7
|
Security for shajara 0.2.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.