forked from jainaman224/Algo_Ds_Notes
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
b24d034
commit 8d5c048
Showing
1 changed file
with
139 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,139 @@ | ||
import 'dart:io'; | ||
|
||
// Class to define the node of a binary tree. | ||
class Node { | ||
|
||
int value; | ||
Node left; | ||
Node right; | ||
|
||
Node(int value) { | ||
|
||
this.value = value; | ||
this.left = null; | ||
this.right = null; | ||
|
||
} | ||
|
||
} | ||
|
||
// Class to define a binary tree. | ||
class Tree { | ||
|
||
// The tree's root. | ||
Node root; | ||
|
||
// Constructor | ||
Tree(Node root) { | ||
this.root = root; | ||
} | ||
|
||
// Insert method to build the tree using recursion. | ||
Node insert(Node root, int val) { | ||
|
||
if (root != null){ | ||
|
||
// Inserting in the left side if value is less than root. | ||
if (val < root.value) { | ||
root.left = insert(root.left, val); | ||
} | ||
else { | ||
// Inserting in the right side if value is greater than root. | ||
if (val > root.value) { | ||
root.right = insert(root.right, val); | ||
} | ||
// Informing user that the value is already present. | ||
else{ | ||
print("Value is already present in the tree."); | ||
} | ||
} | ||
} | ||
else{ | ||
// Insertion | ||
root = Node(val); | ||
} | ||
|
||
return root; | ||
|
||
} | ||
|
||
// Utility method used to print the preorder traversal using recursion. | ||
void preorder_utility(Node node) { | ||
|
||
if (node != null) { | ||
print(node.value); | ||
|
||
// Recursion | ||
preorder_utility(node.left); | ||
preorder_utility(node.right); | ||
} | ||
|
||
} | ||
|
||
// Method to print the preorder traversal of the binary tree. | ||
void tree_preorder_traversal(){ | ||
|
||
print("The preorder traversal of the tree in the given input is follows:"); | ||
preorder_utility(this.root); | ||
|
||
} | ||
|
||
} | ||
|
||
// Driver method of the program | ||
void main(){ | ||
|
||
// Reading user input | ||
print("Enter root of the tree:"); | ||
var input = stdin.readLineSync(); | ||
int root_val = int.parse(input); | ||
|
||
// Root node | ||
Node root_node = Node(root_val); | ||
|
||
// Binary Tree | ||
Tree tree = Tree(root_node); | ||
|
||
String choice = "y"; | ||
|
||
while (choice == "y") { | ||
|
||
print("Enter number to insert:"); | ||
var input3 = stdin.readLineSync(); | ||
int insert_val = int.parse(input3); | ||
tree.insert(root_node, insert_val); | ||
|
||
print("Do you want to insert another number into the tree? (y/n)"); | ||
var input2 = stdin.readLineSync(); | ||
choice = input2; | ||
|
||
} | ||
|
||
/* Sample Input tree | ||
8 | ||
/ \ | ||
3 12 | ||
/ \ / \ | ||
2 6 9 15 | ||
/ \ | ||
5 7 | ||
*/ | ||
|
||
// Calling method to print preorder traversal of the tree. | ||
tree.tree_preorder_traversal(); | ||
|
||
/* Sample Output | ||
The preorder traversal of the tree in the given input is follows: | ||
8 | ||
3 | ||
2 | ||
6 | ||
5 | ||
7 | ||
12 | ||
9 | ||
15 | ||
*/ | ||
|
||
} | ||
|