Skip to content

Commit 13efa21

Browse files
authored
trees complete with insert, search and delete
1 parent 5bacaaf commit 13efa21

File tree

5 files changed

+106
-28
lines changed

5 files changed

+106
-28
lines changed

Diff for: Dsa.js

+5-8
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
11
// Problem Statement:
2-
// Given a binary tree, find the
3-
//maximum depth (height) of the tree.
4-
//The maximum depth of a binary tree is the
5-
// length of the longest path from the root node to any leaf node
2+
// Given a binary tree, find the maximum depth (height) of the tree. The maximum depth of a binary tree is the length of the longest path from the root node to any leaf node
63
class TreeNode {
74
constructor(value) {
85
this.value = value;
@@ -12,7 +9,7 @@ class TreeNode {
129
}
1310

1411
//javascript implementation
15-
// Helper function to create a binary tree for demonstration purposes
12+
// Helper function to create a binary tree for demonstration purposes
1613
function createBinaryTree() {
1714
const root = new TreeNode(1);
1815
root.left = new TreeNode(2);
@@ -32,16 +29,16 @@ function maxDepth(root) {
3229
return 0;
3330
} else {
3431
const leftDepth = maxDepth(root.left);
35-
// console.log(leftDepth)
36-
console.log("leftpath:", leftDepth);
32+
console.log("left depht is",leftDepth)
3733
const rightDepth = maxDepth(root.right);
38-
// console.log(rightDepth)
34+
console.log("right depht is",rightDepth)
3935
return 1 + Math.max(leftDepth, rightDepth);
4036
}
4137
}
4238

4339
// Example usage:
4440
const root = createBinaryTree();
41+
console.log("root is",root)
4542
const depth = maxDepth(root);
4643
console.log("Maximum Depth of Binary Tree:", depth); // Output: Maximum Depth of Binary Tree: 3
4744

Diff for: README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# WHAT ARE TREESS
22
A tree is a **hierarchical data structure** consisting of nodes connected by edges
33

4-
- Each node in a tree has a parent node (except for the root node) and zero or more child nodes. The topmost node in a tree is called the "root," and nodes with no children are called "leaves."
4+
- Each node in a tree has a parent node (except for the root node) and zero or more child nodes. The topmost node in a tree is called the "root," and nodes with no children are called "leafs."
55

66
[image visualization of a tree structure](https://media.geeksforgeeks.org/wp-content/uploads/20221124153129/Treedatastructure.png)
77

Diff for: binary.js

+81
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
// Define a Node class to represent each node in the binary search tree
2+
class Node {
3+
constructor(value) {
4+
this.value = value;
5+
this.left = null;
6+
this.right = null;
7+
}
8+
}
9+
10+
// Define the Binary Search Tree class
11+
class BinarySearchTree {
12+
constructor() {
13+
this.root = null;
14+
}
15+
16+
// Method to insert a new node into the BST
17+
insert(value) {
18+
const newNode = new Node(value);
19+
20+
if (this.root === null) {
21+
this.root = newNode;
22+
} else {
23+
this.insertNode(this.root, newNode);
24+
}
25+
}
26+
27+
insertNode(node, newNode) {
28+
if (newNode.value < node.value) {
29+
if (node.left === null) {
30+
node.left = newNode;
31+
} else {
32+
this.insertNode(node.left, newNode);
33+
}
34+
} else {
35+
if (node.right === null) {
36+
node.right = newNode;
37+
} else {
38+
this.insertNode(node.right, newNode);
39+
}
40+
}
41+
}
42+
43+
// Method to perform binary search
44+
search(target) {
45+
return this.searchNode(this.root, target);
46+
}
47+
48+
searchNode(node, target) {
49+
if (node === null || node.value === target) {
50+
return node;
51+
}
52+
53+
if (target < node.value) {
54+
return this.searchNode(node.left, target);
55+
} else {
56+
return this.searchNode(node.right, target);
57+
}
58+
}
59+
}
60+
61+
// Example usage:
62+
const bst = new BinarySearchTree();
63+
bst.insert(5);
64+
bst.insert(3);
65+
bst.insert(8);
66+
bst.insert(1);
67+
bst.insert(4);
68+
bst.insert(7);
69+
bst.insert(9);
70+
71+
// console.log(bst)
72+
73+
const targetValue = 10;
74+
const result = bst.search(targetValue);
75+
76+
if (result) {
77+
console.log(`Found ${targetValue} in the BST.`);
78+
} else {
79+
console.log(`${targetValue} not found in the BST.`);
80+
}
81+

Diff for: bst.js

+6-6
Original file line numberDiff line numberDiff line change
@@ -60,16 +60,16 @@ class Node {
6060

6161
// Example usage:
6262
const bst = new BinarySearchTree();
63-
bst.insert(5);
64-
bst.insert(3);
65-
bst.insert(8);
66-
bst.insert(1);
63+
bst.insert(5);
64+
bst.insert(3);
65+
bst.insert(8);
66+
bst.insert(1);
6767
bst.insert(4);
6868
bst.insert(7);
69-
bst.insert(9);
69+
bst.insert(9);
7070

7171

72-
const targetValue = 10;
72+
const targetValue = 7;
7373
const result = bst.search(targetValue);
7474

7575
console.log(bst)

Diff for: trees.js

+13-13
Original file line numberDiff line numberDiff line change
@@ -17,32 +17,32 @@ this.children.push(Childname)
1717
}
1818

1919
const familyTree = new Node("Grandpa");
20-
const dad = new Node("Dad");
21-
const mom = new Node("Mom");
22-
familyTree.addChildren(dad);
23-
// familyTree.addChildren(mom);
24-
const child1 = new Node("Judy");
20+
const dad = new Node("Dad");
21+
const mom = new Node("Mom");
22+
familyTree.addChildren(dad);
23+
familyTree.addChildren(mom);
24+
const child1 = new Node("Judy");
2525
const child2 = new Node("James");
26-
const child3 = new Node("Joyce");
27-
dad.addChildren(child1);
28-
// dad.addChildren(child2);
29-
// mom.addChildren(child3);
26+
const child3 = new Node("Joyce");
27+
dad.addChildren(child1);
28+
dad.addChildren(child2);
29+
mom.addChildren(child3);
3030

31-
console.log(familyTree.children[0].children[0].name)
31+
console.log(familyTree)
3232
// Function to print the family tree recursively
3333
function printFamilyTree(node, level = 0) {
3434
const indentation = " ".repeat(level); // Add spaces for indentation
3535

36-
// console.log(`${indentation}- ${node.name}`);
36+
console.log(`${indentation}- ${node.name}`);
3737

3838
for (const child of node.children) {
3939
printFamilyTree(child, level + 1);
4040
}
4141
}
4242

4343
// // Print the family tree
44-
// console.log("Family Tree:");
45-
// printFamilyTree(familyTree);
44+
console.log("Family Tree:");
45+
printFamilyTree(familyTree);
4646

4747
// console.log(familyTree)
4848

0 commit comments

Comments
 (0)