From 9308ddba11e36b92a5cb438103b905ac8ab07ba5 Mon Sep 17 00:00:00 2001 From: Rupali Kavale <30548190+coderquill@users.noreply.github.com> Date: Fri, 5 Oct 2018 01:55:15 +0530 Subject: [PATCH] Added program to implement binary tree creation. --- data-structures/BinaryTree.java | 67 +++++++++++++++++++++++++++++++++ 1 file changed, 67 insertions(+) create mode 100644 data-structures/BinaryTree.java diff --git a/data-structures/BinaryTree.java b/data-structures/BinaryTree.java new file mode 100644 index 0000000..436c047 --- /dev/null +++ b/data-structures/BinaryTree.java @@ -0,0 +1,67 @@ +/* Class containing left and right child of current + node and key value*/ +class Node +{ + int key; + Node left, right; + + public Node(int item) + { + key = item; + left = right = null; + } +} + +// A Java program to make Binary Tree +class BinaryTree +{ + // Root of Binary Tree + Node root; + + // Constructors + BinaryTree(int key) + { + root = new Node(key); + } + + BinaryTree() + { + root = null; + } + + public static void main(String[] args) + { + BinaryTree tree = new BinaryTree(); + + /*create root*/ + tree.root = new Node(1); + + /* following is the tree after above statement + + 1 + / \ + null null */ + + tree.root.left = new Node(2); + tree.root.right = new Node(3); + + /* 2 and 3 become left and right children of 1 + 1 + / \ + 2 3 + / \ / \ + null null null null */ + + + tree.root.left.left = new Node(4); + /* 4 becomes left child of 2 + 1 + / \ + 2 3 + / \ / \ + 4 null null null + / \ + null null + */ + } +}