# ARYAN_GAUTAM'S_BLOG 🚀 # Tree Data Structure - Explanation And Implementation

## Binary Trees

A tree data structure is a hierarchical structure that is used to represent and organize data in a way that is easy to navigate and search. It is a collection of nodes that are connected by edges and has a hierarchical relationship between the nodes.

### Representation of Tree Data Structure --> Tree is considered a non-linear data structure. The data in a tree are not stored in a sequential manner i.e, they are not stored linearly. Instead, they are arranged on multiple levels or we can say it is a hierarchical structure. For this reason, the tree is considered to be a non-linear data structure.

### BINARY TREE -->

Binary Tree is defined as a tree data structure where each node has at most 2 children. Since each element in a binary tree can have only 2 children, we typically name them the left and right child. ### BINARY TREE REPRESENTATION -->

A Binary tree is represented by a pointer to the topmost node (commonly known as the “root”) of the tree. If the tree is empty, then the value of the root is NULL. Each node of a Binary Tree contains the following parts :

1. Data

2. Pointer to left child

3. Pointer to right child

### IMPLEMENTATION OF TREE(BINARY TREE) IN PYTHON ->

We create a tree data structure in python by using the concept of node discussed earlier. We designate one node as root node and then add more nodes as child nodes.

### CREATING ROOT -->

We just create a Node class and add assign a value to the node. This becomes tree with only a root node. When the above code executes it gives the result as shown:

25

### INSERTING INTO TREE -->

To insert into a tree we use the same node class created above and add an insert method to it. Here we are creating a Binary Search Tree.

A binary search tree is a rooted binary tree in which the nodes are arranged in strict total order in which the nodes with keys greater than any particular node is stored on the right sub-trees and the ones with equal to or less than are stored on the left sub-tree satisfying the binary search property. The insert method compares the value of the node to the parent node and decides to add it as a left node or a right node. Finally, the printTree method is used to print the tree.

The code when executes gives the result --> The output result is given as :

[5,12,45,58,100]

In this way we can implement a Tree Data structure in Python.