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 = ...

SQL and rest python for Data analysis

SQL (Structured Query Language) की ओर — ये डेटा हैंडलिंग का अगला स्टेप है, जहाँ हम database से data को fetch, update, delete, filter, aur organize करना सीखते हैं। 💾 SQL क्या है (What is SQL)? SQL का मतलब है Structured Query Language — ये एक database language है जिसका इस्तेमाल data को store, access, और manage करने के लिए किया जाता है। जैसे Excel में data sheets होती हैं, वैसे SQL में tables होती हैं। Type Keyword Use 1️⃣ DDL (Data Definition Language) CREATE , ALTER , DROP Database structure change करने के लिए 2️⃣ DML (Data Manipulation Language) INSERT , UPDATE , DELETE Data change करने के लिए 3️⃣ DQL (Data Query Language) SELECT Data निकालने के लिए 4️⃣ DCL (Data Control Language) GRANT , REVOKE Permission देने या हटाने के लिए 5️⃣ TCL (Transaction Control Language) COMMIT , ROLLBACK Changes को confirm या cancel करने के लिए Download MY SQL From Google >>>Lets learn this concept compare with excel Concept in SQL Excel Equivalen...

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>