-
Notifications
You must be signed in to change notification settings - Fork 392
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #359 from shivamg77/patch-1
Create bstPre.java
- Loading branch information
Showing
1 changed file
with
77 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(" "); | ||
|
||
} | ||
} |