Tree traversal and height and depth Implementation
package DataStructure3;
import java.util.ArrayList;
import java.util.LinkedList;
class Node<E> {
E data;
LinkedList<Node> next;
int lebel;
}
public class TestTree {
public static void main(String[] args) {
Node root = new Node();
root.data = 1;
root.lebel=1;
root = add(root, 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, 3, 7);
root = add(root, 4, 8);
root = add(root, 5, 9);
root = add(root, 5, 10);
root = add(root, 6, 11);
root = add(root, 7, 12);
root = add(root, 13, 1);
print(root);
System.out.print("Traversal= ");
preOrder(root);
System.out.println();
System.out.println("Height/Depth= "+ height(root));
System.out.println("Ancestor= "+ancestor(root,2,3));
}
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;
n.lebel=root.lebel+1;
l.add(n);
root.next = l;
} else {
Node n = new Node();
n.data = data;
n.lebel=root.lebel+1;
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)));
}
}
}
public static void preOrder(Node root) {
if(root.data!=null){
System.out.print(root.data);
}
if (root.next != null) {
for (int i = 0; i < root.next.size(); i++) {
System.out.print("->");
preOrder(((Node) root.next.get(i)));
}
}
}
static int h=0;
public static int height(Node root) {
if(root.data!=null){
//System.out.print(root.data);
if(root.lebel>h){
h=root.lebel;
}
}
if (root.next != null) {
for (int i = 0; i < root.next.size(); i++) {
//System.out.print("->");
height(((Node) root.next.get(i)));
}
}
return h;
}
static String ancestor="";
public static <E> E ancestor(Node root, E data1, E data2){
if (root.next != null) {
for (int i = 0; i < root.next.size(); i++) {
if(((Node) root.next.get(i)).data==data1){
//System.out.println("Root-" + root.data + " Child-" + ((Node) root.next.get(i)).data);
if(findANC(root,data2)){
ancestor=root.data+"";
}
}
ancestor(((Node) root.next.get(i)), data1, data2);
}
}
return (E) ancestor;
}
public static <E> boolean findANC(Node root,E data){
if (root.next != null) {
for (int i = 0; i < root.next.size(); i++) {
if(((Node) root.next.get(i)).data==data){
return true;
}
findANC(((Node) root.next.get(i)), data);
}
}
return false;
}
}
import java.util.ArrayList;
import java.util.LinkedList;
class Node<E> {
E data;
LinkedList<Node> next;
int lebel;
}
public class TestTree {
public static void main(String[] args) {
Node root = new Node();
root.data = 1;
root.lebel=1;
root = add(root, 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, 3, 7);
root = add(root, 4, 8);
root = add(root, 5, 9);
root = add(root, 5, 10);
root = add(root, 6, 11);
root = add(root, 7, 12);
root = add(root, 13, 1);
print(root);
System.out.print("Traversal= ");
preOrder(root);
System.out.println();
System.out.println("Height/Depth= "+ height(root));
System.out.println("Ancestor= "+ancestor(root,2,3));
}
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;
n.lebel=root.lebel+1;
l.add(n);
root.next = l;
} else {
Node n = new Node();
n.data = data;
n.lebel=root.lebel+1;
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)));
}
}
}
public static void preOrder(Node root) {
if(root.data!=null){
System.out.print(root.data);
}
if (root.next != null) {
for (int i = 0; i < root.next.size(); i++) {
System.out.print("->");
preOrder(((Node) root.next.get(i)));
}
}
}
static int h=0;
public static int height(Node root) {
if(root.data!=null){
//System.out.print(root.data);
if(root.lebel>h){
h=root.lebel;
}
}
if (root.next != null) {
for (int i = 0; i < root.next.size(); i++) {
//System.out.print("->");
height(((Node) root.next.get(i)));
}
}
return h;
}
static String ancestor="";
public static <E> E ancestor(Node root, E data1, E data2){
if (root.next != null) {
for (int i = 0; i < root.next.size(); i++) {
if(((Node) root.next.get(i)).data==data1){
//System.out.println("Root-" + root.data + " Child-" + ((Node) root.next.get(i)).data);
if(findANC(root,data2)){
ancestor=root.data+"";
}
}
ancestor(((Node) root.next.get(i)), data1, data2);
}
}
return (E) ancestor;
}
public static <E> boolean findANC(Node root,E data){
if (root.next != null) {
for (int i = 0; i < root.next.size(); i++) {
if(((Node) root.next.get(i)).data==data){
return true;
}
findANC(((Node) root.next.get(i)), data);
}
}
return false;
}
}
Comments
Post a Comment