Introduction to Generic Trees (N-ary Trees)
Generic Tree Node -----------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
Post a Comment