Tree with N child Java implementation
K- tree Implementation in java
Tree Pre-order traversal
--------------------------------------------------------------------------------------------------------------
Dynamic Parent Child
Tree Pre-order traversal
- package DataStructure3;
- import java.util.LinkedList;
- class Node<E> {
- E data;
- LinkedList<Node> next;
- }
- public class TestTree {
- public static void main(String[] args) {
- // TODO Auto-generated method stub
- /*
- * Node head=new Node(); head.data=1;
- *
- * Node c1=new Node(); c1.data=2;
- *
- * Node c2=new Node(); c2.data=21; LinkedList l=new LinkedList();
- * l.add(c1); l.add(c2); head.next=l;
- *
- * LinkedList l2=new LinkedList(); Node c3=new Node(); c3.data=3;
- * l2.add(c3);
- *
- * ((Node)head.next.get(0)).next=l2;
- *
- * for(int i=0;i<head.next.size();i++){
- * System.out.println(((Node)head.next.get(i)).data);
- *
- * }
- * System.out.println((((Node)((Node)head.next.get(0)).next.get(0)).data
- * ));
- */
- Node n = new Node();
- n.data = 1;
- Node root = add(n, 1, 2);
- root = add(root, 1, 3);
- root = add(root, 1, 4);
- root = add(root, 1, 5);
- root = add(root, 2, 6);
- root = add(root, 2, 7);
- root = add(root, 2, 8);
- root = add(root, 3, 9);
- root = add(root, 5, 10);
- root = add(root, 5, 11);
- root = add(root, 5, 12);
- root = add(root, 5, 13);
- root = add(root, 5, 14);
- print(root);
- /*
- * System.out.println("root--"+root.data); System.out.print("Child--");
- * for(int i=0;i<root.next.size();i++){
- * System.out.print(" "+((Node)root.next.get(i)).data);
- *
- * } System.out.println();
- * System.out.println("parent-"+((Node)root.next.
- * get(0)).data+" Child"+((
- * Node)((Node)root.next.get(0)).next.get(0)).data);
- */
- }
- public static <E> Node add(Node root, E parent, E data) {
- if (root.data == parent) {
- if (root.next == null) {
- LinkedList l = new LinkedList();
- Node n = new Node();
- n.data = data;
- l.add(n);
- root.next = l;
- } else {
- Node n = new Node();
- n.data = data;
- root.next.add(n);
- }
- return root;
- } else {
- if (root.next != null) {
- for (int i = 0; i < root.next.size(); i++) {
- add((Node) root.next.get(i), parent, data);
- }
- }
- }
- return root;
- }
- public static void print(Node root) {
- if (root.next != null) {
- for (int i = 0; i < root.next.size(); i++) {
- System.out.println("Root-" + root.data + " Child-"
- + ((Node) root.next.get(i)).data);
- print(((Node) root.next.get(i)));
- }
- }
- }
- }
--------------------------------------------------------------------------------------------------------------
Dynamic Parent Child
- package DataStructure3;
- import java.util.LinkedList;
- class Node<E> {
- E data;
- LinkedList<Node> next;
- }
- public class TestTree {
- public static void main(String[] args) {
- // TODO Auto-generated method stub
- /*
- * Node head=new Node(); head.data=1;
- *
- * Node c1=new Node(); c1.data=2;
- *
- * Node c2=new Node(); c2.data=21; LinkedList l=new LinkedList();
- * l.add(c1); l.add(c2); head.next=l;
- *
- * LinkedList l2=new LinkedList(); Node c3=new Node(); c3.data=3;
- * l2.add(c3);
- *
- * ((Node)head.next.get(0)).next=l2;
- *
- * for(int i=0;i<head.next.size();i++){
- * System.out.println(((Node)head.next.get(i)).data);
- *
- * }
- * System.out.println((((Node)((Node)head.next.get(0)).next.get(0)).data
- * ));
- */
- Node n = new Node();
- n.data = 1;
- Node root = add(n, 1, 2);
- root = add(root, 1, 3);
- root = add(root, 1, 4);
- root = add(root, 2, 5);
- root = add(root, 6, 1);
- print(root);
- /*
- * System.out.println("root--"+root.data); System.out.print("Child--");
- * for(int i=0;i<root.next.size();i++){
- * System.out.print(" "+((Node)root.next.get(i)).data);
- *
- * } System.out.println();
- * System.out.println("parent-"+((Node)root.next.
- * get(0)).data+" Child"+((
- * Node)((Node)root.next.get(0)).next.get(0)).data);
- */
- }
- public static <E> Node add(Node root, E parent, E data) {
- if(root.data==data){
- Node n = new Node();
- n.data = parent;
- LinkedList l = new LinkedList();
- l.add(root);
- n.next=l;
- return n;
- }
- if (root.data == parent) {
- if (root.next == null) {
- LinkedList l = new LinkedList();
- Node n = new Node();
- n.data = data;
- l.add(n);
- root.next = l;
- } else {
- Node n = new Node();
- n.data = data;
- root.next.add(n);
- }
- return root;
- } else {
- if (root.next != null) {
- for (int i = 0; i < root.next.size(); i++) {
- add((Node) root.next.get(i), parent, data);
- }
- }
- }
- return root;
- }
- public static void print(Node root) {
- if (root.next != null) {
- for (int i = 0; i < root.next.size(); i++) {
- System.out.println("Root-" + root.data + " Child-"
- + ((Node) root.next.get(i)).data);
- print(((Node) root.next.get(i)));
- }
- }
- }
- }
Comments
Post a Comment