Traversal binary min heap array

Here given code implementation process.

/*
  C Program 
  Traversal Binary min heap array
*/
#include <stdio.h>

void preorder(int node[],int size,int root)
{

  if(root<size)
  {
    printf("%3d",node[root] );
    preorder(node, size, 2*root+1);

    preorder(node, size, 2*root+2);
  }

}
void inorder(int node[],int size,int root)
{

  if(root<size)
  {
    // (2*root+1) indicating the left child
    inorder(node, size, 2*root+1);
    printf("%3d",node[root] );

    // (2*root+2) indicating the right child
    inorder(node, size, 2*root+2);
  }

}
void postorder(int node[],int size,int root)
{

  if(root<size)
  {
    postorder(node, size, 2*root+1);

    postorder(node, size, 2*root+2);

    printf("%3d",node[root] );
  }

}
int main(){

  /* Min heap
  -----------------------
              1
           /      \
          5        6
         /   \    / \
        9     7  8   10
       / \   /    
      13 12 11    
           
  */
  //min heap array
  int node[]={1,5,6,9,7,8,10,13,12,11};
  
  //get the size of array elements
  int size = sizeof(node)/sizeof(node[0]);
  
  printf("Preorder\n");
  preorder(node,size,0); 
  printf("\nInorder\n");
  inorder(node,size,0); 
  printf("\nPostorder\n");
  postorder(node,size,0); 
  return 0;
}

Output

Preorder
  1  5  9 13 12  7 11  6  8 10
Inorder
 13  9 12  5 11  7  1  8  6 10
Postorder
 13 12  9 11  7  5  8 10  6  1
/*
 C++ Program
 Traversal binary min heap array
*/

#include<iostream>

using namespace std;
class MinHeap {
  public:
    void preorder(int node[], int size, int root) {
      if (root < size) {
        cout << "  " << node[root];
        this->preorder(node, size, 2 *root + 1);
        this->preorder(node, size, 2 *root + 2);
      }
    }
  void inorder(int node[], int size, int root) {
    if (root < size) {
      this->inorder(node, size, 2 *root + 1);
      cout << "  " << node[root];
      this->inorder(node, size, 2 *root + 2);
    }
  }
  void postorder(int node[], int size, int root) {
    if (root < size) {
      this->postorder(node, size, 2 *root + 1);
      this->postorder(node, size, 2 *root + 2);
      cout << "  " << node[root];
    }
  }
};
int main() {
  MinHeap obj;
  /* Min heap
  -----------------------
              1
           /      \
          5        6
         /   \    / \
        9     7  8   10
       / \   /    
      13 12 11    
           
  */
  int arr[] = {1, 5, 6, 9, 7, 8, 10, 13, 12, 11};
  int size = sizeof(arr)/sizeof(arr[0]);
  cout << "Preorder\n";
  obj.preorder(arr, size, 0);
  cout << "\nInorder\n";
  obj.inorder(arr, size, 0);
  cout << "\nPostorder\n";
  obj.postorder(arr, size, 0);
  return 0;
}

Output

Preorder
  1  5  9 13 12  7 11  6  8 10
Inorder
 13  9 12  5 11  7  1  8  6 10
Postorder
 13 12  9 11  7  5  8 10  6  1
/*
  Java Program 
  Traversal binary min heap array
*/
public class MinHeap {
  
  public void preorder(int []node,int size,int root)
  {

    if(root<size)
    {
      System.out.print("  "+node[root] );
      preorder(node, size, 2*root+1);

      preorder(node, size, 2*root+2);
    }

  }
  public void inorder(int []node,int size,int root)
  {

    if(root<size)
    {
      inorder(node, size, 2*root+1);
      System.out.print("  "+node[root] );
      inorder(node, size, 2*root+2);
    }

  }
  public void postorder(int []node,int size,int root)
  {

    if(root<size)
    {
      postorder(node, size, 2*root+1);

      postorder(node, size, 2*root+2);

      System.out.print("  "+node[root] );
    }

  }

  public static void main(String[] args) {

    MinHeap obj = new MinHeap();
    /* Min heap
    -----------------------
                1
             /      \
            5        6
           /   \    / \
          9     7  8   10
         / \   /    
        13 12 11    
             
    */
    //Min heap array
    int[] arr = {1,5,6,9,7,8,10,13,12,11};
   
    int size = arr.length;


    System.out.print("Preorder\n");
    obj.preorder(arr,size,0); 
    System.out.print("\nInorder\n");
    obj.inorder(arr,size,0); 
    System.out.print("\nPostorder\n");
    obj.postorder(arr,size,0); 

  }
}

Output

Preorder
  1  5  9 13 12  7 11  6  8 10
Inorder
 13  9 12  5 11  7  1  8  6 10
Postorder
 13 12  9 11  7  5  8 10  6  1
/*
  C# Program 
  Traversal binary min heap array
*/
using System;
public class MinHeap {

	public void preorder(int []node,int size,int root)
	{

		if(root<size)
		{
			Console.Write("  "+node[root] );
			preorder(node, size, 2*root+1);

			preorder(node, size, 2*root+2);
		}

	}
	public void inorder(int []node,int size,int root)
	{

		if(root<size)
		{
			inorder(node, size, 2*root+1);
			Console.Write("  "+node[root] );
			inorder(node, size, 2*root+2);
		}

	}
	public void postorder(int []node,int size,int root)
	{

		if(root<size)
		{
			postorder(node, size, 2*root+1);

			postorder(node, size, 2*root+2);

			Console.Write("  "+node[root] );
		}

	}

	public static void Main(String[] args) {

		MinHeap obj = new MinHeap();
		/* Min heap
    -----------------------
                1
             /      \
            5        6
           /   \    / \
          9     7  8   10
         / \   /    
        13 12 11    
             
    */
		//Min heap array
		int[] arr = {1,5,6,9,7,8,10,13,12,11};

		int size = arr.Length;


		Console.Write("Preorder\n");
		obj.preorder(arr,size,0); 
		Console.Write("\nInorder\n");
		obj.inorder(arr,size,0); 
		Console.Write("\nPostorder\n");
		obj.postorder(arr,size,0); 

	}
}

Output

Preorder
  1  5  9 13 12  7 11  6  8 10
Inorder
 13  9 12  5 11  7  1  8  6 10
Postorder
 13 12  9 11  7  5  8 10  6  1
# Python 3 Program
# Traversal binary min heap array

class MinHeap :
  def preorder(self, node, size, root) :
    if (root < size) :
      print(" ", node[root],end="")
      self.preorder(node, size, 2 * root + 1)
      self.preorder(node, size, 2 * root + 2)
    
  
  def inorder(self, node, size, root) :
    if (root < size) :
      self.inorder(node, size, 2 * root + 1)
      print(" ", node[root],end="")
      self.inorder(node, size, 2 * root + 2)
    
  
  def postorder(self, node, size, root) :
    if (root < size) :
      self.postorder(node, size, 2 * root + 1)
      self.postorder(node, size, 2 * root + 2)
      print(" ", node[root],end="")
    
  

def main() :
  obj = MinHeap()

  # Min heap
  #
  #              1
  #           /      \
  #          5        6
  #         /   \    / \
  #        9     7  8   10
  #       / \   /
  #      13 12 11
  #  
  arr = [1, 5, 6, 9, 7, 8, 10, 13, 12, 11]
  size = len(arr)
  print("Preorder")
  obj.preorder(arr, size, 0)
  print("\nInorder")
  obj.inorder(arr, size, 0)
  print("\nPostorder")
  obj.postorder(arr, size, 0)

if __name__ == "__main__":
  main()

Output

Preorder
  1  5  9 13 12  7 11  6  8 10
Inorder
 13  9 12  5 11  7  1  8  6 10
Postorder
 13 12  9 11  7  5  8 10  6  1
# Ruby Program
# Traversal binary min heap array

class MinHeap 
	def preorder(node, size, root) 
		if (root < size) 
			print("  ", node[root])
			self.preorder(node, size, 2 * root + 1)
			self.preorder(node, size, 2 * root + 2)
		end
	end
	def inorder(node, size, root) 
		if (root < size) 
			self.inorder(node, size, 2 * root + 1)
			print("  ", node[root])
			self.inorder(node, size, 2 * root + 2)
		end
	end
	def postorder(node, size, root) 
		if (root < size) 
			self.postorder(node, size, 2 * root + 1)
			self.postorder(node, size, 2 * root + 2)
			print("  ", node[root])
		end
	end
end
def main() 
	obj = MinHeap.new()

	# Min heap
	#
	#              1
	#           /      \
	#          5        6
	#         /   \    / \
	#        9     7  8   10
	#       / \   /
	#      13 12 11
	#  
	arr = [1, 5, 6, 9, 7, 8, 10, 13, 12, 11]
	size = arr.length
	print("Preorder\n")
	obj.preorder(arr, size, 0)
	print("\nInorder\n")
	obj.inorder(arr, size, 0)
	print("\nPostorder\n")
	obj.postorder(arr, size, 0)
end
main()

Output

Preorder
  1  5  9 13 12  7 11  6  8 10
Inorder
 13  9 12  5 11  7  1  8  6 10
Postorder
 13 12  9 11  7  5  8 10  6  1
<?php
/*
 Php Program
 Traversal binary min heap array
*/

class MinHeap {
  public  function preorder($node, $size, $root) {
    if ($root < $size) {
      echo("  ". $node[$root]);
      $this->preorder($node, $size, 2 *$root + 1);
      $this->preorder($node, $size, 2 *$root + 2);
    }
  }
  public  function inorder($node, $size, $root) {
    if ($root < $size) {
      $this->inorder($node, $size, 2 *$root + 1);
      echo("  ". $node[$root]);
      $this->inorder($node, $size, 2 *$root + 2);
    }
  }
  public  function postorder($node, $size, $root) {
    if ($root < $size) {
      $this->postorder($node, $size, 2 *$root + 1);
      $this->postorder($node, $size, 2 *$root + 2);
      echo("  ". $node[$root]);
    }
  }
}

function main() {
  $obj = new MinHeap();
  /* Min heap
  -----------------------
              1
           /      \
          5        6
         /   \    / \
        9     7  8   10
       / \   /    
      13 12 11    
           
  */
  $arr = array(1, 5, 6, 9, 7, 8, 10, 13, 12, 11);
  $size = count($arr);
  echo("Preorder\n");
  $obj->preorder($arr, $size, 0);
  echo("\nInorder\n");
  $obj->inorder($arr, $size, 0);
  echo("\nPostorder\n");
  $obj->postorder($arr, $size, 0);
}
main();

Output

Preorder
  1  5  9 13 12  7 11  6  8 10
Inorder
 13  9 12  5 11  7  1  8  6 10
Postorder
 13 12  9 11  7  5  8 10  6  1
/*
 Node Js Program
 Traversal binary min heap array
*/

class MinHeap {
	preorder(node, size, root) {
		if (root < size) {
			process.stdout.write("  " + node[root]);
			this.preorder(node, size, 2 *root + 1);
			this.preorder(node, size, 2 *root + 2);
		}
	}
	inorder(node, size, root) {
		if (root < size) {
			this.inorder(node, size, 2 *root + 1);
			process.stdout.write("  " + node[root]);
			this.inorder(node, size, 2 *root + 2);
		}
	}
	postorder(node, size, root) {
		if (root < size) {
			this.postorder(node, size, 2 *root + 1);
			this.postorder(node, size, 2 *root + 2);
			process.stdout.write("  " + node[root]);
		}
	}
}

function main() {
	var obj = new MinHeap();
	/* Min heap
    ---------------------
              1
           /      \
          5        6
         /   \    / \
        9     7  8   10
       / \   /    
      13 12 11    
           
    */
	var arr = [1, 5, 6, 9, 7, 8, 10, 13, 12, 11];
	var size = arr.length;
	process.stdout.write("Preorder\n");
	obj.preorder(arr, size, 0);
	process.stdout.write("\nInorder\n");
	obj.inorder(arr, size, 0);
	process.stdout.write("\nPostorder\n");
	obj.postorder(arr, size, 0);
}

main();

Output

Preorder
  1  5  9 13 12  7 11  6  8 10
Inorder
 13  9 12  5 11  7  1  8  6 10
Postorder
 13 12  9 11  7  5  8 10  6  1
/*
 Swift 4 Program
 Traversal binary min heap array
*/

class MinHeap {
  func preorder(_ node: [Int] , _ size : Int, _ root: Int) {
    if (root < size) {
      print(" ", node[root],terminator:"");
      self.preorder(node, size, 2 * root + 1);
      self.preorder(node, size, 2 * root + 2);
    }
  }
  func inorder(_ node: [Int] , _ size : Int, _ root: Int) {
    if (root < size) {
      self.inorder(node, size, 2 * root + 1);
      print(" ", node[root],terminator:"");
      self.inorder(node, size, 2 * root + 2);
    }
  }
  func postorder(_ node: [Int] , _ size : Int, _ root: Int) {
    if (root < size) {
      self.postorder(node, size, 2 * root + 1);
      self.postorder(node, size, 2 * root + 2);
      print(" ", node[root],terminator:"");
    }
  }
}
func main() {
  let obj: MinHeap = MinHeap();
  /* Min heap
  -----------------------
              1
           /      \
          5        6
         /   \    / \
        9     7  8   10
       / \   /    
      13 12 11    
           
  */
  let arr: [Int] = [1, 5, 6, 9, 7, 8, 10, 13, 12, 11];
  let size: Int = arr.count;
  print("Preorder");
  obj.preorder(arr, size, 0);
  print("\nInorder");
  obj.inorder(arr, size, 0);
  print("\nPostorder");
  obj.postorder(arr, size, 0);
}
main();

Output

Preorder
  1  5  9 13 12  7 11  6  8 10
Inorder
 13  9 12  5 11  7  1  8  6 10
Postorder
 13 12  9 11  7  5  8 10  6  1


Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.

New Comment







© 2021, kalkicode.com, All rights reserved