-
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
Showing
6 changed files
with
174 additions
and
3 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 |
---|---|---|
@@ -1 +1 @@ | ||
["home", "c", "python", "javascript"] | ||
["home", "c", "python"] |
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 |
---|---|---|
@@ -1 +1,80 @@ | ||
# C | ||
|
||
## Binary Search Tree | ||
|
||
```c | ||
#include <stdio.h> | ||
#include <stdlib.h> | ||
#include <stdbool.h> | ||
|
||
typedef struct node { | ||
int number; | ||
struct node *left; | ||
struct node *right; | ||
} node; | ||
|
||
bool search(node *tree, int number){ | ||
if (tree == NULL) return false; | ||
if (tree->number == number) return true; | ||
if (number < tree->number) return search(tree->left, number); | ||
else return search(tree->right, number); | ||
} | ||
|
||
int main(void) { | ||
// Create a sample binary search tree | ||
node *root = (node *)malloc(sizeof(node)); | ||
root->number = 5; | ||
root->left = (node *)malloc(sizeof(node)); | ||
root->left->number = 3; | ||
root->left->left = (node *)malloc(sizeof(node)); | ||
root->left->left->number = 1; | ||
root->left->left->left = NULL; | ||
root->left->left->right = NULL; | ||
root->left->right = (node *)malloc(sizeof(node)); | ||
root->left->right->number = 4; | ||
root->left->right->left = NULL; | ||
root->left->right->right = NULL; | ||
root->right = (node *)malloc(sizeof(node)); | ||
root->right->number = 7; | ||
root->right->left = (node *)malloc(sizeof(node)); | ||
root->right->left->number = 6; | ||
root->right->left->left = NULL; | ||
root->right->left->right = NULL; | ||
root->right->right = (node *)malloc(sizeof(node)); | ||
root->right->right->number = 8; | ||
root->right->right->left = NULL; | ||
root->right->right->right = NULL; | ||
|
||
// Test the search function | ||
int numbers_to_search[] = {1, 4, 6, 9}; | ||
int num_searches = sizeof(numbers_to_search) / sizeof(numbers_to_search[0]); | ||
|
||
for (int i = 0; i < num_searches; i++) { | ||
int number = numbers_to_search[i]; | ||
bool found = search(root, number); | ||
printf("Number %d is %s in the tree.\n", number, found ? "found" : "not found"); | ||
} | ||
|
||
|
||
free(root->right->right); | ||
free(root->right->left); | ||
free(root->right); | ||
free(root->left->right); | ||
free(root->left->left); | ||
free(root->left); | ||
free(root); | ||
|
||
return 0; | ||
} | ||
``` | ||
## Tree Structure Diagram | ||
``` | ||
5 | ||
/ \ | ||
3 7 | ||
/ \ / \ | ||
1 4 6 8 | ||
|
||
``` |
This file was deleted.
Oops, something went wrong.
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 |
---|---|---|
@@ -1 +1,64 @@ | ||
# Python | ||
|
||
## Dictionary Search | ||
|
||
Dictionary refers to a list of word keys without any values in a file separated by line breaks. The script loads the file, splits the set (collection of unique keys) at each line break, and runs a check in main to see if some test words are in the set. | ||
|
||
````py | ||
# dictionary.py | ||
words = set() | ||
|
||
def load(dictionary): | ||
with open(dictionary, "r") as file: | ||
words.update(file.read().splitlines()) | ||
return True | ||
|
||
def check(word): | ||
return word.lower() in words | ||
|
||
def size(): | ||
return len(words) | ||
|
||
def unload(): | ||
return True | ||
|
||
def main(): | ||
# Load the dictionary | ||
dictionary_file = "test_dictionary.txt" | ||
if load(dictionary_file): | ||
print(f"Loaded {size()} words from {dictionary_file}") | ||
else: | ||
print("Failed to load dictionary") | ||
return | ||
|
||
# Test some words | ||
test_words = ["apple", "banana", "carrot", "DATE", "fig"] | ||
for word in test_words: | ||
if check(word): | ||
print(f"'{word}' is in the dictionary") | ||
else: | ||
print(f"'{word}' is NOT in the dictionary") | ||
|
||
# Unload the dictionary | ||
if unload(): | ||
print("Dictionary unloaded successfully") | ||
else: | ||
print("Failed to unload dictionary") | ||
|
||
|
||
# if __name__ == "__main__" `main()` is only called if the script is run directly (not imported as a module). | ||
if __name__ == "__main__": | ||
main() | ||
|
||
|
||
# touch dictionary_file.txt | ||
# echo "apple" >> dictionary_file.txt | ||
# echo "banana" >> dictionary_file.txt | ||
# echo "cherry" >> dictionary_file.txt | ||
# echo "date" >> dictionary_file.txt | ||
# echo "elderberry" >> dictionary_file.txt | ||
# echo "fig" >> dictionary_file.txt | ||
# echo "grape" >> dictionary_file.txt | ||
# echo "honeydew" >> dictionary_file.txt | ||
```py | ||
```` |
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 |
---|---|---|
@@ -1 +1 @@ | ||
["container", "lldb"] | ||
["container", "lldb", "launch.json"] |
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,30 @@ | ||
```json | ||
{ | ||
"version": "0.2.0", | ||
"configurations": [ | ||
{ | ||
"name": "lldb", | ||
"type": "lldb", | ||
"request": "launch", | ||
"program": "${fileDirname}/${fileBasenameNoExtension}", | ||
"cwd": "${workspaceFolder}", | ||
"preLaunchTask": "clang++" | ||
}, | ||
{ | ||
"name": "python", | ||
"type": "python", | ||
"request": "launch", | ||
"program": "${file}", | ||
"console": "integratedTerminal", | ||
"cwd": "${fileDirname}" | ||
}, | ||
{ | ||
"name": "go", | ||
"type": "go", | ||
"request": "launch", | ||
"mode": "auto", | ||
"program": "${file}" | ||
} | ||
] | ||
} | ||
``` |