-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNodesAtDepth.java
More file actions
117 lines (103 loc) · 2.25 KB
/
Copy pathNodesAtDepth.java
File metadata and controls
117 lines (103 loc) · 2.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
package com.learn.tree;
import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Deque;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;
public class NodesAtDepth {
public static void main(String[] args) {
// TODO Auto-generated method stub
//BinaryTree bt = new BinaryTree();
NodesAtDepth ndt = new NodesAtDepth();
Tree root = ndt.createTree();
ArrayList<LinkedList<Tree>> list = ndt.doBFS(root);
for(int i=0;i<list.size();i++)
{
List lst = list.get(i);
System.out.println("\n");
for(int j=0;j<lst.size();j++)
{
Tree node = (Tree) lst.get(j);
System.out.print(node.value+" ");
}
}
System.out.println("Tree Height =" +ndt.height(root));
}
public ArrayList<LinkedList<Tree>> doBFS(Tree root)
{
ArrayList<LinkedList<Tree>> listarray= new ArrayList<LinkedList<Tree>>();
LinkedList<Tree> list = new LinkedList<Tree>();
list.add(root);
listarray.add(list);
int size = 0;
while(listarray.size() > size)
{
LinkedList<Tree> templist = listarray.get(size);
LinkedList<Tree> newlist = new LinkedList<Tree>();
for(int i=0;i<templist.size();i++)
{
Tree node = (Tree) templist.get(i);
if(node.left!=null)
{
newlist.add(node.left);
}
if(node.right!=null)
{
newlist.add(node.right);
}
}
if(newlist.size()>0)
{
listarray.add(newlist);
}
size++;
}
System.out.println("Tree height = "+ (size-1));
return listarray;
}
public int height(Tree node)
{
int leftheight;
int rightheight;
if(node==null)
{
return -1;
}
else
{
//if(node.left!=null)
leftheight = height(node.left)+1;
//if(node.right!=null)
rightheight = height(node.right)+1;
}
if(leftheight>rightheight)
return leftheight;
else
return rightheight;
}
public Tree createTree()
{
Tree root = null;
BinaryTree bt = new BinaryTree();
for(int i=0;i<10;i++)
{
root = insert(root,(int)(Math.random()*100)%50);
}
return root;
}
public Tree insert(Tree node, int x)
{
if(node == null)
{
Tree node1 = new Tree(x);
return node1;
}
if(x<=node.value)
node.left = insert(node.left,x);
else
if(x>node.value)
node.right =insert(node.right,x);
return node;
}
}