Check if binary tree is Continuous Tree

Check if binary tree is Continuous Tree

Here given code implementation process.

/*
  C Program 
+ Check if binary tree is Continuous Tree
*/
#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 inorder(struct Node*node)
{

  if(node!=NULL){

    inorder(node->left);
    //Print node value
    printf("  %d",node->data);
    inorder(node->right);
  }
}
//Display tree element inorder form
void is_continuous(struct Node*root,int *result)
{

  if(root!=NULL && *result == 1){
    
    if(root->left != NULL)
    {
      if(root->left->data-root->data==-1 || root->left->data-root->data==1)
      {
        is_continuous(root->left,result);
      }
      else
      {
        *result = 0;
      }
      
    }
    if(root->right != NULL)
    {
      if(root->right->data-root->data==-1 || root->right->data-root->data==1)
      {
        is_continuous(root->right,result);
      }
      else
      {
        *result = 0;
      }
    }
  }
}


int main(){

  struct Node*root=NULL;
  /*  Make A Binary Tree
  -----------------------
           5
         /   \
        4     4
       /     /  \
      3     5    3
       \
        2
  */
 

  //Insertion of binary tree nodes
  root               =insert(5);
  root->left        = insert(4); 
  root->right       = insert(4);
  root->left->left  = insert(3); 
  root->right->right = insert(3); 
  root->right->left = insert(5); 
  root->left->left->right = insert(2);

  //Display Tree elements
  inorder(root);
  int result=1;
  is_continuous(root,&result);
  if(result==1)
  {
    printf("\n Continuous Tree ");
  }else
  {
     printf("\n Not Continuous Tree ");
  }


  return 0;
}

Output

 3  2  4  5  5  4  3
 Continuous Tree
/*
C++ Program 
Check if binary tree is Continuous Tree
*/
#include<iostream>

using namespace std;
class Node {
  public:
  int data;
  Node *left, *right;
  Node(int value) {
    this->data = value;
    this->left = NULL;
    this->right = NULL;
  }
};
class BinaryTree {
public:
  Node *root;
  bool result;
  BinaryTree() {
    this->root = NULL;
    this->result = false;
  }
  void inorder(Node *node) {
    if (node != NULL) {
      this->inorder(node->left);
      cout << "  " << node->data;
      this->inorder(node->right);
    }
  }
  void is_continuous(Node *head) {
    if (head != NULL && this->result == true) {
      if (head->left != NULL) {
        if (head->left->data - head->data == -1 || head->left->data - head->data == 1) {
          this->is_continuous(head->left);
        } else {
          this->result = false;
        }
      }
      if (head->right != NULL) {
        if (head->right->data - head->data == -1 || head->right->data - head->data == 1) {
          this->is_continuous(head->right);
        } else {
          this->result = false;
        }
      }
    }
  }
  bool check_continuous() {
    this->result = true;
    this->is_continuous(this->root);
    return this->result;
  }
};

int main() {
  BinaryTree obj;
  /*  Make A Binary Tree
  -----------------------
           5
         /   \
        4     4
       /     /  \
      3     5    3
       \
        2
  */
 
  obj.root = new Node(5);
  obj.root->left = new Node(4);
  obj.root->right = new Node(4);
  obj.root->left->left = new Node(3);
  obj.root->right->right = new Node(3);
  obj.root->right->left = new Node(5);
  obj.root->left->left->right = new Node(2);
  obj.inorder(obj.root);
  if (obj.check_continuous() == true) {
    cout << ("\n Continuous Tree ");
  } else {
    cout << ("\n Not Continuous Tree ");
  }
  return 0;
}

Output

 3  2  4  5  5  4  3
 Continuous Tree
/*
Java Program 
Check if binary tree is Continuous Tree
*/

//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 = null;
    right = null;
  }
}

public class BinaryTree {

  public Node root;

  public boolean result;

  public BinaryTree() {
    //set initial tree root to null
    root = null;
    result = false;

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

    if (node != null) {

      inorder(node.left);
      //Print node value
      System.out.print("  "+ node.data);
      inorder(node.right);
    }
  }


  //Display tree element inorder form
  public void  is_continuous(Node  head) {

    if (head != null &&  result == true) {

      if (head.left != null) {
        if (head.left.data - head.data == -1 || head.left.data - head.data == 1) {
          is_continuous(head.left);
        } else 
        {
          result = false;
        }

      }
      if (head.right != null) {
        if (head.right.data - head.data == -1 || head.right.data - head.data == 1) {
          is_continuous(head.right);
        } else {
           result = false;
        }
      }
    }
  }

  public boolean check_continuous()
  {

    this.result=true;

    is_continuous(root);

    return this.result;
  }

  public static void main(String[] args) {
    //Make object of Binary Tree
    BinaryTree obj = new BinaryTree();
    /*  Make A Binary Tree
      -----------------------
           5
         /   \
        4     4
       /     /  \
      3     5    3
       \
        2
    */
     

    //binary tree nodes
    obj.root               =new Node(5);
    obj.root.left        = new Node(4); 
    obj.root.right       = new Node(4);
    obj.root.left.left  = new Node(3); 
    obj.root.right.right = new Node(3); 
    obj.root.right.left = new Node(5); 
    obj.root.left.left.right = new Node(2);

    //Display Tree elements
    obj.inorder(obj.root);
 
  
    if(obj.check_continuous()==true)
    {
      System.out.print("\n Continuous Tree ");
    }else
    {
      System.out.print("\n Not Continuous Tree ");
    }

  }
}

Output

 3  2  4  5  5  4  3
 Continuous Tree
/*
C# Program 
Check if binary tree is Continuous Tree
*/
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 = null;
		right = null;
	}
}

public class BinaryTree {

	public Node root;

	public Boolean result;

	public BinaryTree() {
		//set initial tree root to null
		root = null;
		result = false;

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

		if (node != null) {

			inorder(node.left);
			//Print node value
			Console.Write("  "+ node.data);
			inorder(node.right);
		}
	}


	//Display tree element inorder form
	public void  is_continuous(Node  head) {

		if (head != null &&  result == true) {

			if (head.left != null) {
				if (head.left.data - head.data == -1 || head.left.data - head.data == 1) {
					is_continuous(head.left);
				} else 
				{
					result = false;
				}

			}
			if (head.right != null) {
				if (head.right.data - head.data == -1 || head.right.data - head.data == 1) {
					is_continuous(head.right);
				} else {
					result = false;
				}
			}
		}
	}

	public Boolean check_continuous()
	{

		this.result=true;

		is_continuous(root);

		return this.result;
	}

	public static void Main(String[] args) {
		//Make object of Binary Tree
		BinaryTree obj = new BinaryTree();
		/*  Make A Binary Tree
      -----------------------
           5
         /   \
        4     4
       /     /  \
      3     5    3
       \
        2
    */


		//binary tree nodes
		obj.root               =new Node(5);
		obj.root.left        = new Node(4); 
		obj.root.right       = new Node(4);
		obj.root.left.left  = new Node(3); 
		obj.root.right.right = new Node(3); 
		obj.root.right.left = new Node(5); 
		obj.root.left.left.right = new Node(2);

		//Display Tree elements
		obj.inorder(obj.root);


		if(obj.check_continuous()==true)
		{
			Console.Write("\n Continuous Tree ");
		}else
		{
			Console.Write("\n Not Continuous Tree ");
		}

	}
}

Output

 3  2  4  5  5  4  3
 Continuous Tree
# Python Program 
# Check if binary tree is Continuous Tree

class Node :

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

class BinaryTree :

  def __init__(self) :
    self.root = None
    self.result = False
  
  def inorder(self, node) :
    if (node != None) :
      self.inorder(node.left)
      print(node.data,end=" ")
      self.inorder(node.right)
    
  
  def is_continuous(self, head) :
    if (head != None and self.result == True) :
      if (head.left != None) :
        if (head.left.data - head.data == -1 or head.left.data - head.data == 1) :
          self.is_continuous(head.left)
        else :
          self.result = False
        
      
      if (head.right != None) :
        if (head.right.data - head.data == -1 or head.right.data - head.data == 1) :
          self.is_continuous(head.right)
        else :
          self.result = False
        
      
    
  
  def check_continuous(self) :
    self.result = True
    self.is_continuous(self.root)
    return self.result
  
def main() :
  obj = BinaryTree() 
  #  Make A Binary Tree
  #           5
  #         /   \
  #        4     4
  #       /     /  \
  #      3     5    3
  #       \
  #        2
  #  
  obj.root = Node(5)
  obj.root.left = Node(4)
  obj.root.right = Node(4)
  obj.root.left.left = Node(3)
  obj.root.right.right = Node(3)
  obj.root.right.left = Node(5)
  obj.root.left.left.right = Node(2)
  obj.inorder(obj.root)
  if (obj.check_continuous() == True) :
    print("\n Continuous Tree ")
  else :
    print("\n Not Continuous Tree ")
  
  

if __name__ == "__main__":
  main()

Output

 3  2  4  5  5  4  3
 Continuous Tree
# Ruby Program
# Check if binary tree is Continuous Tree

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, :result
	attr_accessor :root, :result
	def initialize() 
		@root = nil
		@result = false
	end
	def inorder(node) 
		if (node != nil) 
			self.inorder(node.left)
			print("  ", node.data)
			self.inorder(node.right)
		end
	end
	def is_continuous(head) 
		if (head != nil and @result == true) 
			if (head.left != nil) 
				if (head.left.data - head.data == -1 or head.left.data - head.data == 1) 
					self.is_continuous(head.left)
				else 
					@result = false
				end
			end
			if (head.right != nil) 
				if (head.right.data - head.data == -1 or head.right.data - head.data == 1) 
					self.is_continuous(head.right)
				else 
					@result = false
				end
			end
		end
	end
	def check_continuous() 
		self.result = true
		self.is_continuous(@root)
		return self.result
	end
end



def main() 
	obj = BinaryTree.new()
	#  Make A Binary Tree
	#           5
	#         /   \
	#        4     4
	#       /     /  \
	#      3     5    3
	#       \
	#        2
	#  
	obj.root = Node.new(5)
	obj.root.left = Node.new(4)
	obj.root.right = Node.new(4)
	obj.root.left.left = Node.new(3)
	obj.root.right.right = Node.new(3)
	obj.root.right.left = Node.new(5)
	obj.root.left.left.right = Node.new(2)
	obj.inorder(obj.root)
	if (obj.check_continuous() == true) 
		print("\n Continuous Tree ")
	else 
		print("\n Not Continuous Tree ")
	end
end

main()

Output

 3  2  4  5  5  4  3
 Continuous Tree
<?php
/*
  Php Program
  Check if binary tree is Continuous Tree
*/
class Node {
  public $data;
  public $left;
  public $right;

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

  function __construct() {
    $this->root = null;
    $this->result = false;
  }
  public  function inorder($node) {
    if ($node != null) {
      $this->inorder($node->left);
      echo("  ". $node->data);
      $this->inorder($node->right);
    }
  }
  public  function is_continuous($head) {
    if ($head != null && $this->result == true) {
      if ($head->left != null) {
        if ($head->left->data - $head->data == -1 || $head->left->data - $head->data == 1) {
          $this->is_continuous($head->left);
        } else {
          $this->result = false;
        }
      }
      if ($head->right != null) {
        if ($head->right->data - $head->data == -1 || $head->right->data - $head->data == 1) {
          $this->is_continuous($head->right);
        } else {
          $this->result = false;
        }
      }
    }
  }
  public  function check_continuous() {
    $this->result = true;
    $this->is_continuous($this->root);
    return $this->result;
  }
}

function main() {
  $obj = new BinaryTree();
  /*  Make A Binary Tree
  -----------------------
           5
         /   \
        4     4
       /     /  \
      3     5    3
       \
        2
  */
 
  $obj->root = new Node(5);
  $obj->root->left = new Node(4);
  $obj->root->right = new Node(4);
  $obj->root->left->left = new Node(3);
  $obj->root->right->right = new Node(3);
  $obj->root->right->left = new Node(5);
  $obj->root->left->left->right = new Node(2);
  $obj->inorder($obj->root);
  if (
    $obj->check_continuous() == true) {
    echo("\n Continuous Tree ");
  } else {
    echo("\n Not Continuous Tree ");
  }
}
main();

Output

 3  2  4  5  5  4  3
 Continuous Tree
/*
  Node JS Program
  Check if binary tree is Continuous Tree
*/
class Node {

	constructor(value) {
		this.data = value;
		this.left = null;
		this.right = null;
	}
}
class BinaryTree {
	
	constructor() {
		this.root = null;
		this.result = false;
	}
	inorder(node) {
		if (node != null) {
			this.inorder(node.left);
			process.stdout.write("  " + node.data);
			this.inorder(node.right);
		}
	}
	is_continuous(head) {
		if (head != null && this.result == true) {
			if (head.left != null) {
				if (head.left.data - head.data == -1 || head.left.data - head.data == 1) {
					this.is_continuous(head.left);
				} else {
					this.result = false;
				}
			}
			if (head.right != null) {
				if (head.right.data - head.data == -1 || head.right.data - head.data == 1) {
					this.is_continuous(head.right);
				} else {
					this.result = false;
				}
			}
		}
	}
	check_continuous() {
		this.result = true;
		this.is_continuous(this.root);
		return this.result;
	}
}

function main() {
	var obj = new BinaryTree();
	/*  Make A Binary Tree
	---------------------
           5
         /   \
        4     4
       /     /  \
      3     5    3
       \
        2
  	*/
 
	obj.root = new Node(5);
	obj.root.left = new Node(4);
	obj.root.right = new Node(4);
	obj.root.left.left = new Node(3);
	obj.root.right.right = new Node(3);
	obj.root.right.left = new Node(5);
	obj.root.left.left.right = new Node(2);
	obj.inorder(obj.root);
	if (obj.check_continuous() == true) {
		process.stdout.write("\n Continuous Tree ");
	} else {
		process.stdout.write("\n Not Continuous Tree ");
	}
}
main();

Output

 3  2  4  5  5  4  3
 Continuous Tree
/*
  Swift 4 Program
  Check if binary tree is Continuous Tree
*/
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? ;
  var result: Bool;
  init() {
    self.root = nil;
    self.result = false;
  }
  func inorder(_ node: Node? ) {
    if (node != nil) {
      self.inorder(node!.left);
      print( node!.data, terminator:" ");
      self.inorder(node!.right);
    }
  }
  func is_continuous(_ head: Node? ) {
    if (head != nil && self.result == true) {
      if (head!.left != nil) {
        if (head!.left!.data - head!.data == -1 || head!.left!.data - head!.data == 1) {
          self.is_continuous(head!.left);
        } else {
          self.result = false;
        }
      }
      if (head!.right != nil) {
        if (head!.right!.data - head!.data == -1 || head!.right!.data - head!.data == 1) {
          self.is_continuous(head!.right);
        } else {
          self.result = false;
        }
      }
    }
  }
  func check_continuous() -> Bool {
    self.result = true;
    self.is_continuous(self.root);
    return self.result;
  }

}

func main() {
  let obj: BinaryTree = BinaryTree();
  /*  Make A Binary Tree
  -----------------------
           5
         /   \
        4     4
       /     /  \
      3     5    3
       \
        2
  */
 
  obj.root = Node(5);
  obj.root!.left = Node(4);
  obj.root!.right = Node(4);
  obj.root!.left!.left = Node(3);
  obj.root!.right!.right = Node(3);
  obj.root!.right!.left = Node(5);
  obj.root!.left!.left!.right = Node(2);
  obj.inorder(obj.root);
  if (obj.check_continuous() == true) {
    print("\n Continuous Tree ");
  } else {
    print("\n Not Continuous Tree ");
  }
}

main();

Output

 3  2  4  5  5  4  3
 Continuous Tree


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