-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Create BinaryTreeImplementationUsingQueue.java
- Loading branch information
1 parent
3e0daa6
commit 330c435
Showing
1 changed file
with
61 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,61 @@ | ||
import java.util.Scanner; | ||
import java.util.LinkedList; | ||
import java.util.Queue; | ||
|
||
class Node{ | ||
int val; | ||
Node left; | ||
Node right; | ||
Node(int val){ | ||
this.val = val; | ||
this.left = this.right = null; | ||
} | ||
} | ||
|
||
class BinaryTreeImplementationUsingQueue{ | ||
static Node root; | ||
static Queue<Node> queue = new LinkedList<>(); //queue is only responsible for tracking nodes whose children need processing. | ||
static void insert(int[] nodes){ | ||
|
||
root = new Node(nodes[0]); | ||
queue.add(root); | ||
int i = 1; | ||
|
||
while(!queue.isEmpty()){ | ||
Node currentNode = queue.poll(); | ||
if(i<nodes.length){ | ||
currentNode.left = new Node(nodes[i++]); | ||
queue.add(currentNode.left); | ||
} | ||
if(i<nodes.length){ | ||
currentNode.right = new Node(nodes[i++]); | ||
queue.add(currentNode.right); | ||
} | ||
} | ||
} | ||
|
||
static void preorder(Node root){ // Root -> Left -> Right | ||
if(root == null){ | ||
return; | ||
} | ||
|
||
System.out.println(" " + root.val); | ||
preorder(root.left); | ||
preorder(root.right); | ||
} | ||
|
||
public static void main(String[] args){ | ||
Scanner input = new Scanner(System.in); | ||
System.out.println("Enter the number of nodes: "); | ||
int numOfNodes = input.nextInt(); | ||
System.out.println("Enter the nodes: "); | ||
int[] nodes = new int[numOfNodes]; | ||
for(int i = 0; i<numOfNodes; i++){ | ||
nodes[i] = input.nextInt(); //10 5 -1 3 7 13 20 | ||
} | ||
insert(nodes); | ||
preorder(root); | ||
|
||
input.close(); | ||
} | ||
} |