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

Python Final Lectures

 Q- how to Print Hello World print("Hello World") Variables in python ------- age = 30   #variable should be intutive so that we can learn any time print(age) Note: Shift+Enter is shortcut to run command 2) ' #' this is for writing the comment in python Rules for Variables--- Variable can not be start with any number like - 1age  Number can use in between and end with variable like - age1 age2 Special characters are not allowed expect _ (underscore) like - age_my Space not allowed in variable  Python is case sensitive  Way to define Variable --- age1,age2 = 30,25  age1 = 30 age2 = 25 age1=age2=30   #if 30 age for both variable   >> Data type the type of data is basically data type Integer = age1 to age3 is basically integer   , Integer is basically full number lets check = type(age1)  #it will give u print int float=  basically decimal values Interest =  30.24 type(Interest) #answer is float Message = ...

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>

Python

Indexing--  it will help u to fetch single character  string= "Python" string[2] # slicing process of fetching a sub-string from the given string  #sequence of charater we can fetch means more than 1 character string="Divyanshu" string[2:4+1]   #basically here number is index value #string[start_index:end+index+1] string = "Hunny"   #indexing agr positive hai to left se count hoga #right se negative me string[:]  #it will give u entire string #now If i want to any characterwise index like string is Hunny and I want un only #string[start_index:end_index+1:step] string[1:4:2] #reverse your string #string[:: -1] string="Baba hunny" string[:: -1] # to convert into lowecase string="New Divyanshu" new_string=string.lower()  #new_string becase we've to create another string print(new_string) s1={1,2,3,4,5}    s2={3,2,8,67,85} s3=s1.union(s2) s3   #isme add hota hai whole value lekin common value update nhi hongi #intersection - ...