Posts

Showing posts with the label tree

Count nodes from all lower levels smaller than minimum valued node of current level for every level in a Binary Tree

Given a Binary Tree, the task is for each level is to print the total number of nodes from all lower levels which are less than or equal to every node present at that level. Examples: Input: Below is the given tree:                            4                          /   \                       3       5                     /  \    /  \                  10  2  3    1 Output: 4 3 0 Explanation: Nodes in level 1 has 4 nodes as (3) in level 2 and (2, 3, 1) in level 3. Nodes in level 2 has 3 nodes as (2, 3, 1) in level 3. Nodes in level 3 does not have any level left below it. Input: Below is the given tree:   ...

Lowest common ancestor

Image
Write a program to find the least common ancestor? The lowest common ancestor (LCA) of two nodes v and w in a tree, where we define each node to be a descendant of itself (so if v has a direct connection from w, w is the lowest common ancestor). The LCA of v and w in T is the shared ancestor of v and w that is located farthest from the root. Computation of lowest common ancestors may be useful, for instance, as part of a procedure for determining the distance between pairs of nodes in a tree: the distance from v to w can be computed as the distance from the root to v, plus the distance from the root to w, minus twice the distance from the root to their lowest common ancestor (Djidjev, Pantziou & Zaroliagis 1991). In ontologies, the lowest common ancestor is also known as the least common ancestor. // This function returns pointer to LCA of two given      // values n1 and n2.      // v1 is set as true by this function if n1 is found  ...

How do you create tree data structure ?

/* Class containing left and right child of current node and data value*/ class Node { int  data ; Node   left ,  right ; public  Node (int  data1 ) { data  =  data1 ; left = right = null ; } } // A Java program to introduce Binary Tree class BinaryTree { // Root of Binary Tree Node   root ; // Constructors BinaryTree (int data1) { root  = new  Node (data1); } 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 data structure Implementation

class Node { int data; Node left; Node right; Node(int data) { this.data = data; } } public class BaseTree { public static void main(String[] args) { Node root=new Node(1); Node a1=new Node(2); Node a2=new Node(3); Node a3=new Node(4); Node a4=new Node(5); Node a5=new Node(6); Node a6=new Node(7); root.left=a1; root.right=a2; a1.left=a3; a1.right=a4; a2.left=a5; a2.right=a6; System.out.println(height(root)-1); //preOrder(root); //inOrder(root); //postOrder(root); //System.out.println(root.left.right.data); //System.out.println(root.data); //System.out.println(root.left.data); //System.out.println(root.left.left.data); //System.out.println(root.left.right.data); //System.out.println(root.right.left.data); //System.out.println(root.right.right.data); } public static void preOrder(Node root){ if(root==null){ return; } System.out.println(root.data); preOrder(root.left);// root=root.left;root=ro...

Tree traversal and height and depth Implementation

package DataStructure3; import java.util.ArrayList; import java.util.LinkedList; class Node<E> { E data; LinkedList<Node> next; int lebel; } public class TestTree { public static void main(String[] args) { Node root = new Node(); root.data = 1; root.lebel=1;    root = add(root, 1, 2);    root = add(root, 1, 3);    root = add(root, 1, 4);    root = add(root, 1, 5);    root = add(root, 2, 6);    root = add(root, 3, 7);    root = add(root, 4, 8);    root = add(root, 5, 9);    root = add(root, 5, 10);    root = add(root, 6, 11);    root = add(root, 7, 12);    root = add(root, 13, 1);  

Tree with N child Java implementation

Image
                                                          K- tree Implementation in java                                                           Tree  Pre-order  traversal package DataStructure3; import java.util.LinkedList; class Node<E> { E data; LinkedList<Node> next; } public class TestTree { public static void main(String[] args) { // TODO Auto-generated method stub /* * Node head=new Node(); head.data=1; *  * Node c1=new Node(); c1.data=2; *  * Node c2=new Node(); c2.data=21; LinkedList l=new LinkedList(); * l.add(c1); l.add(c2); head.next=l; *  * LinkedList l2=new LinkedList(); Node c3=new Node()...

Tree with One child - Implementation in Java

class Node { String name; Node child; } public class TreeTest { static Node add(Node x, String child) { if (x == null) { Node node = new Node(); node.name = child; x=node; return x; } if (x.child != null) { add(x.child, child); } else{ Node node = new Node(); node.name = child; x.child=node; } return x; }

Binary search tree implementation using java

Image

Tree Preorder Traversal using Java

Image
/* Node is defined as class Node {     int data;     Node left;     Node right; } */