Convert a binary tree to Max Heap

Convert binary tree to Max Heap

Here given code implementation process.

/*
  C Program 
+ Convert a binary tree to Max Heap
*/
#include <stdio.h>
#include <stdlib.h>
//structure of Binary Tree node
struct Node{
  int data;
  struct Node*left,*right;
};
//Create a binary tree nodes and node fields (data,pointer) 
//And returning the reference of newly nodes

struct Node* insert(int data)
{
  //create dynamic memory to new binary tree node
  struct Node*new_node=(struct Node*)malloc(sizeof(struct Node));
  if(new_node!=NULL)
  {
    //set data and pointer values
    new_node->data=data;
    new_node->left=NULL; //Initially node left-pointer is NULL
    new_node->right=NULL;//Initially node right-pointer is NULL
  }else
  {
    printf("Memory Overflow\n");
    exit(0); //Terminate program execution
  }
  //return reference
  return new_node;
  
}
//Display tree element inorder form
void inorderData(struct Node* node)
{

  if(node!=NULL)
  {

    inorderData(node->left);
    //Print node value
    printf("%3d",node->data);
    inorderData(node->right);
  }
}
//Display tree element preorder form
void preOrderData(struct Node* node)
{

  if(node!=NULL)
  {
       //Print node value
    printf("%3d",node->data);
    preOrderData(node->left);
    
    preOrderData(node->right);
  }
}
//Display tree element postorder form
void postOrderData(struct Node* node)
{

  if(node!=NULL)
  {

    postOrderData(node->left);
    
    postOrderData(node->right);
      //Print node value
    printf("%3d",node->data);
  }
}
void swap(struct Node*first,struct Node*second)
{
  int temp=first->data;
  first->data=second->data;
  second->data=temp;
}

//convert a binary tree to Max Heap
void maxHeap(struct Node*root)
{
  if(root==NULL) return;



  maxHeap(root->left);

  maxHeap(root->right);
  
  if(root->left != NULL && root->left->data > root->data)
  {
    //swap node value
    swap(root,root->left);
    //check min head after swap
    maxHeap(root); 
  }
  if(root->right != NULL && root->right->data > root->data)
  {
    //swap node value
    swap(root,root->right);
    //check min head after swap
    maxHeap(root); 
  }
}
int main(){

  struct Node*root=NULL;
  /*  Make A Binary Tree
  -----------------------
          5
         /  \
        4    7
       /    /  \
      3    6    10
       \    \
        9    8
  */


  //Insertion of binary tree nodes
  root               =insert(5);
  root->left         =insert(4);
  root->right        =insert(7);
  root->right->right =insert(10);
  root->right->left  =insert(6);
  root->left->left   =insert(3);
  root->right->left->right  =insert(8);
  root->left->left->right   =insert(9);
  //Display Tree elements
  printf("\n Before convert");
  printf("\n Inorder Data : ");
  inorderData(root);

  printf("\n Preorder Data : ");
  preOrderData(root);

  printf("\n Postorder Data : ");
  postOrderData(root);

  //Convert
  maxHeap(root);

    /* After convert
  -----------------------
          10
         /  \
        5    9
       /    /  \
      4    7    8
       \    \
        3    6
  */
  printf("\n\n After convert");
  printf("\n Inorder Data : ");
  inorderData(root);

  printf("\n Preorder Data : ");
  preOrderData(root);

  printf("\n Postorder Data : ");
  postOrderData(root);
  return 0;
}

Output

 Before convert
 Inorder Data :   3  9  4  5  6  8  7 10
 Preorder Data :   5  4  3  9  7  6  8 10
 Postorder Data :   9  3  4  8  6 10  7  5

 After convert
 Inorder Data :   4  3  5 10  7  6  9  8
 Preorder Data :  10  5  4  3  9  7  6  8
 Postorder Data :   3  4  5  6  7  8  9 10
/*
  C++ Program
  Convert a binary tree to Max Heap
*/
#include<iostream>

using namespace std;
class Node {
public:
  int data;
  Node *left, *right;
  Node(int value) {
    this->data = value;
    this->left = this->right = NULL;
  }
};
class BinaryTree {
  public:
  Node *root;
  BinaryTree() {
    this->root = NULL;
  }
  void in_order(Node *node) {
    if (node != NULL) {
      this->in_order(node->left);
      cout << "  " << node->data;
      this->in_order(node->right);
    }
  }
  void pre_order(Node *node) {
    if (node != NULL) {
      cout << "  " << node->data;
      this->pre_order(node->left);
      this->pre_order(node->right);
    }
  }
  void post_order(Node *node) {
    if (node != NULL) {
      this->post_order(node->left);
      this->post_order(node->right);
      cout << "  " << node->data;
    }
  }
  void swap(Node *first, Node *second) {
    int value = first->data;
    first->data = second->data;
    second->data = value;
  }
  void maxHeap(Node *head) {
    if (head == NULL)
      return;
    this->maxHeap(head->left);
    this->maxHeap(head->right);
    if (head->left != NULL && head->left->data > head->data) {
      this->swap(head, head->left);
      this->maxHeap(head);
    }
    if (head->right != NULL && head->right->data > head->data) {
      this->swap(head, head->right);
      this->maxHeap(head);
    }
  }
};

int main() {
  BinaryTree obj;
  /*  Make Binary Tree
  -----------------------
          5
         /  \
        4    7
       /    /  \
      3    6    10
       \    \
        9    8
  */

  obj.root = new Node(5);
  obj.root->left = new Node(4);
  obj.root->right = new Node(7);
  obj.root->right->right = new Node(10);
  obj.root->right->left = new Node(6);
  obj.root->left->left = new Node(3);
  obj.root->right->left->right = new Node(8);
  obj.root->left->left->right = new Node(9);
  cout << ("\nBefore Convert ");
  cout << ("\nIn-order Data : ");
  obj.in_order(obj.root);
  cout << ("\nPre-order Data : ");
  obj.pre_order(obj.root);
  cout << ("\nPost-order Data : ");
  obj.post_order(obj.root);
  obj.maxHeap(obj.root);
  cout << ("\nAfter Convert ");
  cout << ("\nIn-order Data : ");
  obj.in_order(obj.root);
  cout << ("\nPre-order Data : ");
  obj.pre_order(obj.root);
  cout << ("\nPost-order Data : ");
  obj.post_order(obj.root);
  return 0;
}

Output

Before Convert 
In-order Data :   3  9  4  5  6  8  7  10
Pre-order Data :   5  4  3  9  7  6  8  10
Post-order Data :   9  3  4  8  6  10  7  5
After Convert 
In-order Data :   4  3  5  10  7  6  9  8
Pre-order Data :   10  5  4  3  9  7  6  8
Post-order Data :   3  4  5  6  7  8  9  10
/*
Java Program 
Convert a binary tree to Max Heap
*/

//Class of Binary Tree node
class Node {

  public int data;
  public Node left, right;
  //Make a tree node
  public Node(int value) {
    //Assign field values
    data = value;
    left = right = null;
  }
}


public class BinaryTree {

  public Node root;


  public BinaryTree() {
    //Set initial value
    root = null;


  }
  //Display tree element inorder form
  public void in_order(Node node) {

    if (node != null) {

      in_order(node.left);
      //Print node value
      System.out.print("  " + node.data);
      in_order(node.right);
    }
  }
  //Display tree element preorder form
  public void pre_order(Node node) {

    if (node != null) {
      //Print node value
      System.out.print("  " + node.data);
      pre_order(node.left);

      pre_order(node.right);
    }
  }
  //Display tree element preorder form
  public void post_order(Node node) {

    if (node != null) {

      post_order(node.left);

      post_order(node.right);
      //Print node value
      System.out.print("  " + node.data);
    }
  }
  public void swap(Node first, Node second) {
    int value = first.data;
    first.data = second.data;
    second.data = value;
  }

  //convert a binary tree to Max Heap
  public void maxHeap(Node head) {
    if (head == null) return;



    maxHeap(head.left);

    maxHeap(head.right);

    if (head.left != null && head.left.data > head.data) {
      //swap node value
      swap(head, head.left);
      //check min head after swap
      maxHeap(head);
    }
    if (head.right != null && head.right.data > head.data) {
      //swap node value
      swap(head, head.right);
      //check min head after swap
      maxHeap(head);
    }
  }
  public static void main(String[] args) {
    //Make object of Binary Tree
    BinaryTree obj = new BinaryTree();
    /* Make A Binary Tree
      -----------------------
              5
             /  \
            4    7
           /    /  \
          3    6    10
           \    \
            9    8
      */


    //binary tree nodes
    obj.root = new Node(5);
    obj.root.left = new Node(4);
    obj.root.right = new Node(7);
    obj.root.right.right = new Node(10);
    obj.root.right.left = new Node(6);
    obj.root.left.left = new Node(3);
    obj.root.right.left.right = new Node(8);
    obj.root.left.left.right = new Node(9);
    System.out.print("\nBefore Convert ");
    System.out.print("\nIn-order Data : ");
    obj.in_order(obj.root);

    System.out.print("\nPre-order Data : ");
    obj.pre_order(obj.root);

    System.out.print("\nPost-order Data : ");
    obj.post_order(obj.root);


    //Convert
    obj.maxHeap(obj.root);

    /*After convert
    -----------------------
            10
           /  \
          5    9
         /    /  \
        4    7    8
         \    \
          3    6
    */
    System.out.print("\nAfter Convert ");
    System.out.print("\nIn-order Data : ");
    obj.in_order(obj.root);

    System.out.print("\nPre-order Data : ");
    obj.pre_order(obj.root);

    System.out.print("\nPost-order Data : ");
    obj.post_order(obj.root);
  }
}

Output

Before Convert 
In-order Data :   3  9  4  5  6  8  7  10
Pre-order Data :   5  4  3  9  7  6  8  10
Post-order Data :   9  3  4  8  6  10  7  5
After Convert 
In-order Data :   4  3  5  10  7  6  9  8
Pre-order Data :   10  5  4  3  9  7  6  8
Post-order Data :   3  4  5  6  7  8  9  10
/*
C# Program 
Convert a binary tree to Max Heap
*/
using System;
//Class of Binary Tree node
public class Node {

	public int data;
	public Node left, right;
	//Make a tree node
	public Node(int value) {
		//Assign field values
		data = value;
		left = right = null;
	}
}


public class BinaryTree {

	public Node root;


	public BinaryTree() {
		//Set initial value
		root = null;


	}
	//Display tree element inorder form
	public void in_order(Node node) {

		if (node != null) {

			in_order(node.left);
			//Print node value
			Console.Write("  " + node.data);
			in_order(node.right);
		}
	}
	//Display tree element preorder form
	public void pre_order(Node node) {

		if (node != null) {
			//Print node value
			Console.Write("  " + node.data);
			pre_order(node.left);

			pre_order(node.right);
		}
	}
	//Display tree element preorder form
	public void post_order(Node node) {

		if (node != null) {

			post_order(node.left);

			post_order(node.right);
			//Print node value
			Console.Write("  " + node.data);
		}
	}
	public void swap(Node first, Node second) {
		int value = first.data;
		first.data = second.data;
		second.data = value;
	}

	//convert a binary tree to Max Heap
	public void maxHeap(Node head) {
		if (head == null) return;



		maxHeap(head.left);

		maxHeap(head.right);

		if (head.left != null && head.left.data > head.data) {
			//swap node value
			swap(head, head.left);
			//check min head after swap
			maxHeap(head);
		}
		if (head.right != null && head.right.data > head.data) {
			//swap node value
			swap(head, head.right);
			//check min head after swap
			maxHeap(head);
		}
	}
	public static void Main(String[] args) {
		//Make object of Binary Tree
		BinaryTree obj = new BinaryTree();
		/* Make A Binary Tree
      -----------------------
              5
             /  \
            4    7
           /    /  \
          3    6    10
           \    \
            9    8
      */


		//binary tree nodes
		obj.root = new Node(5);
		obj.root.left = new Node(4);
		obj.root.right = new Node(7);
		obj.root.right.right = new Node(10);
		obj.root.right.left = new Node(6);
		obj.root.left.left = new Node(3);
		obj.root.right.left.right = new Node(8);
		obj.root.left.left.right = new Node(9);
		Console.Write("\nBefore Convert ");
		Console.Write("\nIn-order Data : ");
		obj.in_order(obj.root);

		Console.Write("\nPre-order Data : ");
		obj.pre_order(obj.root);

		Console.Write("\nPost-order Data : ");
		obj.post_order(obj.root);


		//Convert
		obj.maxHeap(obj.root);

		/*After convert
    -----------------------
            10
           /  \
          5    9
         /    /  \
        4    7    8
         \    \
          3    6
    */
		Console.Write("\nAfter Convert ");
		Console.Write("\nIn-order Data : ");
		obj.in_order(obj.root);

		Console.Write("\nPre-order Data : ");
		obj.pre_order(obj.root);

		Console.Write("\nPost-order Data : ");
		obj.post_order(obj.root);
	}
}

Output

Before Convert 
In-order Data :   3  9  4  5  6  8  7  10
Pre-order Data :   5  4  3  9  7  6  8  10
Post-order Data :   9  3  4  8  6  10  7  5
After Convert 
In-order Data :   4  3  5  10  7  6  9  8
Pre-order Data :   10  5  4  3  9  7  6  8
Post-order Data :   3  4  5  6  7  8  9  10
# Python Program 
# Convert a binary tree to Max Heap
class Node :

  def __init__(self, value) :
    self.data = value
    self.left = self.right = None
  

class BinaryTree :

  def __init__(self) :
    self.root = None
  
  def in_order(self, node) :
    if (node != None) :
      self.in_order(node.left)
      print(node.data, end="  ")
      self.in_order(node.right)
    
  
  def pre_order(self, node) :
    if (node != None) :
      print(node.data, end="  ")
      self.pre_order(node.left)
      self.pre_order(node.right)
    
  
  def post_order(self, node) :
    if (node != None) :
      self.post_order(node.left)
      self.post_order(node.right)
      print(node.data, end="  ")
    
  
  def swap(self, first, second) :
    value = first.data
    first.data = second.data
    second.data = value
  
  def maxHeap(self, head) :
    if (head == None):
      return
    self.maxHeap(head.left)
    self.maxHeap(head.right)
    if (head.left != None and head.left.data > head.data) :
      self.swap(head, head.left)
      self.maxHeap(head)
    
    if (head.right != None and head.right.data > head.data) :
      self.swap(head, head.right)
      self.maxHeap(head)
    

def main() :
  obj = BinaryTree()
  # Make A Binary Tree
  #
  #          5
  #         /  \
  #        4    7
  #       /    /  \
  #      3    6    10
  #       \    \
  #        9    8
  #  
  obj.root = Node(5)
  obj.root.left = Node(4)
  obj.root.right = Node(7)
  obj.root.right.right = Node(10)
  obj.root.right.left = Node(6)
  obj.root.left.left = Node(3)
  obj.root.right.left.right = Node(8)
  obj.root.left.left.right = Node(9)
  print("\nBefore Convert ")
  print("In-order Data : ")
  obj.in_order(obj.root)
  print("\nPre-order Data : ")
  obj.pre_order(obj.root)
  print("\nPost-order Data : ")
  obj.post_order(obj.root)
  obj.maxHeap(obj.root)
  print("\nAfter Convert ")
  print("In-order Data : ")
  obj.in_order(obj.root)
  print("\nPre-order Data : ")
  obj.pre_order(obj.root)
  print("\nPost-order Data : ")
  obj.post_order(obj.root)


if __name__ == "__main__":
  main()

Output

Before Convert 
In-order Data : 
3  9  4  5  6  8  7  10  
Pre-order Data : 
5  4  3  9  7  6  8  10  
Post-order Data : 
9  3  4  8  6  10  7  5  
After Convert 
In-order Data : 
4  3  5  10  7  6  9  8  
Pre-order Data : 
10  5  4  3  9  7  6  8  
Post-order Data : 
3  4  5  6  7  8  9  10
# Ruby Program
# Convert a binary tree to Max Heap
class Node 
	attr_reader :data, :left, :right
	attr_accessor :data, :left, :right
	def initialize(value) 
		@data = value
		@left = nil
		@right = nil
	end
end

class BinaryTree 
	attr_reader :root
	attr_accessor :root
	def initialize() 
		@root = nil
	end
	def in_order(node) 
		if (node != nil) 
			self.in_order(node.left)
			print("  ", node.data)
			self.in_order(node.right)
		end
	end
	def pre_order(node) 
		if (node != nil) 
			print("  ", node.data)
			self.pre_order(node.left)
			self.pre_order(node.right)
		end
	end
	def post_order(node) 
		if (node != nil) 
			self.post_order(node.left)
			self.post_order(node.right)
			print("  ", node.data)
		end
	end
	def swap(first, second) 
		value = first.data
		first.data = second.data
		second.data = value
	end
	def maxHeap(head) 
		if (head == nil) 
			return
		end
		self.maxHeap(head.left)
		self.maxHeap(head.right)
		if (head.left != nil and head.left.data > head.data) 
			self.swap(head, head.left)
			self.maxHeap(head)
		end
		if (head.right != nil and head.right.data > head.data) 
			self.swap(head, head.right)
			self.maxHeap(head)
		end
	end
end
def main() 
	obj = BinaryTree.new()
	# Make A Binary Tree
	#
	#          5
	#         /  \
	#        4    7
	#       /    /  \
	#      3    6    10
	#       \    \
	#        9    8
	#  
	obj.root = Node.new(5)
	obj.root.left = Node.new(4)
	obj.root.right = Node.new(7)
	obj.root.right.right = Node.new(10)
	obj.root.right.left = Node.new(6)
	obj.root.left.left = Node.new(3)
	obj.root.right.left.right = Node.new(8)
	obj.root.left.left.right = Node.new(9)
	print("\nBefore Convert ")
	print("\nIn-order Data  :")
	obj.in_order(obj.root)
	print("\nPre-order Data  :")
	obj.pre_order(obj.root)
	print("\nPost-order Data  :")
	obj.post_order(obj.root)
	obj.maxHeap(obj.root)
	print("\nAfter Convert ")
	print("\nIn-order Data  :")
	obj.in_order(obj.root)
	print("\nPre-order Data  :")
	obj.pre_order(obj.root)
	print("\nPost-order Data  :")
	obj.post_order(obj.root)
end

main()

Output

Before Convert 
In-order Data  :  3  9  4  5  6  8  7  10
Pre-order Data  :  5  4  3  9  7  6  8  10
Post-order Data  :  9  3  4  8  6  10  7  5
After Convert 
In-order Data  :  4  3  5  10  7  6  9  8
Pre-order Data  :  10  5  4  3  9  7  6  8
Post-order Data  :  3  4  5  6  7  8  9  10
<?php
/*
  Php Program
  Convert a binary tree to Max Heap
*/
class Node {
  public $data;
  public $left;
  public $right;

  function __construct($value) {
    $this->data = $value;
    $this->left = null;
    $this->right = null;
  }
}
class BinaryTree {
  public $root;

  function __construct() {
    $this->root = null;
  }
  public  function in_order($node) {
    if ($node != null) {
      $this->in_order($node->left);
      echo("  ". $node->data);
      $this->in_order($node->right);
    }
  }
  public  function pre_order($node) {
    if ($node != null) {
      echo("  ". $node->data);
      $this->pre_order($node->left);
      $this->pre_order($node->right);
    }
  }
  public  function post_order($node) {
    if ($node != null) {
      $this->post_order($node->left);
      $this->post_order($node->right);
      echo("  ". $node->data);
    }
  }
  public  function swap($first, $second) {
    $value = $first->data;
    $first->data = $second->data;
    $second->data = $value;
  }
  public  function maxHeap($head) {
    if ($head == null) {
      return;
    }
    $this->maxHeap($head->left);
    $this->maxHeap($head->right);
    if ($head->left != null && $head->left->data > $head->data) {
      $this->swap($head, $head->left);
      $this->maxHeap($head);
    }
    if ($head->right != null && $head->right->data > $head->data) {
      $this->swap($head, $head->right);
      $this->maxHeap($head);
    }
  }
}
function main() {
  $obj = new BinaryTree();
  /* Make A Binary Tree
  -----------------------
          5
         /  \
        4    7
       /    /  \
      3    6    10
       \    \
        9    8
  */
  $obj->root = new Node(5);
  $obj->root->left = new Node(4);
  $obj->root->right = new Node(7);
  $obj->root->right->right = new Node(10);
  $obj->root->right->left = new Node(6);
  $obj->root->left->left = new Node(3);
  $obj->root->right->left->right = new Node(8);
  $obj->root->left->left->right = new Node(9);
  echo("\nBefore Convert ");
  echo("\nIn-order Data : ");
  $obj->in_order($obj->root);
  echo("\nPre-order Data : ");
  $obj->pre_order($obj->root);
  echo("\nPost-order Data : ");
  $obj->post_order($obj->root);
  $obj->maxHeap($obj->root);
  echo("\nAfter Convert ");
  echo("\nIn-order Data : ");
  $obj->in_order($obj->root);
  echo("\nPre-order Data : ");
  $obj->pre_order($obj->root);
  echo("\nPost-order Data : ");
  $obj->post_order($obj->root);
}
main();

Output

Before Convert 
In-order Data  :  3  9  4  5  6  8  7  10
Pre-order Data  :  5  4  3  9  7  6  8  10
Post-order Data  :  9  3  4  8  6  10  7  5
After Convert 
In-order Data  :  4  3  5  10  7  6  9  8
Pre-order Data  :  10  5  4  3  9  7  6  8
Post-order Data  :  3  4  5  6  7  8  9  10
/*
  Node JS Program
  Convert a binary tree to Max Heap
*/
class Node {

	constructor(value) {
		this.data = value;
		this.left = null;
		this.right = null;
	}
}
class BinaryTree {
	
	constructor() {
		this.root = null;
	}
	in_order(node) {
		if (node != null) {
			this.in_order(node.left);
			process.stdout.write("  " + node.data);
			this.in_order(node.right);
		}
	}
	pre_order(node) {
		if (node != null) {
			process.stdout.write("  " + node.data);
			this.pre_order(node.left);
			this.pre_order(node.right);
		}
	}
	post_order(node) {
		if (node != null) {
			this.post_order(node.left);
			this.post_order(node.right);
			process.stdout.write("  " + node.data);
		}
	}
	swap(first, second) {
		var value = first.data;
		first.data = second.data;
		second.data = value;
	}
	maxHeap(head) {
		if (head == null) {
			return;
		}
		this.maxHeap(head.left);
		this.maxHeap(head.right);
		if (head.left != null && head.left.data > head.data) {
			this.swap(head, head.left);
			this.maxHeap(head);
		}
		if (head.right != null && head.right.data > head.data) {
			this.swap(head, head.right);
			this.maxHeap(head);
		}
	}
}
function main() {
	var obj = new BinaryTree();
	/* Make A Binary Tree
	-----------------------
	      5
	     /  \
	    4    7
	   /    /  \
	  3    6    10
	   \    \
	    9    8
	*/
	obj.root = new Node(5);
	obj.root.left = new Node(4);
	obj.root.right = new Node(7);
	obj.root.right.right = new Node(10);
	obj.root.right.left = new Node(6);
	obj.root.left.left = new Node(3);
	obj.root.right.left.right = new Node(8);
	obj.root.left.left.right = new Node(9);
	process.stdout.write("\nBefore Convert ");
	process.stdout.write("\nIn-order Data : ");
	obj.in_order(obj.root);
	process.stdout.write("\nPre-order Data : ");
	obj.pre_order(obj.root);
	process.stdout.write("\nPost-order Data : ");
	obj.post_order(obj.root);
	obj.maxHeap(obj.root);
	process.stdout.write("\nAfter Convert ");
	process.stdout.write("\nIn-order Data : ");
	obj.in_order(obj.root);
	process.stdout.write("\nPre-order Data : ");
	obj.pre_order(obj.root);
	process.stdout.write("\nPost-order Data : ");
	obj.post_order(obj.root);
}
main();

Output

Before Convert 
In-order Data  :  3  9  4  5  6  8  7  10
Pre-order Data  :  5  4  3  9  7  6  8  10
Post-order Data  :  9  3  4  8  6  10  7  5
After Convert 
In-order Data  :  4  3  5  10  7  6  9  8
Pre-order Data  :  10  5  4  3  9  7  6  8
Post-order Data  :  3  4  5  6  7  8  9  10
/*
  Swift 4 Program
  Convert a binary tree to Max Heap
*/
class Node {
  var data: Int;
  var left: Node? ;
    var right: Node? ;
  init(_ value: Int) {
    self.data = value;
    self.left = nil;
    self.right = nil;
  }
}
class BinaryTree {
  var root: Node? ;
  init() {
    self.root = nil;
  }
  func in_order(_ node: Node? ) {
    if (node != nil) {
      self.in_order(node!.left);
      print(node!.data, terminator:" ");
      self.in_order(node!.right);
    }
  }
  func pre_order(_ node: Node? ) {
    if (node != nil) {
      print(node!.data, terminator:" ");
      self.pre_order(node!.left);
      self.pre_order(node!.right);
    }
  }
  func post_order(_ node: Node? ) {
    if (node != nil) {
      self.post_order(node!.left);
      self.post_order(node!.right);
      print(node!.data, terminator:" ");
    }
  }
  func swap(_ first: Node? , _ second : Node? ) {
    let value: Int = first!.data;
    first!.data = second!.data;
    second!.data = value;
  }
  func maxHeap(_ head: Node? ) {
    if (head == nil) {
      return;
    }
    self.maxHeap(head!.left);
    self.maxHeap(head!.right);
    if (head!.left != nil && head!.left!.data > head!.data) {
      self.swap(head, head!.left);
      self.maxHeap(head);
    }
    if (head!.right != nil && head!.right!.data > head!.data) {
      self.swap(head, head!.right);
      self.maxHeap(head);
    }
  }
}
func main() {
  let obj: BinaryTree = BinaryTree();
  /* Make A Binary Tree
  -----------------------
          5
         /  \
        4    7
       /    /  \
      3    6    10
       \    \
        9    8
  */
  obj.root = Node(5);
  obj.root!.left = Node(4);
  obj.root!.right = Node(7);
  obj.root!.right!.right = Node(10);
  obj.root!.right!.left = Node(6);
  obj.root!.left!.left = Node(3);
  obj.root!.right!.left!.right = Node(8);
  obj.root!.left!.left!.right = Node(9);
  print("\nBefore Convert ");
  print("In-order Data : ");
  obj.in_order(obj.root);
  print("\nPre-order Data : ");
  obj.pre_order(obj.root);
  print("\nPost-order Data : ");
  obj.post_order(obj.root);
  obj.maxHeap(obj.root);
  print("\n\nAfter Convert ");
  print("In-order Data : ");
  obj.in_order(obj.root);
  print("\nPre-order Data : ");
  obj.pre_order(obj.root);
  print("\nPost-order Data : ");
  obj.post_order(obj.root);
}
main();

Output

Before Convert 
In-order Data : 
3 9 4 5 6 8 7 10 
Pre-order Data : 
5 4 3 9 7 6 8 10 
Post-order Data : 
9 3 4 8 6 10 7 5 

After Convert 
In-order Data : 
4 3 5 10 7 6 9 8 
Pre-order Data : 
10 5 4 3 9 7 6 8 
Post-order Data : 
3 4 5 6 7 8 9 10 


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