convert a binary search tree to doubly linklist using recursion - c

As mentioned in the Title , I have a Binary search tree. I want to convert it to sorted doubly linklist using recursion.
My code
for each node in tree
find max of left sub-tree and assign its right to present node ,present node left to max
find min of right sub-tree and assign its left to present node ,present node right to max
and now recursively do same thing to other nodes in BST .
but this solution is not efficient as it reaches each node more than one time .In my quest of optimized code i got a link from google greatTreeList sollution . I have searched the same in SO both sollutions are same and worked for me. I didnt understand the append function of the sollution as it contains code
join(alast,b)
join(blast,a)
For tree whose nodes are inserted in following order 10,5,9,6,8,7,12,11,13,14
can anyone please explain how
join(alast,b)
join(blast,a)
are linking node in each recursion call.

I think you are over thinking this actually quite easy task - extracting the data from a binary tree in order is as simple as doing a depth first traversal - this is the point of a binary tree that it very efficiently gives you the elements in the sorted order.
So what you need to do is a standard depth first walk of the tree and each time you find a node add it to you linked list.
This in order depth first recursion is fairly straight forward is pseudocode:
Traverse(Node N)
Traverse(N.left);
Add N to the linked list
Traverse(N.right);
I suggest you try this manually on you example so you see how it works.

In order to convert a binary search tree to a sorted doubly linked list, one typically performs an inorder depth-first traversal, building the list along the traversal.
Try to come up with code that performs an inorder depth-first traversal and prints the items of the binary tree in sorted order. From there, it should be easy to complete your task.

Expanding on Elemental's answer
Traverse(Node N)
Traverse(N.left);
Add N to the linked list
Traverse(N.right);
To add N to the linked list,
your linked list class or data structure should have an append() method or similar.
Use your imagination, but something like this:
def append(N):
new_tail = node(val=N, prev=self.tail, next=None)
self.tail.next = new_tail
self.tail = new_tail
of course you also need to add self.head = self.tail the first time you append.

Related

Can we delete avl tree node in this way

So I was studying AVL trees and came across deleting a particular node from the tree. It was done by deleting a node similar to a BST and then balancing the height difference factor. But if we have the array which contains the order of inserted elements and we have a node to be deleted. I delete the occurrence of that node in the array and then construct AVL from scratch. Can this be a good way to do the deletion?
Of course you can delete this way. But the real question when discussing algorithms is what is the complexity of that?
The complexity of the standard algorithm of deletion from an AVL tree is o(lg(n)) - you can find explanations online everywhere to that fact.
Now let's look at your method - the complexity of converting the AVL tree to a sorted array would take O(n) using inorder traversal. Than constructing the AVL tree from a sorted array is O(n).
So in the bottom line, this method is just less efficient.

Theoretical: Most efficient way to implement a function that involves searching for multiple binary tree nodes

I'm trying to find the best way to implement a function that takes in a binary search tree and a specific phrase/word and returns a linked list.
So within my program, I have successfully created a binary search tree where each node contains a char *string, int value, and left/right struct pointers. Moreover, the nodes within this tree have been inserted based on the alphabet with strcmp. There are no duplicate strings within the tree.
My problem lies in the fact that the linked list, which needs to be returned, has to contain any tree node that matches the given search word OR holds the search word as a substring.
Based on this, I've been struggling to code any type of recursive function that is able to help find more than one node. Using ststr and a simple depth-first traversal will only return the first node that it finds.
I have tried passing in a 2D array of some sort but it just gets really messy and annoying.
So is there any other way to tackle a problem like this?
I'm relatively new to C and programming in general so I'm just looking for any ideas or pseudocode that might help me out.
Thank you,

How to make a Binary search tree using array but not using linked list?

I am able to make a binary search tree using linked list. But I can not able to make it using linear array.
Just a general direction -
I would use the array indexes as the children - like a heap data structure.
for vertex at index i, children nodes are at 2i+1 (left chiled) and 2i+2 (right chiled).
father node is at [i-1)/2)].
good luck

Efficiently search a linked list

I have a sorted linked list, one function takes the root pointer and an Id and searches through the list to delete the item, however this takes linear time.
Is there any way to efficiently search through the linked list without doing it linearly using the fact that its sorted? The assignment hints heavily that their is a better way but I can't think of a way to do it without being terribly inefficient or just using a linear search.
Each node in a linked list only contains a pointer to the next node (and optionally the previous node), so without any other constructs the only way to search the list is linearly.
However, since you sort the list, you could build a binary tree in the process. Then you can use that tree to search the list with a time complexity of O(log n) instead of O(n).

Need Exercise to solve based on Linear Linked List

I have completed implementing Operation of Linear Linked List using C,
Now inorder to test my ability i need to solve some problems based on Linear Linked List, and there you people can help me by suggesting some problems/assignments ...
I think there is nothing wrong in asking this type of help from my community members .
Determine whether the linked list contains cycle or not.
In the circular linked list add new node at the end without traversing the list.
Reverse the list.
Print the nodes in the list in reverse order without reversing the list.
Make a list Circular linked list.
Sort the list.
Merge two sorted lists.

Resources