Skip to main content

Tree Data Structure Java

    

Introduction to Generic Trees (N-ary Trees)

Generic Tree Node -----------
● Implementation of a generic tree node in Java is quite simple. 
● The node class will contain two attributes: 
        ○ The node​ data     
        ○ Since there can be a variable number of children nodes, thus the second attribute will be a list of its children nodes. Each child is an instance of the same node class. This is a general ​n-nary tree​. 
● Consider the given implementation of the ​Tree Node​ class

Question: What is Tree Data Strucure ?

Ans; In Java, a tree data structure is a hierarchical data structure consisting of nodes connected by edges. Each node in a tree can have zero or more child nodes, with one node being designated as the root node. Nodes that have no children are called leaf nodes.


Question: How to create member variable and basic class tree class properties using arrayList ?

Ans:

import java.util.*;  //java.util.ArrayList
public class Tree_Node<T> { //T is for create Generic class

T data;
ArrayList<Tree_Node<T>> children;



}


Note: before use arrayList we need to import package .

Question: why you are using angle brackets (<T>) with ArrayList<Tree_Node<T>> but not with T data is because of how generics work in Java.


Ans: In the case of ArrayList<Tree_Node<T>>, you are creating an ArrayList that can hold elements of type Tree_Node<T>. Here, T is the generic type parameter that represents the type of data stored in each Tree_Node.

On the other hand, when you declare T data, you are simply declaring a variable data of type T. The actual type for T will be determined when an instance of Tree_Node<T> is created. So, when you create an instance of Tree_Node<String>, for example, T will be replaced with String, and data will be of type String.

In summary, you use angle brackets with ArrayList<Tree_Node<T>> because you're declaring a collection of Tree_Node objects with a specific type T. However, for T data, you're declaring a variable whose type will be specified when an instance of Tree_Node is created, so you don't need to specify the type parameter T when declaring the variable.

Question: Why we are writing <Tree_Node> ? Ans: bcse isme data hota hai har ek node ka mtlb khud ka , saath hi children ka address hota hai taki next children ka / tree node ka address store kar paye aur next tree node ko check kar paye.

Question: How to implement Generic trees ?

Ans:

First I'm going to create Member variable in Tree_Node class .

import java.util.*;
public class Tree_Node<T> { //T is for create Generic class

T data;
ArrayList<Tree_Node<T>> children; //this is arrayList
//ArrayList me angular bracket me Tree_Node is liye likh rhe kyu ki every children ka
//ek data and children hoga



//constructor
Tree_Node(T data){
this.data= data;
children = new ArrayList<>(); //ArrayList Initialized

}


}


after then will create a object for this class on another class>>>

import java.util.*;
public class Tree_Node_Use {


public static void main(String[] args) {



//here we are creating a object of Tree_Node class
Tree_Node<Integer> root = new Tree_Node<>(4);
Tree_Node<Integer> node1 = new Tree_Node<>(2);
Tree_Node<Integer> node2 = new Tree_Node<>(3);
Tree_Node<Integer> node3 = new Tree_Node<>(5);
Tree_Node<Integer> node4 = new Tree_Node<>(6);

root.children.add(node1);
root.children.add(node2);
root.children.add(node3);

node3.children.add(node4);
System.out.println(root);

}
}







Comments

Popular posts from this blog

Block vs Inline Elements In HTML

  Block: Block element always start in New Line  Block element always start in new line occupy complete width available  Example: h2, p, div Inline Element: They do not start in new line occuply width as much required  Example: img, anchor a,span There are no different between div and span, only difference is div is block element and span is Inline, it will help you to group your content or elements

Add CSS using external CSS

>>> U just need to create a another page and save it with the name style.css >>> and then go to link that style page with your html docs how to link your css with html page ? >>> You can find code below , it will help you to link your external page with your html docs <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1"> <title>Divyanshu Khare || MERN Developer</title> <meta description = "description" content="Divyanshu Khare's website"> <link rel="stylesheet" type="text/css" href="style.css">   <!----------link external css page ---------> </head> <body> </body> </html>

Find Unique Number in Array Java , XOR

 Question:  WAP to find unique number in Array Java package aRray ; import java . util .*; public class practicalArray { public static void main ( String [] args ) { Scanner scan = new Scanner ( System . in ) ; // Step 1: Input size of the array from the user int size = scan . nextInt () ; // Step 2: Create an integer array of the specified size int [] arr = new int [ size ] ; // Step 3: Initialize 'res' with the first element of the array int res = arr [ 0 ] ; // Step 4: Loop to read array elements and find the unique number for ( int i = 1 ; i < size ; i ++ ) { // Read an integer from the user and store it in the array arr [ i ] = scan . nextInt () ; // Step 5: Use XOR (^) operation to find the unique number res = res ^ arr [ i ] ; } // Step 6: Print the unique number System . out . print...