How to make binary search tree in MATLAB [closed] - binary-tree

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
I am trying to make a binary search tree in MATLAB just like how you would do it in java or C++, and fill the nodes with characters. I understand that the nodes can be made with a struct, but I can't understand how to connect them so that it becomes a binary tree.
My struct has the following fields: left (for the node with the lower value), value, and right (for the node with a higher value).

You should use an array of your struct, and use the array index instead of a pointer. So with your struct:
node = struct('left',[],'right',[],'value',0);
you can add a node like so:
node(2).value = 1;
node(1).left = 2; % the index of the new node
creating a tree where node(2) is the left child of node(1).
isempty(node(i).left) means that node i has no left child.

Related

Is there any algorithm to achieve some optimization for hanger placement? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 months ago.
Improve this question
I need to do a job where I need to place a particular object(Hanger) in a standard distance.
The rules are:
We should try to place each object in a given standard distance from each other.
There is a max distance from one object to adjacent object which in no way should be violated.
From the start and end also similar standard and maximum distance rule applies.
And there are some portions given where the objects placement needs to be avoided.
I'm not even able to start... which algorithm to use.
If anyone has any suggestion how I can achieve this or some related source please let me know.

Insert element at index and Delete element by value in a Array [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
What is the fastest way to insert an element at the index and Delete an element by value in a large Array? I can loop over the contents of the array (find the location and then shift all the elements) but it will give complexity of o(n).
Is there a built-in that can help shift the elements in an array/slice? or if there is another data structure that can help?
[Is there a] Go[...] datatype similar to List in Python [?]
No, there is not.

Generators v/s Returning a List - Python [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I was writing a program in Python which returns / retrieves all the adjacent elements in a given list. So I developed two functions:
One that stores all such adjacent values in a list and returns the list:
def ret_adj(data):
#adjs = []
visited = []
for i in data:
if i in visited:
#adjs.append(i)
yield i
else:
visited.append(i)
if data[data.index(i)+1] != i:
del visited[:]
#return adjs
And one that yields the values:
if i in visited:
#adjs.append(i)
yield i
else:
visited.append(i)
if data[data.index(i)+1] != i:
del visited[:]
So my question basically is, which method is more efficient? Returning all values as a list, or yielding them?
Typically, a generator will be more efficient, especially for large data sets.
On the other hand, if your usual method of processing the data is to immediately turn the generator into a list, then you would be better off just returning the list in the first place.

Sort linked list in place [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
I have a struct type node:
struct node
{
int data;
node *next;
};
I have a linked list using this node structure. I can sort this linked list using another linked list and copying node values there, but I want to implement a selection sort in the linked list. I have gone through some questions and articles but it is not clear to me whether this is possible. Do I need an index to implement selection sort? If it is possible then how can I proceed?
Selection sort is meant for arrays, vectors, things that have random access iterators and no links like next pointers, (the links create an overhead when swapping elements). For a simple list sort, start with an empty list that will become the sorted list, say this is struct node *sorted = NULL; . Remove nodes one at at time from the original list and insert them into the sorted list (that starts off as an empty list) in sorted order.
Write some code and update your answer with what you've tried so far.

Why the leaf node in red black tree is NIL? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I feel confused when learning Red Black Tree,actually I don't know why we need NIL as leaf in the tree and it seems useless.Could any one can help me?Thanks in advance:)
The main reason it has NIL is to always balance the tree, two children per parent for example. If it wasn't properly balanced, in the worst case it would just be a glorified linked list not getting the benefits of a red black tree.

Resources