Skip to content

Instantly share code, notes, and snippets.

@neer2808
Created April 12, 2022 06:14
Show Gist options
  • Save neer2808/bd5d25ea892162947892e72e11a4acfd to your computer and use it in GitHub Desktop.
Save neer2808/bd5d25ea892162947892e72e11a4acfd to your computer and use it in GitHub Desktop.
// Binay tree by using the Array
package BTree;
public class BTest {
int arr[];
int lastusedindex;
public BTest(int size) {
arr = new int[size+1];
this.lastusedindex = 0;
}
public void insert(int val)
{
if(arr.length-1 == lastusedindex)
{
System.out.println("Array is full");
}
else
{
arr[lastusedindex+1]= val;
lastusedindex++;
}
}
public void traversepreorder(int index)
{
if(index>lastusedindex)
{
return;
}
else {
System.out.print(arr[index] + " ");
traversepreorder(index * 2);
traversepreorder(index * 2 + 1);
}
}
public void traverse(int index)
{
if(index>lastusedindex)
{
return;
}
else {
traverse(index * 2);
System.out.print(arr[index] + " ");
traverse(index * 2 + 1);
}
}
public static void main(String[] args) {
BTest obj = new BTest(5);
obj.insert(13);
obj.insert(34);
obj.insert(42);
obj.insert(12);
obj.insert(78);
obj.traverse(1);
System.out.println();
obj.traversepreorder(1);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment