Print all odd nodes of Binary Search Tree

Print all odd nodes of BST

Here given code implementation process.

//C Program 
//Print all odd nodes of Binary Search Tree
#include <stdio.h>
#include <stdlib.h>
//structure of Binary Search Tree node
struct Node
{
  int data;
  struct Node *left,*right; 
};

//Adding a new node in binary search tree
void add( struct Node **root, int data)
{
  //Create a dynamic node of binary search tree 
  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

    if(*root == NULL)
    {
      //When adds a first node in binary tree
      *root = new_node;
    }
    else
    {
      struct Node *find = *root;
      //iterate binary tree and add new node to proper position
      while(find != NULL)
      {
        if(find -> data > data)
        { 
          if(find->left==NULL)
          {
            find->left = new_node;
            break;
          }
          else
          { //visit left sub-tree
            find = find->left;
          }
        }
        else
        {
          if(find->right == NULL)
          {
            find->right = new_node;
            break;
          }
          else
          {
            //visit right sub-tree
            find = find->right;
          }
        }
      }
    }
  }else
  {
    printf("Memory Overflow\n");
    exit(0); //Terminate program execution
  }

}


void print_odd_node(struct Node*root)
{
  if(root != NULL)
  {
    
    
    print_odd_node(root->left);
    if(root->data%2!=0)
    {
      printf("%3d",root->data );
    }
    print_odd_node(root->right);
  }
}

int main(){
    
  struct Node*root = NULL;

  //Add nodes in binary search tree
  /*
             5
           /    \
          3      9
         / \     / \
        1   4   8   11
       / \     /      \
      -3  2   6        12


  */                


    add(&root,5); 
    add(&root,3); 
    add(&root,9); 
    add(&root,1); 
    add(&root,4); 
    add(&root,8); 
    add(&root,11); 
    add(&root,-3); 
    add(&root,2); 
    add(&root,6); 
    add(&root,12); 
  
    print_odd_node(root);

  return 0;
}

Output

 -3  1  3  5  9 11
/*
 C++ Program
 Print all odd nodes of Binary Search Tree
*/

#include<iostream>

using namespace std;
class Node {
public:
  int data;
  Node *left;
  Node *right;
  Node(int value) {
    this->data = value;
    this->left = NULL;
    this->right = NULL;
  }
};
class BinarySearchTree {
public:
  Node *root;
  bool status;
  BinarySearchTree() {
    this->root = NULL;
    this->status = false;
  }
  void add(int value) {
    Node *new_node = new Node(value);
    if (new_node != NULL) {
      if (this->root == NULL) {
        this->root = new_node;
      } else {
        Node *find = this->root;
        while (find != NULL) {
          if (find->data >= value) {
            if (find->left == NULL) {
              find->left = new_node;
              break;
            } else {
              find = find->left;
            }
          } else {
            if (find->right == NULL) {
              find->right = new_node;
              break;
            } else {
              find = find->right;
            }
          }
        }
      }
    } else {
      cout << "\nMemory Overflow\n";
    }
  }
  void print_odd_node(Node *head) {
    if (head != NULL) {
      this->print_odd_node(head->left);
      if (head->data % 2 != 0) {
        cout << "  " << head->data;
      }
      this->print_odd_node(head->right);
    }
  }
};

int main() {
  BinarySearchTree obj;
    /*
             5
           /    \
          3      9
         / \     / \
        1   4   8   11
       / \     /      \
      -3  2   6        12


  */    
  obj.add(5);
  obj.add(3);
  obj.add(9);
  obj.add(1);
  obj.add(4);
  obj.add(8);
  obj.add(11);
  obj.add(-3);
  obj.add(2);
  obj.add(6);
  obj.add(12);
  obj.print_odd_node(obj.root);
  return 0;
}

Output

 -3  1  3  5  9 11
//Java program
//Print all odd nodes of Binary Search Tree

class Node {
  public int data;
  public Node left;
  public Node right;

  public Node(int value) {
    data = value;
    left = null;
    right = null;
  }
}
public class BinarySearchTree {


  public Node root;

  public boolean status;


  BinarySearchTree() {
    root = null;
    status = false;
  }
  //insert a node in BST
  public void add(int value) {
    //Create a dynamic node of binary search tree 
    Node new_node = new Node(value);

    if (new_node != null) {
      if (root == null) {
        //When adds a first node in binary tree
        root = new_node;
      } else {
        Node find = root;

        //add new node to proper position
        while (find != null) {
          if (find.data >= value) {
            if (find.left == null) {
              find.left = new_node;
              break;
            } else {
              //visit left sub-tree
              find = find.left;
            }
          } else {
            if (find.right == null) {
              find.right = new_node;
              break;
            } else {
              //visit right sub-tree
              find = find.right;
            }
          }
        }
      }
    } else {
      System.out.print("\nMemory Overflow\n");
    }
  }
  public void print_odd_node(Node head) {
    if (head != null) {
      print_odd_node(head.left);

      if (head.data % 2 != 0) {
        System.out.print("  " + head.data);
      }
      print_odd_node(head.right);
    }
  }
  public static void main(String[] args) {

    BinarySearchTree obj = new BinarySearchTree();
    //Add nodes in binary search tree
    /*
           5
         /    \
        3      9
       / \     / \
      1   4   8   11
     / \     /      \
    -3  2   6        12


    */


    obj.add(5);
    obj.add(3);
    obj.add(9);
    obj.add(1);
    obj.add(4);
    obj.add(8);
    obj.add(11);
    obj.add(-3);
    obj.add(2);
    obj.add(6);
    obj.add(12);

    obj.print_odd_node(obj.root);
  }
}

Output

 -3  1  3  5  9 11
//C# program
//Print all odd nodes of Binary Search Tree
using System;
public class Node {
	public int data;
	public Node left;
	public Node right;

	public Node(int value) {
		data = value;
		left = null;
		right = null;
	}
}
public class BinarySearchTree {


	public Node root;

	public Boolean status;


	BinarySearchTree() {
		root = null;
		status = false;
	}
	//insert a node in BST
	public void add(int value) {
		//Create a dynamic node of binary search tree 
		Node new_node = new Node(value);

		if (new_node != null) {
			if (root == null) {
				//When adds a first node in binary tree
				root = new_node;
			} else {
				Node find = root;

				//add new node to proper position
				while (find != null) {
					if (find.data >= value) {
						if (find.left == null) {
							find.left = new_node;
							break;
						} else {
							//visit left sub-tree
							find = find.left;
						}
					} else {
						if (find.right == null) {
							find.right = new_node;
							break;
						} else {
							//visit right sub-tree
							find = find.right;
						}
					}
				}
			}
		} else {
			Console.Write("\nMemory Overflow\n");
		}
	}
	public void print_odd_node(Node head) {
		if (head != null) {
			print_odd_node(head.left);

			if (head.data % 2 != 0) {
				Console.Write("  " + head.data);
			}
			print_odd_node(head.right);
		}
	}
	public static void Main(String[] args) {

		BinarySearchTree obj = new BinarySearchTree();
		//Add nodes in binary search tree
		/*
           5
         /    \
        3      9
       / \     / \
      1   4   8   11
     / \     /      \
    -3  2   6        12


    */


		obj.add(5);
		obj.add(3);
		obj.add(9);
		obj.add(1);
		obj.add(4);
		obj.add(8);
		obj.add(11);
		obj.add(-3);
		obj.add(2);
		obj.add(6);
		obj.add(12);

		obj.print_odd_node(obj.root);
	}
}

Output

 -3  1  3  5  9 11
# Python 3 Program
# Print all odd nodes of Binary Search Tree

class Node :

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

class BinarySearchTree :
  
  def __init__(self) :
    self.root = None
    self.status = False
  
  def add(self, value) :
    new_node = Node(value)
    if (new_node != None) :
      if (self.root == None) :
        self.root = new_node
      else :
        find = self.root
        while (find != None) :
          if (find.data >= value) :
            if (find.left == None) :
              find.left = new_node
              break
            else :
              find = find.left
            
          else :
            if (find.right == None) :
              find.right = new_node
              break
            else :
              find = find.right
            
          
        
      
    else :
      print("\nMemory Overflow\n")
    
  
  def print_odd_node(self, head) :
    if (head != None) :
      self.print_odd_node(head.left)
      if (head.data % 2 != 0) :
        print(head.data,end=" ")
      
      self.print_odd_node(head.right)
    
  
def main() :
  obj = BinarySearchTree()

  #
  #             5
  #           /    \
  #          3      9
  #         / \     / \
  #        1   4   8   11
  #       / \     /      \
  #      -3  2   6        12
  #  
  obj.add(5)
  obj.add(3)
  obj.add(9)
  obj.add(1)
  obj.add(4)
  obj.add(8)
  obj.add(11)
  obj.add(-3)
  obj.add(2)
  obj.add(6)
  obj.add(12)
  obj.print_odd_node(obj.root)
  

if __name__ == "__main__":
  main()

Output

 -3  1  3  5  9 11
# Ruby Program
# Print all odd nodes of Binary Search 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 BinarySearchTree 
	attr_reader :root, :status
	attr_accessor :root, :status
	def initialize() 
		@root = nil
		@status = false
	end
	def add(value) 
		new_node = Node.new(value)
		if (new_node != nil) 
			if (@root == nil) 
				@root = new_node
			else 
				find = @root
				while (find != nil) 
					if (find.data >= value) 
						if (find.left == nil) 
							find.left = new_node
							break
						else 
							find = find.left
						end
					else 
						if (find.right == nil) 
							find.right = new_node
							break
						else 
							find = find.right
						end
					end
				end
			end
		else 
			print("\nMemory Overflow\n")
		end
	end
	def print_odd_node(head) 
		if (head != nil) 
			self.print_odd_node(head.left)
			if (head.data % 2 != 0) 
				print("  ", head.data)
			end
			self.print_odd_node(head.right)
		end
	end
end
def main() 
	obj = BinarySearchTree.new()

	#
	#             5
	#           /    \
	#          3      9
	#         / \     / \
	#        1   4   8   11
	#       / \     /      \
	#      -3  2   6        12
	#  
	obj.add(5)
	obj.add(3)
	obj.add(9)
	obj.add(1)
	obj.add(4)
	obj.add(8)
	obj.add(11)
	obj.add(-3)
	obj.add(2)
	obj.add(6)
	obj.add(12)
	obj.print_odd_node(obj.root)
end

main()

Output

 -3  1  3  5  9 11
<?php
/*
 Php Program
 Print all odd nodes of Binary Search Tree
*/

class Node {
  public $data;
  public $left;
  public $right;

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

  function __construct() {
    $this->root = null;
    $this->status = false;
  }
  public  function add($value) {
    $new_node = new Node($value);
    if ($new_node != null) {
      if ($this->root == null) {
        $this->root = $new_node;
      } else {
        $find = $this->root;
        while ($find != null) {
          if ($find->data >= $value) {
            if ($find->left == null) {
              $find->left = $new_node;
              break;
            } else {
              $find = $find->left;
            }
          } else {
            if ($find->right == null) {
              $find->right = $new_node;
              break;
            } else {
              $find = $find->right;
            }
          }
        }
      }
    } else {
      echo("\nMemory Overflow\n");
    }
  }
  public  function print_odd_node($head) {
    if ($head != null) {
      $this->print_odd_node($head->left);
      if ($head->data % 2 != 0) {
        echo("  ". $head->data);
      }
      $this->print_odd_node($head->right);
    }
  }
}
function main() {
  $obj = new BinarySearchTree();
    /*
             5
           /    \
          3      9
         / \     / \
        1   4   8   11
       / \     /      \
      -3  2   6        12


  */    
  $obj->add(5);
  $obj->add(3);
  $obj->add(9);
  $obj->add(1);
  $obj->add(4);
  $obj->add(8);
  $obj->add(11);
  $obj->add(-3);
  $obj->add(2);
  $obj->add(6);
  $obj->add(12);
  $obj->print_odd_node($obj->root);
}
main();

Output

 -3  1  3  5  9 11
/*
 Node Js Program
 Print all odd nodes of Binary Search Tree
*/

class Node {
	
	constructor(value) {
		this.data = value;
		this.left = null;
		this.right = null;
	}
}
class BinarySearchTree {

	constructor() {
		this.root = null;
		this.status = false;
	}
	add(value) {
		var new_node = new Node(value);
		if (new_node != null) {
			if (this.root == null) {
				this.root = new_node;
			} else {
				var find = this.root;
				while (find != null) {
					if (find.data >= value) {
						if (find.left == null) {
							find.left = new_node;
							break;
						} else {
							find = find.left;
						}
					} else {
						if (find.right == null) {
							find.right = new_node;
							break;
						} else {
							find = find.right;
						}
					}
				}
			}
		} else {
			process.stdout.write("\nMemory Overflow\n");
		}
	}
	print_odd_node(head) {
		if (head != null) {
			this.print_odd_node(head.left);
			if (head.data % 2 != 0) {
				process.stdout.write("  " + head.data);
			}
			this.print_odd_node(head.right);
		}
	}
}

function main() {
	var obj = new BinarySearchTree();
	  /*
             5
           /    \
          3      9
         / \     / \
        1   4   8   11
       / \     /      \
      -3  2   6        12


  */    
	obj.add(5);
	obj.add(3);
	obj.add(9);
	obj.add(1);
	obj.add(4);
	obj.add(8);
	obj.add(11);
	obj.add(-3);
	obj.add(2);
	obj.add(6);
	obj.add(12);
	obj.print_odd_node(obj.root);
}


main();

Output

 -3  1  3  5  9 11
/*
 Swift 4 Program
 Print all odd nodes of Binary Search 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 BinarySearchTree {
  var root: Node? ;
  var status: Bool;
  init() {
    self.root = nil;
    self.status = false;
  }
  func add(_ value: Int) {
    let new_node: Node? = Node(value);
    if (new_node != nil) {
      if (self.root == nil) {
        self.root = new_node;
      } else {
        var find: Node? = self.root;
        while (find != nil) {
          if (find!.data >= value) {
            if (find!.left == nil) {
              find!.left = new_node;
              break;
            } else {
              find = find!.left;
            }
          } else {
            if (find!.right == nil) {
              find!.right = new_node;
              break;
            } else {
              find = find!.right;
            }
          }
        }
      }
    } else {
      print("\nMemory Overflow\n");
    }
  }
  func print_odd_node(_ head: Node? ) {
    if (head != nil) {
      self.print_odd_node(head!.left);
      if (head!.data % 2 != 0) {
        print(head!.data, terminator:"  ");
      }
      self.print_odd_node(head!.right);
    }
  }
}
func main() {
  let obj: BinarySearchTree = BinarySearchTree();
    /*
             5
           /    \
          3      9
         / \     / \
        1   4   8   11
       / \     /      \
      -3  2   6        12


  */    
  obj.add(5);
  obj.add(3);
  obj.add(9);
  obj.add(1);
  obj.add(4);
  obj.add(8);
  obj.add(11);
  obj.add(-3);
  obj.add(2);
  obj.add(6);
  obj.add(12);
  obj.print_odd_node(obj.root);
}
main();

Output

 -3  1  3  5  9 11


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