-
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.
Estudos de árvores binárias e introdução à tabela hash com listas
- Loading branch information
1 parent
27ec225
commit e3bc29a
Showing
2 changed files
with
214 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,147 @@ | ||
#include <stdio.h> | ||
#include <stdlib.h> | ||
|
||
// Binary tree implementation with linked list | ||
typedef struct treenode { | ||
int data; | ||
struct treenode *left, *right; | ||
} treenode; | ||
|
||
// function to create a new root | ||
treenode *createnode(int data) { | ||
|
||
// Alloc a new space in memory to root | ||
treenode *new = malloc(sizeof(treenode)); | ||
new->left = NULL; | ||
new->right = NULL; | ||
new->data = data; | ||
|
||
return new; | ||
|
||
} | ||
|
||
// Inserting elements in a binary tree | ||
treenode *insert_node(treenode *node, int data) { | ||
|
||
// Checking if the tree is empty | ||
if(node == NULL) return createnode(data); | ||
|
||
// If isn't empty, now check if de data is lesser than root | ||
if(data < node->data) | ||
node->left = insert_node(node->left, data); | ||
// If isn't empty. now check if de data is greater than root | ||
else if(data > node->data) | ||
node->right = insert_node(node->right, data); | ||
|
||
// Return the node with the new leaf | ||
return node; | ||
|
||
} | ||
|
||
// Printing elements in a tree pre-order | ||
void print_tree(treenode *node) { | ||
|
||
if(node != NULL) { | ||
|
||
printf("%d\n", node->data); | ||
print_tree(node->left); | ||
print_tree(node->right); | ||
|
||
} | ||
|
||
} | ||
|
||
// Searching elements in a tree | ||
treenode *search_tree(treenode *node, int value) { | ||
|
||
// If not found the value | ||
if(node == NULL) return NULL; | ||
|
||
// If found the value | ||
if (value == node->data) | ||
return node; | ||
|
||
// Same logic in printing, we search for values lesser or higher | ||
else if(value < node->data) | ||
return search_tree(node->left, value); | ||
else if (value > node->data) | ||
return search_tree(node->right, value); | ||
|
||
} | ||
|
||
// founding the min value (aux to delete_tree) | ||
treenode *find_min(treenode *node) { | ||
|
||
while(node->left != NULL) | ||
node = node->left; | ||
|
||
return node; | ||
|
||
} | ||
|
||
// Deleting a given node value | ||
treenode *delete_tree(treenode *node, int value) { | ||
|
||
// End of recursion or empty tree | ||
if(node == NULL) return node; | ||
|
||
// Choosing the direction to found the value to be deleted | ||
if(value < node->data) | ||
node->left = delete_tree(node->left, value); | ||
else if(value > node->data) | ||
node->right = delete_tree(node->right,value); | ||
|
||
else { | ||
// case 1 and 2, if the node has 0 or 1 child | ||
if(node->left == NULL) { | ||
treenode *temp = node->right; | ||
free(node); | ||
return temp; | ||
} else if (node->right == NULL) { | ||
treenode *temp = node->left; | ||
free(node); | ||
return temp; | ||
} | ||
|
||
// Third case: If the node has two children, we've to found the lesser | ||
treenode *temp = find_min(node->right); | ||
|
||
//copy the content of the sucessor for te actual node | ||
node->data = temp->data; | ||
|
||
// Now, delete de sucessor | ||
node->right = delete_tree(node->right, temp->data); | ||
|
||
} | ||
|
||
return node; | ||
|
||
} | ||
|
||
// Example using each function | ||
|
||
/* | ||
int main(void) { | ||
treenode *n1 = createnode(10); | ||
insert_node(n1, 8); | ||
insert_node(n1, 6); | ||
insert_node(n1, 9); | ||
insert_node(n1, 12); | ||
insert_node(n1, 11); | ||
insert_node(n1, 13); | ||
print_tree(n1); | ||
if(search_tree(n1,13)) | ||
printf("Found\n"); | ||
delete_tree(n1,10); | ||
print_tree(n1); | ||
} | ||
*/ | ||
|
||
|
||
|
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,67 @@ | ||
#include <stdlib.h> | ||
#include <stdio.h> | ||
|
||
/* | ||
To program a hash Table with chaining, we've to create | ||
the basic linked list functions | ||
but the insertion has to bem sorted | ||
*/ | ||
|
||
typedef struct node { | ||
int data; | ||
struct node *next; | ||
} node; | ||
|
||
|
||
// Sorted insertion and creation of linked list | ||
void sorted_insert(node *node, int x) { | ||
|
||
struct node *temp = node, *q = NULL, *new; | ||
new = malloc(sizeof(node)); | ||
new->data = x; | ||
new->next = NULL; | ||
|
||
if(node == NULL) | ||
node = new; | ||
else { | ||
while(temp && temp->data < x){ | ||
q=temp; | ||
temp=temp->next; | ||
} | ||
if(temp == node){ | ||
new->next = node; | ||
node = new; | ||
} | ||
else{ | ||
new->next=q->next; | ||
q->next=new; | ||
} | ||
} | ||
|
||
} | ||
|
||
// Searching in linked list | ||
node *search(node *node, int key) { | ||
|
||
while (node != NULL) { | ||
if(key == node->data) | ||
return node; | ||
node=node->next; | ||
} | ||
|
||
return NULL; | ||
|
||
} | ||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|