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 hashes)

Uploaded Source

Supported by

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