Skip to content

Commit

Permalink
Merge pull request #359 from shivamg77/patch-1
Browse files Browse the repository at this point in the history
Create bstPre.java
  • Loading branch information
tanus786 authored Oct 21, 2022
2 parents 89382ed + fcc4bb7 commit 4cd38e0
Showing 1 changed file with 77 additions and 0 deletions.
77 changes: 77 additions & 0 deletions Advanced data structures/bstPre.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
public class bstPre {

static Node root;

static class Node {
int data;
Node left, right;

Node(int data) {
this.data = data;
left = null;
right = null;
}
}

Node insert(Node node, int value) {
if (node == null) {
return new Node(value);
}

if (value < node.data) {
if (node.left != null) {
insert(node.left, value);
} else
node.left = new Node(value);
} else if (value > node.data) {
if (node.right != null) {
insert(node.right, value);
} else
node.right = new Node(value);
}
return node;
}

void add(int data) {
root = insert(root, data);
}

void displayInOrder(Node node) {
if (node == null) {
return;
} else {
displayInOrder(node.left);
System.out.print(" " + node.data);
displayInOrder(node.right);
}
}

void displayPostOrder(Node node) {
if (node == null) {
return;
} else {
displayPostOrder(node.left);
displayPostOrder(node.right);
System.out.print(" " + node.data);
}
}

public static void main(String args[]) {
bstPre bt = new bstPre();

bt.add(50);
bt.add(30);
bt.add(10);
bt.add(40);
bt.add(80);

System.out.println("InOrder:");
bt.displayInOrder(root);
System.out.println("");
System.out.println("PostOrder:");

bt.displayPostOrder(root);
System.out.println(" ");

}
}

0 comments on commit 4cd38e0

Please sign in to comment.