Skip to main content

Posts

Showing posts with the label c

Binary heap and heap sorting

Heap Heap is a special type binary tree where every node P is larger than its parent nodes. This is called max heap. In min heap, every node is smaller than its parent In case of max heap, the largest element is always available at the root of the tree. In min heap, the smallest element is at the root of the tree. A heap which is a complete binary tree is called binary heap. A complete binary tree is the one where each level of the tree is completely filled except for last level, which is filled from left to right. Array implementation of binary heap Because binary heap is a complete binary tree, it will be efficient store its elements in an array.  Such an array will have index of  parent and its children as follows. For every node with index i, the left child is at index 2i+1 and right child is at index 2i+2 In the diagram, a binary heap implemented using an array is shown. The indices are written next to each node. Heapify The process of rearranging the elements to maintain...

Binary tree deletion - non-recursive

In the previous post we have seen how to delete a node of a binary search tree using recursion. Today we will see how to delete a node of BST using a non-recursive function. Let us revisit the 3 scenarios here Deleting a node with no children just link the parent to NULL Deleting a node with one child link the parent to  non-null child of node to be deleted Deleting a node with both children select the successor of node to be deleted copy successor's value into this node delete the successor In order to start, we need a function to search for a node in binary search tree. Did you know that searching in  a BST is very fast, and is of the order O(logn). To search Start with root Repeat until value is found or node is NULL If the search value is greater than node branch to right If the search value is lesser than node branch to left.  Here is the function NODEPTR find_node (NODEPTR root,NODEPTR * parent, int delval) { NODEPTR nd = root; NODEPTR pa = root; if (ro...

Binary tree deletion

Do not get all scared and worried. It is not rocket science (or as I would like to call it - it is not regex). Remember deleting a node from linked list. When you deleted a node, you did the following 2 steps Free the memory of the node Link the previous node of the deleted node to the next node You will have to do these things in binary tree too. But the difficulty here is do you link the previous node - or parent node in tree terminology, to the left child of deleted node? Or to the right child? You can not link both children, as the parent already may have one child node. So before we solve this, let us categorize the deletion with the help of a diagram. Consider the cases The node to be deleted is a leaf node. That is, it does not have left or right child. In this case, link the parent to NULL in place of deleted node.  If you want to delete 13 - which is a leaf node, you set 14->left to NULL and free memory of node 13. If you want to delete 7 - which is also a leaf node and...

Binary tree traversal in C

In an earlier post , we have seen how to add a new node to a binary tree. Binary Tree Terminology: Tree is a non-linear data structure where every node has multiple branches A binary tree is a tree where each node has maximum 2 branches Each node branching out is called child node  The starting node of the tree is called root The two children of binary tree are left child and right child. The child node along with its branches and sub-branches are called sub-tree.  A node has two sub-trees - left subtree and right subtree A node which has no child nodes and hence no subtrees is called a leaf node Image from : http://msoe.us/taylor Normally when we talk about binary tree, we refer to binary search tree which is ordered tree. In a binary search tree, every node to the right of a given node will have larger value than the given node. And every node to the left of given node will have value smaller than given node. That is to say the left subtree of an...

Function to sort an array using insertion sort

Insertion sort is slightly better sorting method among O(n2) algorithms. It is effecient if the elements are almost sorted and if the size is small. Insertion sort basically works by splitting your elements into two parts. Sorted half and unsorted half. To begin with, Sorted half is empty and unsorted half has all elements. Then one element is removed from unsorted half and added to the sorted half. When adding , this element is inserted at the correct location. This process is continued till we have all the elements in sorted half and unsorted half is empty. Now let us try to understand it with a diagram. First 17 is removed from unsorted array and added to sorted array. Since there is one element there, it is sorted. Next 4 which is front most element now in unsored array is removed and added to sorted array. But 4 is smaller than 17, so 17 is pushed to right and its place is taken by 4.    Next 32 is removed from the front of unsorted array and added to sorted array. But he...

Function to sort an array using bubble sort

Quick and dirty way of sorting an array is bubble sort. It is very easy to write and follow. But please keep in mind that it is not at all effecient. #include<iostream> using std::cin; using std::cout; void readArray(int arr[],int sz); void printArray(int arr[],int sz); void sortArray(int arr[],int sz); void swap(int &a,int &b); int main() {    int sz;    cout<<"Size of the array=";    cin>>sz;    int arr[sz];    readArray(arr,sz);     sortArray(arr,sz);   cout<<"Sorted array is ";   printArray(arr,sz); } void readArray(int arr[],int sz) {  for(int i=0;i<sz;i++)    {       cout<<"arr["<<i<<"]=";       cin>>arr[i];   } } void printArray(int arr[],int sz) {  for(int i=0;i<sz;i++)    {       cout<<"arr["<<i<<"]=";    ...

Program to delete a node from linked list

How do you remove a node from a linked list? If you have to delete a node, first you need to search the node. Then you should find its previous node. Then you should link the previous node to the next node. If node containing 8 has to be deleted, then n1 should be pointing to n2. Looks quite simple. Isn't it? But there are at least two special cases you have to consider. Of course, when the node is not found. If the node is first node of the list viz head. If the node to be deleted is head node, then if you delete, the list would be lost. You should avoid that and make the second node as the head node. So it becomes mandatory that you return the changed head node from the function.   Now let us have a look at the code. #include<stdio.h> #include<stdlib.h> struct node { int data; struct node * next; }; typedef struct node * NODEPTR; NODEPTR create_node ( int value) { NODEPTR temp = (NODEPTR) malloc( size...