Showing posts with label java collection. Show all posts
Showing posts with label java collection. Show all posts

Binary Tree, Heap Concept Used in Java collection

A Binary Tree is made of nodes.
Each node contains left pointer and right pointer and a data element.





The root pointer points to the top most node in the tree.
Binary Search Tree (BST) or "Ordered Binary Tree" is a type of binary tree where the nodes are arranged in order for each node.
Elements in left sub tree are less ot equal to the node.
Elements in the right are greater that the node.


Binary tree has two possible branches. left and right.


Heap is complete binary tree. It is tree based datastructure.
There are two types of heap
Max Heap: Element with greatest key (value) is always in the root node.
Min Heap: Element with smallest key (value) is always in the root node.


Java 2 platform provides binary heap implementation with class PriorityQueue java collection framework.


In Java virtual machine heap (heap described above and JVM heap could be different, jvm specification does not provide such details. However the term used is same.) is the runtime data area from which memory for all class instances and array is allocated.
  • Heap is created on virtual machine startup.
  • Heap storage for objects is reclaimed by a automatic storage management system know as GC.
  • Heap is shared among all java virtual machine thread.
  • Heap is split up in to "generations"
  • "Young Generation" stores short lived objects.
  • "Old Generation" objects persist longer are stored in old generation.
  • "Permanent Generation" is used to store class definition and reflection related information and used by virtual machine itself.
  • -Xms (minimum) and -Xmx (Max) parameters are used to specify heap size.
Create Binary tree data structure in java.
package BinaryTree;

public class BinaryTreeExample {
 static class Node
 {
  Node left;
  Node right;
  int value;
  public Node(int value) {
   this.value = value;
  }
 }
 
 public void insert(Node node, int value){
  if (value < node.value) {
            if (node.left != null) {
                insert(node.left, value);
            } else {
                System.out.println("  Inserted " + value + " to left of node " + node.value);
                node.left = new Node(value);
            }
        } else if (value > node.value) {
            if (node.right != null) {
                insert(node.right, value);
            } else {
                System.out.println("  Inserted " + value + " to right of node " + node.value);
                node.right = new Node(value);
            }
        }

 }
 
 public void printInOrder(Node node) {
        if (node != null) {
            printInOrder(node.left);
            System.out.println("  Traversed " + node.value);
            printInOrder(node.right);
        }
    }

}


-----------------------------------

package BinaryTree;

public class Main {
 public static void main(String... str){
  BinaryTreeExample bnary= new BinaryTreeExample();
  BinaryTreeExample.Node node = new BinaryTreeExample.Node(50);
  bnary.insert(node, 6);
  bnary.insert(node, 20);
  bnary.insert(node, 9);
  bnary.insert(node, 9);
  bnary.insert(node, 20);
  bnary.insert(node, 14);
  bnary.insert(node, 15);
  bnary.insert(node, 22);
  bnary.printInOrder(node);
 }
}