Posts

Introduction of binary tree in data structure

Image
Introduction To Binary Trees A binary tree is a hierarchical data structure in which each node has at most two children generally referred as left child and right child. Each node contains three components: Pointer to left subtree Pointer to right subtree Data element The topmost node in the tree is called the root. An empty tree is represented by  NULL  pointer. A representation of binary tree is shown: Common Terminologies Root:  Topmost node in a tree. Parent:  Every node (excluding a root) in a tree is connected by a directed edge from exactly one other node. This node is called a parent. Child:  A node directly connected to another node when moving away from the root. Leaf/External node:  Node with no children. Internal node:  Node with atleast one children. Depth of a node:  Number of edges from root to the node. Height of a node:  Number of edges from the node to the deepest leaf. Height of the tree is the height of the root. In the above binary

Implementation of linked list

How to traverse a linked list Displaying the contents of a linked list is very simple. We keep moving the temp node to the next one and display its contents. When temp is  NULL , we know that we have reached the end of linked list so we get out of the while loop. struct node * temp = head ; printf ( "\n\nList elements are - \n" ); while ( temp != NULL ) { printf ( "%d --->" , temp -> data ); temp = temp -> next ; } The output of this program will be: List elements are - 1 --->2 --->3 ---> How to add elements to linked list You can add elements to either beginning, middle or end of linked list. Add to beginning Allocate memory for new node Store data Change next of new node to point to head Change head to point to recently created node struct node * newNode ; newNode = malloc ( sizeof ( struct node )); newNode -> data = 4 ; newNode -> next = head ; head = newNod