Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit f9606e2

Browse files
Binary tree implementation using arry
1 parent af965a7 commit f9606e2

File tree

2 files changed

+110
-0
lines changed

2 files changed

+110
-0
lines changed
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
package me.premaseem.datastructure.binaryTree.binaryTreeArray;
2+
3+
public class BinaryTreeArr {
4+
5+
int[] a;
6+
int lui =0; // last used index
7+
8+
public BinaryTreeArr(int size){
9+
a = new int[size +1];
10+
}
11+
12+
public boolean insert(int v){
13+
if (lui >= a.length-1){
14+
System.out.println("cannot insert");
15+
return false;
16+
}
17+
a[++lui] = v;
18+
return true;
19+
}
20+
21+
public int search(int v){
22+
for (int i = 1; i < lui; i++) {
23+
if(a[i] == v){
24+
return i;
25+
}
26+
}
27+
return -1;
28+
}
29+
30+
public boolean delete(int v){
31+
int s = search(v);
32+
if(s != -1){
33+
a[s] = a[lui--];
34+
return true;
35+
}
36+
System.out.println("could not find element");
37+
return false;
38+
}
39+
40+
public void pre(int i){
41+
if(i > lui){
42+
return;
43+
}
44+
// System.out.print("Pre order traversal ");
45+
System.out.print(" >> "+a[i]);
46+
pre(i*2);
47+
pre(i*2+1);
48+
}
49+
50+
public void post(int i){
51+
if(i > lui){
52+
return;
53+
}
54+
// System.out.print("Post order traversal ");
55+
pre(i*2);
56+
pre(i*2+1);
57+
System.out.print(" >> "+a[i]);
58+
}
59+
60+
public void in(int i){
61+
if(i > lui){
62+
return;
63+
}
64+
// System.out.print("In order traversal ");
65+
pre(i*2);
66+
System.out.print(" >> "+a[i]);
67+
pre(i*2+1);
68+
}
69+
70+
public void level(int i){
71+
for (int j = i; j <=lui ; j++) {
72+
System.out.print(">> "+ a[j]);
73+
}
74+
}
75+
76+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package me.premaseem.datastructure.binaryTree.binaryTreeArray;
2+
3+
public class BinaryTreeArrMain {
4+
5+
6+
public static void main(String[] args) {
7+
BinaryTreeArr bsta = new BinaryTreeArr(15);
8+
9+
// insert
10+
for (int i = 1; i <=15 ; i++) {
11+
System.out.println("inserted " +bsta.insert(i));
12+
}
13+
14+
15+
// search
16+
System.out.println("search result "+bsta.search(3));
17+
System.out.println("search result "+bsta.search(17));
18+
19+
// delete
20+
// System.out.println("delete result "+bsta.delete(7));
21+
System.out.println("delete result "+bsta.delete(29));
22+
23+
// pre order traversal
24+
bsta.pre(1);
25+
System.out.println();
26+
// In order traversal
27+
bsta.in(1);
28+
System.out.println();
29+
// Post order traversal
30+
bsta.post(1);
31+
System.out.println();
32+
}
33+
34+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /