Skip to main content

Posts

Showing posts with the label Tree Data Structure In Java

Take Input recursive Tress

  import java.util. * ; public class TakeInput { public static Tree_Node < Integer > takeInput () { Scanner scan = new Scanner( System . in ) ; int n = scan .nextInt() ; //this is for root Tree_Node < Integer > root = new Tree_Node< Integer >( n ) ; int childNode = scan .nextInt() ; for ( int i = 0 ; i < childNode ; i ++){ Tree_Node < Integer > child = takeInput () ; root . children .add( child ) ; } return root ; } public static void main ( String [] args ) { takeInput () ; } }

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