Home » leet code » 100 Days Challenge Leetcode » Binary Trees – Data structure – Nilesh blog.tech

Binary Trees – Data structure – Nilesh blog.tech

What are Binary Trees?

A binary tree is a tree data structure in which each node has at most two children. The children are referred to as the left child and the right child.Binary trees are commonly used to implement binary search trees and binary heaps. They have several useful properties, such as the ability to search, insert, and delete elements in logarithmic time, as well as the ability to sort data efficiently.

In a binary tree, the root node is the topmost node, and the leaf nodes are the nodes at the bottom of the tree that have no children. Every node in a binary tree has a left child and a right child, with the exception of the leaf nodes, which have no children.Binary trees have a number of important properties that make them useful in various applications.

For example, they are well-suited for searching, since it is relatively easy to traverse the tree and locate a particular element. They are also relatively efficient for inserting and deleting elements, since these operations only require a small number of comparisons and rearrangements of the tree.

binary trees are commonly used for ?

Binary trees are commonly used for a variety of purposes, including:

  1. Binary search trees: These are used to store data in a way that allows for efficient search and insertion operations.
  2. Binary heaps: These are used to implement priority queues, where elements with higher priorities are given precedence over elements with lower priorities.
  3. Expression trees: These are used to represent expressions in a tree-like format, with the operators serving as the internal nodes and the operands serving as the leaf nodes.
  4. Decision trees: These are used in machine learning and artificial intelligence to make decisions based on certain conditions.
  5. Huffman trees: These are used in data compression to represent the frequencies of different characters in a data stream.
  6. Syntax trees: These are used in natural language processing and computer science to represent the structure of a sentence or other piece of text.
  7. Trie trees: These are used to store large sets of words or strings in a way that allows for efficient search and insertion operations.
  8. Graphs: Trees are a type of graph, and they can be used to represent relationships or connections between different entities.
  9. File systems: Some computer file systems use a tree-like structure to organize and store files.
  10. Network routing: Trees can be used to represent network topologies and to find the shortest path between two nodes in a network

binary tree traversal data Structures

binary tree traversal ?Binary tree traversal refers to the process of visiting (checking and/or updating) each node in a binary tree. There are three common ways to traverse a binary tree: in-order, pre-order, and post-order.

In-order traversal: In this traversal method, the left subtree is visited first, then the root, and finally the right subtree.

Pre-order traversal: In this traversal method, the root is visited first, then the left subtree, and finally the right subtree.

Post-order traversal: In this traversal method, the left subtree is visited first, then the right subtree, and finally the root.

These traversal methods are implemented using recursive algorithms, which involve traversing the left and right subtrees before returning to the root. They can also be implemented using iterative algorithms, which use a stack to keep track of the nodes that need to be visited.Binary tree traversal is often used to perform a specific task on each node in the tree, such as searching for a particular value, printing the nodes in a certain order, or calculating the sum of all the nodes in the tree. It can also be used to create a copy of the tree, or to check the tree for certain properties (such as being a binary search tree).

Write pseudo code / Program for binary tree?

Here is some pseudo code for creating and traversing a binary tree:

class Node:  
def __init__(self, data):    
self.data = data    
self.left = None    
self.right = None
class BinaryTree:  
def __init__(self):    
self.root = None    
def insert(self, data):   
 new_node = Node(data)    
if self.root is None:      
self.root = new_node   
current = self.root      
while current:       
 if data < current.data:          
if current.left is None:           
 current.left = new_node           
current = current.left        
 if current.right is None:            
current.right = new_node            
break          else:            current = current.right    def in_order_traversal(self, node):    if node is not None:      self.in_order_traversal(node.left)      print(node.data)      self.in_order_traversal(node.right)    def pre_order_traversal(self, node):    if node is not None:      print(node.data)      self.pre_order_traversal(node.left)      self.pre_order_traversal(node.right)    def post_order_traversal(self, node):    if node is not None:      self.post_order_traversal(node.left)      self.post_order_traversal(node.right)      print(node.data)tree = BinaryTree()tree.insert(5)tree.insert(3)tree.insert(7)tree.insert(2)tree.insert(4)tree.insert(6)tree.insert(8)print("In-order traversal:")tree.in_order_traversal(tree.root)print("Pre-order traversal:")tree.pre_order_traversal(tree.root)print("Post-order traversal:")tree.post_order_traversal(tree.root)

This code defines a Node class, which represents a single node in a binary tree, and a BinaryTree class, which represents an entire binary tree. The BinaryTree class has methods for inserting nodes, and for performing in-order, pre-order, and post-order traversals of the tree.

The insert method adds a new node to the tree, while the traversal methods (in_order_traversal, pre_order_traversal, and post_order_traversal) print the data of each node in the tree according to the specified traversal method.

This is just one way to implement a binary tree and its traversal methods. The specific details of the implementation may vary depending on the specific requirements of the application

Leave a Reply