Skip to main content

Simple implementation of Tree Data Structure

Project description

treeds

Easy to use Tree Data Structure with many useful methods.

Developed by Tatwik.

Examples of using treeds

Tree Example

We will consider this image as an example for the below code.

Creating a Tree


  1. Adding the root node at initialization.
  from treeds import Tree
  tree = Tree(root_nodes=['a'])
  1. We can have multiple root nodes, Just by passing multiple nodes to the parameter 'root_nodes'
  1. Adding the root node after initialization.
  from treeds import Tree
  tree = Tree()
  tree.add_node(('nil', 'a'))
  1. Here the parent node of the root node 'a' is 'nil'.
  2. You can modify the 3rd line of the above code as
tree.add_children('nil', ['a'])

The above function helps to add multiple root nodes instead of one root node 'a'.

  1. Adding the remaining nodes to the tree.
# level-1
tree.add_children('a', ['b', 'c', 'd'])
# level-2
tree.add_children('b', ['e', 'f'])
tree.add_children('d', ['g', 'h'])
# level-3
tree.add_children('h', ['i', 'j'])
  1. Now we have created a Tree that's in the above image.
  1. Let's print the Tree we have created.
print(tree.tree)

Output:

{'nil': (None, ['a']), 'a': ('nil', ['b', 'c', 'd']), 'b': ('a', ['e', 'f']), 'c': ('a', []), 'd': ('a', ['g', 'h']), 'e': ('b', []), 'f': ('b', []), 'g': ('d', []), 'h': ('d', ['i', 'j']), 'i': ('h', []), 'j': ('h', [])}

Let's check the above Output dictionary.

  • Dict Keys - Node names
  • Dict Values - (parent node, [child nodes])

Let's see an example: Consider: 'a': ('nil', ['b', 'c', 'd'])

  • Here 'a' is the node name.
  • 'nil' is the parent node of 'a'.
  • ['b', 'c', 'd'] are the children nodes of node 'a'.

Using the methods of Tree class


  • add_node()

Adds the node to the tree.

  • parameter node: Tuple with parent and child.
tree.add_node(('parent_node', 'node_name'))
  • add_children()

Adds multiple children to the node.

  • parameter node: The name of the node.
  • parameter children: List of children of the node.
tree.add_children('node_name', ['child_node1', 'child_node2', 'child_node3'])
  • get_parent()

Returns the parent of the given node.

  • parameter node: The name of the node.
  • returns: The parent of the given node.
tree.get_parent('b')

Output: 'a'

  • get_children()

Returns the list of childeren of the given node.

  • parameter node: The name of the node.
  • returns: The list of children for the node.
tree.get_children('a')

Output: ['b', 'c', 'd']

  • get_path()

Returns The path from root node to the given node.

  • parameter node: The name of the node.
  • return: The list of path from root node to the given node.
tree.get_path('i')

Output: ['a', 'd', 'h', 'i']

  • get_depth()

Returns the depth at where the node is located in the tree.

  • parameter node: The name of the node.
  • return: The depth at which the node is located in the tree.
tree.get_depth('g')

Output: 2

  • delete()

Deletes the node and the tree to the down of the node.

  • parameter node: The name of the node.
tree.delete('d')

Because we deleted node 'd' all the nodes below it will also get deleted so the nodes ['d', 'g', 'h', 'i', 'j'] will be deleted.


Mail me

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

treeds-0.0.1.tar.gz (3.1 kB view details)

Uploaded Source

File details

Details for the file treeds-0.0.1.tar.gz.

File metadata

  • Download URL: treeds-0.0.1.tar.gz
  • Upload date:
  • Size: 3.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.2 importlib_metadata/3.10.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.0 CPython/3.7.0

File hashes

Hashes for treeds-0.0.1.tar.gz
Algorithm Hash digest
SHA256 fa38e2ccb67711cc92a885d665fbf12796876f5a8c498a22c60792392c795a60
MD5 18d613e3db83e2407f34a34ba3748b67
BLAKE2b-256 88ea8efe0c65d7c4fa1f4d7201bedd59d9b9d8a15af2caf4e8efe3bce4c6c414

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page