Convert binary tree into simple linked list - c

struct Monitor {
int codMonitor;
char* producator;
float diagonala;
int numarPorturi;
};
struct nodls {
Monitor info;
nodls* next;
};
nodls* creareNod(Monitor m) { --create node
nodls* nou = (nodls*)malloc(sizeof(nodls));
nou->info.codMonitor = m.codMonitor;
nou->info.producator = (char*)malloc(sizeof(char)*(strlen(m.producator) + 1));
strcpy(nou->info.producator, m.producator);
nou->info.diagonala = m.diagonala;
nou->info.numarPorturi = m.numarPorturi;
nou->next = nou;
return nou;
}
nodls* inserare(nodls* cap, Monitor m) { -- insert
nodls* nou = creareNod(m);
if (cap == NULL) {
cap = nou;
cap->next = cap;
}
else
{
nodls* temp = cap;
while (temp->next != cap)
temp = temp->next;
temp->next = nou;
nou->next = cap;
}
return cap;
}
void afisareMonitor(Monitor m) { -- display struct
printf("\nMonitorul cu codul %d, producatorul %s, diagonala %f, numarul de porturi %d",
m.codMonitor, m.producator, m.diagonala, m.numarPorturi);
}
void traversare(nodls** cap) { --display function
nodls* temp = *cap;
if (cap == NULL)
printf("\nLista este goala");
while (temp->next != *cap) {
afisareMonitor(temp->info);
temp = temp->next;
}
afisareMonitor(temp->info);
}
void stergereNod(nodls* cap) --delete node function
{
.......
}
void dezalocare(nodls* cap) { free allocate space
............
}
How I can convert using the following code, my binary tree into a simple linked list. This can be done with recursion maybe.
getLeavesList(root) {
if root is NULL: return
if root is leaf_node: add_to_leaves_list(root)
getLeavesList(root -> left)
getLeavesList(root -> right)
}
So, if the root is NULL, this is, if the function received no valid pointer, then return an error message.
If the root is a leaf, this is, if both left and right child nodes are NULL, the you have to add it to the list of leaf nodes.
Then you recursively call the function with the left and right child nodes.

https://www.geeksforgeeks.org/tree-traversals-inorder-preorder-and-postorder/
This article covers 3 simple depth-first approaches(preorder, inorder, postorder) to traverse a binary tree. It also has a nice compact example in C.
The pseudo-code algorithm you provided is actually the proper preorder approach.
The only modification you have to make in the
void printPreorder(struct node* node)
method is to replace the printing of the node's data:
printf("%d ", node->data);
with checking if the node is a leaf and if yes adding it to a list:
if((node->left == NULL) && (node->right == NULL))
{
appendList(&node);
}
Of course appendList is just my ad-hoc creation but based on the code that you've provided in the question you'll know how to add to a linked-list. If not please feel free to ask.
ps: https://www.geeksforgeeks.org/ is an amazing resource hats of to those guys for the amazing work!
psps: If you'd like to return an error message when the function is called with NULL for the first time, that is if no valid tree is passed to the traversal, I'd suggest you to make a wrapper function that will call the recursive one. Then, in that wrapper you can check if the head passed to it is NULL before you even call the recursive method at all.

Related

Trying to change a maze pathfinder algorithm from using DFS to BFS to find the shortest path in C

I have created a pathfinding algorithm in C that uses a map.txt file with a maze written in 0s for walls and 1s for paths. Currently the path finding algorithm I'm using is a DFS with a stack to keep track of nodes but I'm trying to change it to a queue to utilize BFS so that I will be certain that the shortest path is always found. But when I try to print out the new path in BFS it seems like the rest of the path gets deleted somewhere but I can't find where.
Here is the path-finding algorithm in question:
struct node *BFS(struct graph* graph, int vertex, int endVertex, struct node* path_stack) {
struct node* temp = graph->adjlist[vertex]; //This is the first neighbor to the vertex node
struct node* temp2 = graph->adjlist[endVertex];// We use this temp to check if there exist a path at all (if the end node exist and has neighbours)
graph->visited[vertex] = 1; // sets the self as visited
static int done = 0;
if(temp2 == NULL || temp == NULL)
{
printf("No Path can be found");
return path_stack;
}
// if we havent arrived yet
while (temp != NULL) { // checks if the current neighbor exists
if(done == 1)
{
break;
return path_stack;
}
if(temp->vertex == endVertex) // Check for when we have gotten to the goal
{
printStack(path_stack);
done = 1;
break;
}
path_stack = push(temp, path_stack); // we push the first neighbor to the stack
if (graph->visited[temp->vertex] == 0) { //if the neighbor is not visited we run the algorithm again and now with the neighbors value as the vertex
BFS(graph, temp->vertex, endVertex,path_stack);
}
else
{
if(graph->visited[temp->vertex] == 1)
{
path_stack = pop(path_stack);
}
temp = temp->next; // we go the next neighbor to the current node
}
}
return path_stack;
}
Here is my push code that has been changed to push last like a queue:
struct node* push(struct node* Node, struct node* head)// Push has now been altered to push like a queue, so we can use bfs instead of dfs
{
struct node* temp = CreateNode(Node->vertex,Node->Y,Node->X); // We use a queue to keep track of visited nodes
temp->next = NULL;
if(head == NULL)
head = temp;
else
{
head->next = temp;
head = temp;
}
return head;
}
and here is my function that prints the path but now it only prints the last node in the path and all the others are gone.
void printStack(struct node* head)//Function that prints all the nodes in the stack
{
struct node* tmp = head;
//struct node* tmp2 = NULL;
printf("(The Start)->");
while(tmp != NULL)
{
//tmp2 = push_path(tmp, tmp2);
printf("(%d,%d)->", tmp->X, tmp->Y);
tmp = tmp->next;
}
/* while(tmp2 != NULL)
{
printf("(%d,%d)->", tmp2->X, tmp2->Y);
tmp2 = tmp2->next;
}
*/
printf("(The End)\n");
}
The code for printing the path has also been changed a little because when using DFS the path would be printed reversed so the code that has been commented out was for when I needed to reverse it back but now it's not necessary to use it. Any help with why the path can't be printed is very much appreciated, I'm still quite new to C programming and path finding algorithms.

kd-tree iterative non-stack based insertion

Im looking for iterative non-stack based (due to memory constraints, and minimal refactoring) kd-tree insertion only.
I have an entire kd-tree library working in C, functions (insert ,read, update, delete, knn search, rebalancing) I started to replace all recursive functions with iterative ones.
However, I noticed that my insertion was not working for some test data. Meaning actually the search could not find the inserted nodes when using iterative implementation but all data was found when using recursive implementation, , therefore, the bug is in iterative insert version.
node structure:
typedef struct kd_tree_node
{
struct kd_tree_node* left;
struct kd_tree_node* right;
struct kd_tree_node* parent;
float* dataset;
float distance_to_neighbor;
} kd_tree_node;
Below is an iterative insertion (i included parts directly related. No rebalacing logic, etc...):
void
kd_tree_add_record(kd_tree_node* root, const float data [], int depth,
const int k_dimensions,
const int copying, const float rebuild_threshold) {
/*rebalancing logic is NOT relevant, which I have NOT include, we can just build inefficient tree*/
/* is root empty? */
if (is_empty_node(root, k_dimensions)) {
root = kd_tree_new_node(data, k_dimensions, copying);
/*was the root set before*/
if (is_empty_node(kd_tree_get_root(), k_dimensions)) {
kd_tree_set_root(root);
}
} else {
/*iteratively insert new node*/
current = kd_tree_get_root();
/*while current is NOT null*/
while (!is_empty_node(current, k_dimensions)) {
parent = current;
/* Calculate current dimension (cd) of comparison */
cd = depth % k_dimensions;
/*determine current dimension/*/
/*by using modula operator we can cycle through all dimensions */
/* and decide the left or right subtree*/
median = kd_tree_get_column_median(cd);
//printf("kd_tree_add_record.(), median=%f\n",median);
if (data[cd] < median) {
current = current->left;
} else {
current = current->right;
}
depth++;
}//end while
/*should be inserted left or right of the parent*/
int insert_left = 1;
depth = 0;
if (!is_empty_node(parent,k_dimensions)) {
int c = 0;
for (; c < k_dimensions; c++) {
cd = depth % k_dimensions;
median = kd_tree_get_column_median(cd);
if (parent->dataset[cd] < median) {
} else {
insert_left = 0;
break;
}
depth++;
}
if (insert_left)
{
parent->left = kd_tree_new_node(data, k_dimensions, copying);
}
else
{
parent->right = kd_tree_new_node(data, k_dimensions, copying);
}
}
}//end else
}
I based my iterative kd-tree insert above code, by attempting to follow the iterative binary tree insert C++ code from: (https://www.techiedelight.com/insertion-in-bst/) which can be tested online, see below(note this not my code and its provided as reference):
void insertIterative(Node*& root, int key)
{
// start with root node
Node *curr = root;
// pointer to store parent node of current node
Node *parent = nullptr;
// if tree is empty, create a new node and set root
if (root == nullptr)
{
root = newNode(key);
return;
}
// traverse the tree and find parent node of key
while (curr != nullptr)
{
// update parent node as current node
parent = curr;
// if given key is less than the current node, go to left subtree
// else go to right subtree
if (key < curr->data)
curr = curr->left;
else
curr = curr->right;
}
// construct a new node and assign to appropriate parent pointer
if (key < parent->data)
parent->left = newNode(key);
else
parent->right = newNode(key);
}
Here is my previous kd-tree recursive insertion version, which works:
kd_tree_node *
kd_tree_add_record(kd_tree_node * root,
const float data[], int depth,
const int k_dimensions,
const int copying,
const float rebuild_threshold) {
float median = 0.0;
/* Tree is empty? */
if (NULL == root || NULL == root -> dataset || is_empty_node(root, k_dimensions)) {
root = kd_tree_new_node(data, k_dimensions, copying);
//update the root globally
if (kd_tree_get_root() == NULL) {
kd_tree_set_root(root);
}
} else {
/* Calculate current dimension (cd) of comparison */
size_t cd = depth % k_dimensions;
/*determine current dimension/*/
/*by using modula operator we can cycle through all dimensions */
/* and decide the left or right subtree*/
median = kd_tree_get_column_median(cd);
if (data[cd] < median) {
root -> left = kd_tree_add_record(root -> left, data, depth + 1,
k_dimensions,
copying, rebuild_threshold);
} else {
root -> right = kd_tree_add_record(root -> right, data, depth + 1,
k_dimensions,
copying, rebuild_threshold);
}
} //end else
return root;
}
current test results:
-53.148998,0.000000,9.000000 Found
7.999700,0.069812,8.000000 Found
7.998780,0.139619,8.000000 Found
7.997260,0.209416,8.000000 Not Found!
7.995130,0.279196,8.000000 Not Found!
7.992390,0.348955,8.000000 Not Found!
8.987670,0.471024,9.000000 Found
8.983210,0.549437,9.000000 Found
7.980510,0.558052,8.000000 Not Found!
3.000000,3.000000,3.000000 Found
4.000000,4.000000,4.000000 Found
5.000000,5.000000,5.000000 Found!
100.000000,100.000000,100.000000 Found
How can I extend the iterative non-stack binary insert algorithm to kd-trees?
Really appreciated!

Recursive Binary Tree Insert in C

So I am trying to learn how to create a binary tree in C so far I have got this.
void addRecordsToTree(struct date *in, struct date *root) {
if (root == NULL) {
root = malloc(sizeof(struct date));
root = in;
return;
} else {
//Right side of tree processing
if (compareTwoRecords(in, root) >= 0) {
addRecordsToTree(in, root->right);
return;
} else {
root->right = in;
return;
}
//Left side of tree processing.
if (compareTwoRecords(in, root) < 0) {
addRecordsToTree(in, root->left);
return;
} else {
root->left = in;
return;
}
}
}
int main() {
loadFiles();
struct date treeRoot;
struct date *old = malloc(sizeof(struct date));
old = loadContentsIntoHeap(files[file2014]);
addRecordsToTree(&old[0], &treeRoot);
addRecordsToTree(&old[1], &treeRoot);
addRecordsToTree(&old[2], &treeRoot);
addRecordsToTree(&old[3], &treeRoot);
addRecordsToTree(&old[4], &treeRoot);
addRecordsToTree(&old[5], &treeRoot);
printRecord(7, old);
return 0;
}
The problem is when I check the state of the program in a debugger there is just jumbled up data. I think it could be a type problem somewhere, I find pointers are bit of a mind boggling concept. Im not sure if I have used them right. So here is a screen shot of the debugger.
As you can see at the bottom struct called 'old' is the data I am trying to make the tree out of and treeRoot is where I am trying to place it but I can't understand why I get these garbage values.
Also what is up with the memory address of left and right? am I not creating them correctly.
Another observation I made is when I watch my code in the debugger it seems that root is never == NULL and never gets set, why?
You just did the following:
int x = 2;
int y = x;
y = 5;
Is the second line here necessary or the third one. It is a totally illogical program if you did this. You just did the same thing with a pointer instead of integer. You firstly had a pointer to the base address of dynamic memory then you just overwrote it by initializing it the second time.
And, the iterative approach is far better in comparison to the recursive one. I share the code for inserting a node in a binary tree both recursively and iteratively:
void insert(struct node *temp, struct node **root)
{
while (*root != NULL)
root = (*root)->element < temp->element ? &(*root)->left : &(*root)->right;
*root = temp;
}
#if 0
/* Recursive approach */
void insert(struct node *temp, struct node **root)
{
if(*root == NULL)
*root = temp;
else if ((*root)->element < temp->element)
insert(temp, &(*root)->left);
else
insert(temp, &(*root)->right);
}
#endif
void create_node(int x, struct node **root)
{
struct node *temp = (struct node *) malloc(sizeof(struct node));
if (temp == NULL)
printf("Unable to allocate memory. Free some space.\n");
else
{
temp->left = NULL;
temp->right = NULL;
temp->element = x;
insert(temp, root);
}
}
int main()
{
struct node *root = NULL;
create_node(1, &root);
create_node(2, &root);
create_node(3, &root);
return 0;
}
I saw an additional Problem in your "addRecordsToTree"-function:
the IF-block of the
"//Right side of tree processing"
will allways return from the function. regardless wether the "IF"-Expression is true or false.
So your left-leaves of thew tree will never be inserted. So you probalby should check/debug that function.

Find max and min value of BST

Hello stackoverflowers,
i am facing a problem with my function in C, i want to create a function that give me the min and max value in BST.
The problem is when i use this function it returns the same value for min and max:
void Find_Min_Max(node *bt,int* maxint,int* minint)
{
node *tmp = bt;
if( bt == NULL)
{
*maxint = 0; // Only if the tree contains nothing at all
*minint = 0; // Only if the tree contains nothing at all
}
if( bt->left)
return Find_Min_Max(bt->left,&(*maxint),&(*minint));
*minint = bt->data;
if( tmp->right)
return Find_Min_Max(tmp->right,&(*maxint),&(*minint));
*maxint = tmp->data;
}
But when i use it to give me just one result max/min, i delete this part of code, everything work perfectly:
if( tmp->right)
return Find_Min_Max(tmp->right,&(*maxint),&(*minint));
*maxint = tmp->data;
Any idea how this will work?.
Thank you in advance.
It's not really easy / intuitive to recursively compute max and min at the same time in the same function. I would even say it's not possible, because those are two completely different traversals.
You should have a function to get the minimum, a function to get the maximum, and call each of them inside Find_Min_Max.
This would be a possible approach:
int find_min(node *n) {
if (n == NULL) {
return 0;
}
while (n->left != NULL) {
n = n->left;
}
return n->data;
}
find_max is similar, but traverses right links only:
int find_max(node *n) {
if (n == NULL) {
return 0;
}
while (n->right != NULL) {
n = n->right;
}
return n->data;
}
Then, find_min_max() is easy to code:
void find_min_max(node *bt, int *min, int *right) {
*min = find_min(bt);
*max = find_max(bt);
}
find_min() and find_max() could be recursive, but the iterative approach has the desirable property of using constant memory (and consequently avoids stack overflows).
To find the minimum value in a BST, you follow the chain of left children from the root until you reach a node with no left child. That node contains the minimum value (even if it does have a right child).
The algorithm to find the maximum is exactly the mirror image: follow the chain of right children until you reach a node with no right child. That node contains the maximum value.
It does not make sense to try to perform both traversals at the same time, because they follow completely different paths. If you want a single function to discover both the minimum and the maximum value, then it doesn't make much sense for that function itself to be recursive. It could, however, wrap calls to two separate recursive functions, one to find the minimum, and the other to find the maximum.
Finding min and max value in a BST are very easy. Please check both code snippet below I explain how these codes work.
public int minValueInBST(Node node){
if (node == null) throw new IllegalStateException();
Node current = node;
while (current.leftChild != null) {
current = node.leftChild;
}
return current.value;
}
To find the minimum value in BST we have to find the leftmost leaf node because that node contains the minimum value. So at first, we check the root node is null or not if null we throw IllegalStateException otherwise we find the left node, at last, we return the left node value.
public int maxValueInBST(Node node){
if (node == null) throw new IllegalStateException();
Node current = node;
while (current.rightChild != null) {
current = node.rightChild;
}
return current.value;
}
To find the maximum value in BST we have to find the rightmost leaf node because that node contains the maximum value. So at first, we check the root node is null or not if null we throw IllegalStateException otherwise we find the right node, at last, we return the right node value.
// try this
tree_node *min(tree_node *root)
{
if (!root)
{
printf("Tree is empty");
exit(1);
}
tree_node *ret_val;
if (root->left == NULL)
{
ret_val = root;
}
else
{
ret_val = min(root->left);
}
return ret_val;
}
tree_node *max(tree_node *root)
{
if (!root)
{
printf("Tree is empty");
exit(1);
}
tree_node *ret_val;
if (root->right == NULL)
{
ret_val = root;
}
else
{
ret_val = max(root->right);
}
return ret_val;
}
complete code

Linked List of BST in C: Breadth First Search

I am writing a program that is a linked list of binary search trees. We are supposed to search for a number in the trees and print the tree and line number found. Because of this, we are supposed to use a Breadth-First Search function. I am getting a segmentation fault in my dequeue function and I am unsure why.
These are my structures:
typedef struct BST {
int value;
int treeNum;
struct BST* left;
struct BST* right;
}BST;
typedef struct rootList{
struct BST* root;
struct rootList* next;
}rootList;
typedef struct bfsQ{
struct BST* treeNode;
struct bfsQ* next;
}bfsQ;
This is my BFS function:
void BFS(rootList* listHead, int searchValue)
{
if(listHead->root == NULL)
{
printf("%d/tNO/tN/A/tN/A\n", searchValue);
}
else
{
bfsQ* newQueue = NULL;
BST* temp = NULL;
newQueue = malloc(sizeof(bfsQ));
newQueue->next = NULL;
enqueue(&newQueue, listHead->root);
while(newQueue != NULL)
{
temp = dequeue(&newQueue);
printf("Did this work?");
if(temp->value == searchValue)
printf("HI I WAS FOUND");
else
{
if(temp->left != NULL)
enqueue(&newQueue, temp->left);
if(temp->right != NULL)
enqueue(&newQueue, temp->right);
}
}
BFS(listHead->next, searchValue);
}
}
This is my enqueue:
void enqueue(bfsQ** qHead, BST* new_tree_node)
{
bfsQ *temp = malloc(sizeof(bfsQ));
BST *node;
temp->treeNode = new_tree_node;
temp->next = *qHead;
*qHead = temp;
node = temp->treeNode;
printf("%d\n", node->value);
}
This is my dequeue:
BST* dequeue(bfsQ** qHead)
{
bfsQ *temp, *first;
BST *newBST;
temp = (*qHead);
while(temp->next != NULL)
{
printf("THIS IS NOT NULL YET\n");
temp = temp->next;
}
first = temp;
newBST = first->treeNode;
free(temp);
return first->treeNode;
}
What am I doing wrong? The enqueue is working correctly, however my dequeue is not storing correctly.
EDIT: Apparently I need to:
"This function implements a variant of a level by level search or formally
called as the BREADTH FIRST SEARCH.
-> This function searches for a given value in the binary trees and it does that
by searching for level 1 in each binary trees, then moving on to level 2 if
it fails to find it that value in level 1 and so on.
-> Basically, you have to search for a given value in all the binary trees, one
level at a time, in the linked list simultaneously."
So I'm not sure if I need to search the whole tree, then move on, or look at each tree, line by line.
From the superficial look I had into the code, it looks generally ok (though I would have implemented some parts differently), but the last lines in dequeue() are certainly wrong:
first = temp;
newBST = first->treeNode;
free(temp);
return first->treeNode;
Accessing first->treeNode in the last line is catastrophic: first holds an address that has already been freed (temp and first refer to the same memory location). I think you wanted to return newBST instead:
return newBST;
You might as well throw first away, as it seems useless, and turn that into:
newBST = temp->treeNode;
free(temp);
return newBST;

Resources