What is Tree? it is an user-defined non-liner data structure. Tree is one of the user-defined data structures. You may ask about others. Briefly those are Stack and Queue. Stack follows the LIFO principle and allows the addition and deletion of elements from only one end known as Top. Queue follows the FIFO principle and allows the addition and deletion of elements from both ends known as Front and Rear.
Python tree questions
1.What is tree?
Tree is an user-defined data structure
2. How the data structure looks like?
A Tree is a collection of nodes, each node consists of values and reference of nodes (known as child nodes).
3. How can we access nodes in a Tree?
All nodes in a tree are accessible through the root node of the tree.
4. Are trees mutable?
Trees are mutable.
5. Can we add or delete child (leaf) nodes?
Yes, you can add or delete child nodes.
6. Write code to create a node?
class Node():
def __init__(self,val):
self.val = val
self.child = []
def add_child(self,node):
self.child.append(node)
The Node class has two methods. The init and add_child are two methods. You know that init method is a constructor, and it executes when you call a class (Here is Node) default.
The child nodes you are maintaining in a list. That’s you can see in the init method. The add_child methods takes input and create a list of child nodes.
7. How to add child nodes?
Below is the logic to add child nodes.
class Node():
def __init__(self,val):
self.val = val
self.child = []
def add_child(self, node):
self.child.append(node)
#step1
root = Node(1)
print(root)
#step2
print(root.child)
#step3
child1 = Node(2)
child2 = Node(3)
root.add_child(child1)
root.add_child(child2)
print (root.child)
#step4
for c in root.child:
print(c.val)
print(root.val)
Output
The output generated from the code that we have written.
<__main__.Node object at 0x7f7cb279dee0>
[]
[<__main__.Node object at 0x7f7cb279df70>, <__main__.Node object at 0x7f7cb27c9400>]
2
3
1
** Process exited - Return Code: 0 **
Press Enter to exit terminal
8. What is the python library that is available to implement trees?
Anytree
9. What are the key points to remember?
- Tree is a user-defined data structure that can be implemented using a list or anytree library of Python.
- N-ary tree node can have up to n child nodes.
- Majorly used trees are binary trees and binary search trees.
- Binary and Binary Search trees can be implemented using the
binarytree
library available for Python. - Binary trees and BST nodes can have 0 to 2 child nodes.
- Parent node in BST must have a value greater than the left child node and less value than the right child node.
- Trees have major use case in hierarchical data like file system, syntax tree in compilation, and so on.
10. What is BST (Binary search tree)?
The Binary search tree (BST) is a special type of binary tree. A tree is known as BST if it fulfills the following properties:
- Each node has maximum two child nodes.
- Left child has value less than parent node.
- Right child has value more than parent node
References
Shopping