Basic and Balance BST insertion node placement - data-structures

I have some questions about certain placement of child nodes since I'm just learning
BSTs and it's quite confusing even after reading some sources and doing some online insertion applets.
Let's say I want to add nodes 5,7,3,4 to an empty basic BST.
add 5
5
add 7
5
7
add 3
5
3 7
add 4
5
3 7
4
Ok I understand that the left child must be less than the parent AND less than or
equal to the right child from that same parent. I follow it until we add the 4 node. How
do we determine that the insertion of 4 goes to the bottom right leaf position of 3 instead of the bottom left leaf position?
Also, doing a AVL insertion of nodes 5,18,3,7,11 yielded some surprising position placements. Inserting the fourth node, 7, went down through 18 instead of 3. Is there a particular reason why? Assuming that is the correct way, inserting 11 would switch the 11 and 18 spots, but wouldn't having 18 as the parent node, 7 as left child, and 11 as right child adhere to the principle of left child smaller than parent and smaller or equal to right child? I'm confused! I would appreciate any help. Thank you!
insert 7
5
3 18
7
insert 11
5
3 11
7 18

In BSTs, elements in the left (right) subtree of any node are all less (greater) than the parent of the root of the subtree. So, in the left subtree of 5, both 3 and 4 are less than 5. Now look at 3. The reason 4 goes to the right, is because 4 is bigger than 3, so it goes right.
Same for your AVL question. 7 became the left child of 18, instead of the right child of 3 because 5 is the root. When doing an insert, you compare elements one level at a time. "Is 7 bigger than 5 (root)? Yes, go right. Is 7 bigger than 18? No, go left. No node to compare to, so 7 goes here".
Having 11 as the right child would not fit into a BST. 11 is smaller than 18, so it should be in the left subtree rooted at 18.

Related

Cyclic connected graph vs acyclic connected graph use case

I have a graph with such structure and rules:
Graph image
It is directed connected cyclic graph (direction from top to bottom nodes).
Any parent node can have 0, 1 or 2 children.
Child can have 1 or 2 parents (root node n_id_1 has no parents).
There are left child and right child. Parent can have right child and not have left child or vice versa (see n_id_9 and n_id_2).
If parent (n_id_9) has left branch (n_id_3 - n_id_8) and no right branch, it always becomes parent of child of left branch (n_id_11).
At this point i see 2 solutions for data structure:
Option 1. Make a directed connected cyclic graph like this (most obvious way):
parent children
1 2
2 3, 11
3 5, 6
5 9
6 4
9 10, 12
4 7
12 8
7 8
8 11
I think that downside of this - hard to render and to maintain.
Option 2. Make a directed connected acyclic graph in which every parent can have 3 children: left, right and third (don't know how to call it better). So for example n_id_2 has left child n_id_3, no right child and third child n_id_11. For missing children we should specify null.
parent children (left, right, third)
1 2, null, null
2 3, null, 11
3 5, 6, 8
5 9, null, null
6 4, null, null
9 null, 10, 12
4 7, null, null
12 null, null, null
7 null, null, null
8 null, null, null
It is much easier to render and walk through data since now we have a tree without cycles. Downside of this i see - if for some reason specs would be changed - it might be harder to change. I prefer this one.
So the questions are:
Are there any other solutions?
What do you think about my provided solutions?
Thank you for reviewing.
This really depends on what operations you need to perform on your graph. From your comment, you only need to edit the graph and not perform any expensive computation, so take the representation that suits you better from the usability perspective.
Personally, I would go with your second solution, which seems more natural for a graph with such properties, I would only describe it a little different:
The graph can be represented as a sequence of nodes, each node may have a left and right child branch and a successor (which is what you called third). Both child branches are optional; no successor means the end of the sequence. A child branch is again a sequence of the same kind of nodes.
This gives you a simple recursive structure, which I find easy to reason about (YMMV, of course).
As a side note: this graph is not cyclic, only it's undirected counterpart is.

Deleting a node in a binary tree having children

If we have to delete a node from a binary tree, how should we handle the children of the node being deleted. E.g. in this tree:
100
/
10
/ \
5 20
where 100 is the root node, 10 is left child of 100, 5 is left child of 10 and 20 is right child of 10. So after deleting 10, what will happen to 5 and 20?
It is your choice if what should you do after deleting.You would probably want to move one of the child upward on some so certain criteria. Either of of Child will take place of parent.
delete node in Binary Search Tree.
delete node in AVL.
Heapify in Heapsort.
And more.
So it is always a choice you have to take to solve for objective.

How many leaves are in a given heap and its level

I have a question and it goes like this:
There is a Max heap binary tree.
Let's assume that in the Heap there are (2^2017)-2017 nodes at the bottom-most level.
A)How many levels are there in the heap?
B)What are the number of the Leaves in the heap?
Thanks
A full binary heap that has 2x nodes at the bottom-most level has (x+1) levels. Consider this heap:
1
2 3
4 5 6 7
It has 4 (22) nodes at the bottom level, and 3 levels.
If the bottom level is full, then the number of leaf nodes is the same as the number of nodes on the bottom level.
In your case, the bottom level has (22017)-2017 leaf nodes. We know that the maximum number of nodes possible on this level is 22017 (because maximum is always a power of 2). And we also know, from the example above, that there are 2018 levels in the tree.
The number of leaf nodes, however, is not (22017)-2017. Consider this heap:
1
2 3
4 5 6 7
8 9
It has two leaf nodes at the bottom level, and three leaf nodes at the level above the bottom level (5, 6, and 7).
I think you'll find that in your case, the number of leaf nodes is (22017)-2017 + 2017/2. The 2017/2 is the number of leaf nodes on the level above.

How to build binary tree knowing only which nodes are connected?

I have to build a binary tree but i don't know which node is parent, left child, or right child. I only know which nodes are connected. Example: for input like this:
6 4
5 7
9 7
1 5
10 4
3 4
2 6
7 8
5 6
(from 1 there is always one path) the tree should looks like that:
One the input i have also given number of nodes. Any ideas, tips?
From the list of the edges, one can easily create a tree.
You can find which are the leaf nodes, just search which nodes are appearing only in one edge.
But, it is not possible to know which of the leaf nodes is the head of the tree.
In a tree structure it is possible to choose any leaf, choose it as the head, and reorder the tree, and it'll be a valid tree.
There is also the issue of tree isomorphism, you can swap the right and left sub-trees to get a valid tree.
To summarize, from this list you can get 6 heads and in each 4 possible swaps, so in total 24 different valid trees.

binary search tree diagram for numbers 1 to 10

How would i draw a binary search diagram for numbers 1 to 10?
Would it be split into two sub-trees or would it be 1 straight line with the nodes to the right of the parent node?
The diagram is to start with 1 as the parent node.
The value in the middle should be the root, this is to get the lowest height in your tree.
Then from there just write two pointers on each node one for the lower and one for the greater and so on, until you get to your leafs nodes.
1,2,3,4,5,6,7,8,9,10
5
3 8
2 4 7 9
1 6 10

Resources