Please open the menu to show more

Friday, August 11, 2017

Who says java does not supports pointers part-six, lets implement a Tree in Java, a code by Pankaj

This extraordinary code of Binary Tree is implemented and provided by Pankaj, from Nit-Arunachal Pradesh
Note: Binary tree is a non linear data structure. It is very important and useful in computer science. In a binary tree huge amount of data can be stored and retrieved in a very less period of time. So, its time complexity is very good. For further information on binary tree please click on given link more about binary tree
note : before using this code please create a "java project" in "eclipse" and store all the classes in separate program files inside "src" folder of "java project".
## save this class inside BTNode.java file ## import java.util.Scanner; public class BTNode {
BTNode left, right; int data; // Constructor to craete a node public BTNode() { left = null; right = null; data = 0; } // Constructor to put value of data public BTNode(int n) { left = null; right = null; data = n; } // method to join left node public void setLeft(BTNode n) { left = n; } // method to join right node public void setRight(BTNode n) { right = n; } // method to get left node public BTNode getLeft() { return left; } // method to get right node public BTNode getRight() { return right; } // method for set data to node public void setData(int d) { data = d; } // method to get data from node public int getData() { return data; } }
## save this class inside BT.java file ## // Class BT public class BT { private BTNode root; public BT() { root = null; } // empty check public boolean isEmpty() { return root == null; } public void insert(int data) { root = insert(root, data); } private BTNode insert(BTNode node, int data) { if (node == null) node = new BTNode(data); else { if (node.getRight() == null) node.right = insert(node.right, data); else node.left = insert(node.left, data); } return node; } // count number of nodes public int countNodes() { return countNodes(root); } // count nodes implementation private int countNodes(BTNode r) { if (r == null) return 0; else { int l = 1; l += countNodes(r.getLeft()); l += countNodes(r.getRight()); return l; } } // search for an element public boolean search(int val) { return search(root, val); } // search implementation private boolean search(BTNode r, int val) { if (r.getData() == val) return true; if (r.getLeft() != null) if (search(r.getLeft(), val)) return true; if (r.getRight() != null) if (search(r.getRight(), val)) return true; return false; } // inorder traversal public void inorder() { inorder(root); } private void inorder(BTNode r) { if (r != null) { inorder(r.getLeft()); System.out.print(r.getData() + " "); inorder(r.getRight()); } } // preorder traversal public void preorder() { preorder(root); } private void preorder(BTNode r) { if (r != null) { System.out.print(r.getData() + " "); preorder(r.getLeft()); preorder(r.getRight()); } } // postorder traversal public void postorder() { postorder(root); } private void postorder(BTNode r) { if (r != null) { postorder(r.getLeft()); postorder(r.getRight()); System.out.print(r.getData() + " "); } } } ## save this class inside BinaryTree.java file ## public class BinaryTree { public static void main(String[] args) { Scanner scan = new Scanner(System.in); BT bt = new BT(); System.out.println("Binary Tree Test\n"); char ch; do { System.out.println("\nBinary Tree Operations\n"); System.out.println("press 1 to insert "); System.out.println("press 2 to search"); System.out.println("press 3 to count nodes"); System.out.println("press 4 to check empty"); int choice = scan.nextInt(); switch (choice) { case 1: System.out.println("Enter integer element to insert"); bt.insert(scan.nextInt()); break; case 2: System.out.println("Enter integer element to search"); int element = scan.nextInt(); System.out.println("Search result : " + bt.search(element)); break; case 3: System.out.println("Nodes = " + bt.countNodes()); break; case 4: System.out.println("Empty status = " + bt.isEmpty()); break; default: System.out.println("Wrong Entry \n "); break; } // display the tree System.out.print("\nPost order : "); bt.postorder(); System.out.print("\nPre order : "); bt.preorder(); System.out.print("\nIn order : "); bt.inorder(); System.out.println("\n\nDo you want to continue (Type y or n) \n"); ch = scan.next().charAt(0); } while (ch == 'Y' || ch == 'y'); } }

1 comment: