Skip to content

Commit 20987e9

Browse files
committed
added bottom view of a tree in java
1 parent 69bba57 commit 20987e9

File tree

4 files changed

+142
-0
lines changed

4 files changed

+142
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
import java.util.*;
2+
import java.util.Map.*;
3+
import java.util.Queue;
4+
abstract class BottomView
5+
{
6+
/**
7+
* BINARY TREE NODE STRUCTURE
8+
**/
9+
private static class BTNode
10+
{
11+
private int data;
12+
private BTNode left;
13+
private BTNode right;
14+
15+
//constructors
16+
public BTNode(int data)
17+
{
18+
this.data=data;
19+
}
20+
//getters
21+
public BTNode getLeft()
22+
{
23+
return left;
24+
}
25+
public BTNode getRight()
26+
{
27+
return right;
28+
}
29+
public int getData()
30+
{
31+
return data;
32+
}
33+
//setters
34+
public void setLeft(BTNode left)
35+
{
36+
this.left = left;
37+
}
38+
public void setRight(BTNode right)
39+
{
40+
this.right = right;
41+
}
42+
}
43+
44+
/**
45+
* BOTTOM VIEW OF A TREE
46+
**/
47+
public static void bottomview(BTNode root)
48+
{
49+
/**
50+
* NEW quenode CLASS FOR A QUEUE
51+
**/
52+
final class quenode
53+
{
54+
public BTNode data;
55+
public int hd;
56+
public quenode(BTNode data,int hd)
57+
{
58+
this.data = data;
59+
this.hd = hd;
60+
}
61+
}
62+
Queue<quenode> Q = new LinkedList<quenode>();
63+
Q.add(new quenode(root,0));
64+
Map<Integer,Integer> m = new TreeMap<Integer,Integer>();
65+
66+
while(!Q.isEmpty())
67+
{
68+
quenode temp = Q.poll();
69+
70+
m.put(temp.hd, temp.data.getData());
71+
72+
if(temp.data.getLeft()!=null)
73+
{
74+
Q.add(new quenode(temp.data.getLeft(),temp.hd-1));
75+
}
76+
if(temp.data.getRight()!=null)
77+
{
78+
Q.add(new quenode(temp.data.getRight(),temp.hd+1));
79+
}
80+
}
81+
82+
//traversing the map
83+
for(Entry<Integer,Integer> entry: m.entrySet())
84+
{
85+
System.out.print(entry.getValue()+" ");
86+
}
87+
System.out.println();
88+
}
89+
90+
/**
91+
* TREE EXAMPLE GIVEN
92+
* 20 <- ROOT
93+
* / \
94+
* 8 22
95+
* / \ \
96+
* 5 3 25
97+
* / \
98+
* 10 14
99+
*
100+
* BOTTOM VIEW : 5 10 3 14 25
101+
* */
102+
public static void main(String[] args)
103+
{
104+
BTNode a = new BTNode(20);
105+
BTNode b = new BTNode(8);
106+
BTNode c = new BTNode(22);
107+
BTNode d = new BTNode(5);
108+
BTNode e = new BTNode(3);
109+
BTNode f = new BTNode(25);
110+
BTNode g = new BTNode(10);
111+
BTNode h = new BTNode(14);
112+
113+
BTNode root = a;
114+
a.setLeft(b);
115+
a.setRight(c);
116+
117+
b.setLeft(d);
118+
b.setRight(e);
119+
120+
e.setLeft(g);
121+
e.setRight(h);
122+
123+
c.setRight(f);
124+
bottomview(root);
125+
}
126+
}

Java/README.md

+5
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,11 @@
9898
* Breadth First Search
9999
* LEVEL ORDER
100100
* [ITERATIVE](Data-Structures/TREES/BINARY-TREE/LevelOrder.java)
101+
* MISC
102+
* Bottom View of a Tree
103+
* [ITERATIVE](Data-Structures/TREES/BINARY-TREE/MISC/BottomView.java)
104+
105+
##### BINARY SEARCH TREES
101106

102107
#### GRAPHS
103108

datastructures.md

+10
Original file line numberDiff line numberDiff line change
@@ -368,6 +368,16 @@ Indexer for Data Structures Lover
368368
* ITERATIVE
369369
* [JAVA](Java/Data-Structures/TREES/BINARY-TREE/LevelOrder.java)
370370

371+
#### MISC
372+
373+
##### BOTTOM VIEW OF A TREE
374+
375+
* blog
376+
* docs
377+
* implementation
378+
* [ITERATIVE](Java/Data-Structures/TREES/BINARY-TREE/MISC/BottomView.java)
379+
* [complexity](docs/complexity.md#binary-trees)
380+
371381
### GENERIC TREES
372382

373383
### THREADED BINARY TREE

docs/complexity.md

+1
Original file line numberDiff line numberDiff line change
@@ -579,6 +579,7 @@ SNo. | Operations | Order and Type of Time Complexity O(n) | Order and Type of S
579579
2 | Pre Order Traversal | O(n) -- Linear | O(n) -- Linear | :heavy_check_mark: | :heavy_check_mark:
580580
3 | Post Order Traversal | O(n) -- Linear | O(n) -- Linear | :heavy_check_mark: | :heavy_check_mark:
581581
4 | Level Order Traversal | O(n) -- Linear | O(n) -- Linear | :heavy_check_mark: | :x:
582+
5 | Bottom View of a Tree | O(n) -- Linear | O(n) -- Linear | :heavy_check_mark: | :x:
582583

583584
#### GENERIC TREES
584585

0 commit comments

Comments
 (0)