How to create BST in post order-way - algorithm theory - algorithm

I'm preparing to exam from algorithms and have exercise which I do not understand:
"Create a BST from given numbers and write it by post-order method. Choose correct order in this notation."
Numbers: 42, 30, 45, 55, 70, 53, 40, 33, 60, 50
Anserws:
So I've started to think about this excercise and decided to sketch BST graph like this:
And I was thought that if I will read numbers from graph in post-order way, I will receive answer, but no. I think BST tree is ok, maybe I should create BST already in post-order notation somehow?

You have made a mistake in drawing the BST. The regulations for making a BST is,
the left-subtree's value for each node, should always be lesser than the current root.
the right-subtree's value for each node, should always be greater than the current root.
Now consider the sub-tree, that you have drew:
45
55 70
53 60
It's not following BST's property, re-draw the diagram, and try for Post-Order as demanded.
The property of Post-order is following:
1. visit left sub-tree.
2. visit right sub-tree.
3. visit the root.

I think BST tree is ok,
You made a mistake in drawing the BST when you inserted 53. It should not become the left child of 45, but its right child. It was the only mistake, but it obviously had big consequences for the rest of the values that you inserted in the BST.
I was thought that if I will read numbers from graph in post-order way, I will receive answer
That is right. If you recreate the BST without mistakes, this is the correct approach.

Related

Sweep Line Algorithm: AVL tree for neighboring line segments

I am currently trying to implement the sweep line algorithm for line segments intersection for Unity in C#.
I am using the book "Computational Geometry, Algorithms and Applications, 3rd Edition" by "Mark de Berg· Otfried Cheong. Marc van Kreveld· Mark Overmar."
PDF: https://people.inf.elte.hu/fekete/algoritmusok_msc/terinfo_geom/konyvek/Computational%20Geometry%20-%20Algorithms%20and%20Applications,%203rd%20Ed.pdf
At the moment I'm trying to implement the 2 datastructures: An AVL Tree for the Events and an AVL Tree for the line segments ordered by the x axis.
I successfully implemented the events structure, but I am struggling with the line segments structure: On page 25 of the book (P. 35 in the pdf) it explains how we store the line segments only in the leaves of the tree, but how would one implement such a tree? Since the other nodes in the trees are still "segments" but only serve as values to guide the search, it means that all segments will be in the tree twice. But doesn't an AVL tree only have each value once? and if not, how would you order them.
Beyond that, I don't fully understand the sentence "Suppose we search in T for the
segment immediately to the left of some point p that lies on the sweep line. At
each internal node ν we test whether p lies left or right of the segment stored
at ν. Depending on the outcome we descend to the left or right subtree of ν,
eventually ending up in a leaf. Either this leaf, or the leaf immediately to the left
of it, stores the segment we are searching for." That sentence is on page 25 as well.
Firstof, when would we descend to the left and when to the right? And why could also the leaf immediately to the left be the leaf we are looking for?
Hopefully I didnt write too much text. Thanks!

Find previous "chokepoint" in path

I have a "tree"-like structure of nodes and I'm trying to figure out an algorithm that will find previous "chokepoint" when end node is given. Here is a picture to better demonstrate:
So when 15 is specified as end node I want to find 7
And if 7 is specified as end node I want to find 1
But in the example above if anything else than 7,15 or 16 is specified as end node the found node is the previous one since that is the only node connecting to the end node.
So the node I am searching for is the previous node that all paths must go through to get to the end node.
I tried an algorithm where I start from the end node and go backwards (using Breadth-first) and every node I find that has 2 or more outputs I add to a new list and nodes with one output I skip. For example in case with 15 as the end node, I end up adding 10 and 7 to list of potential nodes, but I'm not sure how to from there. Since I should not continue traversing from 7.
Is there potentially an algorithm out there that already does that and if not how could I achieve this?
I believe your "choke points" are what is commonly known as "dominators". In a directed graph, one node X dominates another Y if all paths to Y must go through X. In your graph, 1 and 7 dominate all greater nodes.
See: https://en.wikipedia.org/wiki/Dominator_(graph_theory)
The dominators of a directed graph form a tree. The wikipedia article gives a simple algorithm for finding them all in quadratic time.
You can do it in linear time, but it's tricky. The classic algorithm is from Lengauer and Tarjan. You can find it here: https://www.cs.princeton.edu/courses/archive/fall03/cs528/handouts/a%20fast%20algorithm%20for%20finding.pdf
A topological sort is an ordering of the graph such that each arrow agrees with the order. For example in your example we might come up with the order:
1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 16, 15
Do a topological sort using any of the O(n) algorithms for it.
Next, we walk the graph and track how many incoming edges each node has.
Finally we walk the graph in our sorted order and track how many edges we have seen one end of but not the other, and how many nodes have no incoming edges. Any time we come to a node where all outgoing edges have not ended and every future node has incoming edges, that is a chokepoint.
After that, we can prepare two maps. The first is from each node to its topological order. The second is a balanced binary tree of where the chokepoints are.
The analysis in advance is O(n). The actual lookups are now O(log(n)).

Creating a Red-Black tree from BST tree - the fastest way?

I have to create and describe an algorithm for my university course that gets a BST tree T and creates new BST tree T' that satifies properties (and is as fast as possible):
1) T' has the same exact key values as T
2) T' is a Red-Black tree
So far I've had only one idea: randomize 0 or 1. In case of 0, get the max key node from left subtree of T and insert it into T', otherwise get the min key node from right subtree of T and insert it into T'. This is to ensure that Red-Black tree is at least somewhat balanced. The insertion would be any standard RB insertion.
Complexity of getting min/max is O(h), and since this needs to be repeated for each of the nodes in T, this would get quite high. I could also keep a pointer at the max node of left subtree and min node of the right subtree, which would solve the problem of traversing the whole height of the tree every time.
What do you think about this solution? I'm pretty sure it can be done better. Sorry if there is an obvious better solution, but I couldn't find answer to this on the internet, also it's only my 2nd semester at the university and I don't have much experience with programming.
Unless you have some other constraints or information, the fastest way is to forget about the shape of the original BST.
Just put the keys in an ordered list, and build a complete binary tree from it, all in O(N) time.
Then, if there's a partially filled leaf level, then color those nodes red. The rest are black.

Searching a number in a binary search tree. If it doesn't exist, find the the biggest number smaller than it [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
Say you have a balanced binary search tree (say AVL tree) with n numbers.
You need an algorithm that looks for a given number x, but if it doesn't find it,
returns the largest number that is smaller than x.
For the sake of the question do it without inserting x and then removing it, and the solution should be O(log(n)).
Thanks
This question is about finding the Inorder Predecessor of a (virtually) inserted node. I will first explain what an Inorder Predecessor is and then explain how we can find the Inorder Predecessor in a BST.
Before we can talk about the Inorder Predecessor, we must first be familiar with Inorder Traversal of a Binary Search Tree.
An Inorder traversal is an algorithm to visit all the nodes of a Binary Search Tree in a specific order as outlined in the recursive algorithm below:
Inorder(Node S)
Inorder(left child of S)
print S
Inorder(right child of S)
In plain words, we recursively print the left tree, then the center node, and then the right subtree.
The algorithm is quite simple yet elegant in that when it prints the keys in this order, owing to the ordering of keys in the structure of a BST, we would be getting a special sequence of keys. What is that special sequence? Let's trace the algorithm for a BST and check that.
Let's consider the BST shown below:
Here, let's trace our algorithm, first we start the algorithm at the root(node with value 20), then as the algorithm says we recursively call the same function for the left subtree, which is the subtree rooted at node 8, and again we call for the left subtree of 8, which is the node 4. At this point 4 has no left subtree, so we print the key 4. Since 4 has no right subtree, we are done for the subtree rooted at node 4, and we backtrack to the node 8. Now we print the node 8. Then we call the recursive function for the right subtree of node 8, and following the algorithm step by step, you will realize that the sequence printed is: 4, 8, 10, 12, 14, 20, 22
What is this sequence? It's just the sorted sequence of the keys.
How did this happen? Well, it's just the way BST nodes are structured: BST has a special property which is that for each node S, all keys to the left of the node S are lesser than S and all nodes to the right of S are greater than S.
In other words, the Inorder traversal just neatly prints out the keys of a Binary Search Tree in the sorted order.
Okay, now that we understand the recursive algorithm that prints the Inorder Traversal of a BST, we re-focus on our task of finding the node which would be the the largest node that is smaller than our target node. What is this node? It's just the node which would be printed just before our virtually inserted node in the Inorder traversal of the BST. This is called the Inorder Predecessor.
Let's look back at our BST and understand what an Inorder Predecessor is.
Here what is the Inorder Predecessor of 12? What do we do? We know that the Inorder Predecessor of a node is the node which was printed just before this node was printed in the Inorder traversal. Which node can this be? To answer this, we must visualize the exact point in time at which our node 12 would be printed. Clearly for something to be printed before a specific node by our Inorder traversal algorithm, it should be some node whose printing step is already done. In this case, for the node 12, we see that the printing step of 12 is preceded immediately by a call to recursively print 12's left child. What is 12's left child? It is 10. Clearly 10 is the node that would be printed just before 12 in the Inorder traversal. Thus, 10 is the Inorder Predecessor of 12.
In general, it is easy to see that if a node has a left subtree, then the Inorder Precedessor for the node is the maximum node in it's left subtree.
But what if the node has no left subtree? Say, what is the Inorder Predecessor of the node 10? How do we find this?
Well, this is a bit tricky, as 10 does not have a left subtree. Let's think, which node could have been printed just before the node 10? We are looking for a node which has already been printed. First, how did we get to the node 10 in the traversal's algorithm sequence? We came left from the node 12, could the Inorder Predecessor be 12? No, because the node 12 would be only printed after 10, as per the algorithm, as 10 is being printed by the recursive call to the left subtree of node 12. Okay, what called 12? 8 called the recursive call to 12, as part of it's recursive call to it's right subtree. Okay, could 8 be the Inorder Predecessor? First have we printed 8 already? Yes, we have already printed 8 as we are now already in 8's right subtree. 8 is indeed the node that would be printed just before the node 10 in the Inorder Traversal. Why? What did we do just now? All we did was look back at how we ended up at node 10, retrace the steps and see the first node that has already been printed. Clearly, that is the Inorder Predecessor. Hence 8 is the Inorder Predecessor of 10.
In general, it is easy to see that if a node has no left subtree, all you need to do is go up the tree from your node to the root and in doing so, find the node which is a right child of it's parent, then the parent would be the Inorder Predecessor.
Thus, all you need to do to find a Inorder Predecessor of a node, is to trace back the recursive calls of how you got to the node in the traversal.
Summarizing:
If the node has a left subtree, the Inorder Predecessor is the maximum key in the left subtree of the node.
Else
Go up till you find a node which is a right child of it's parent, then the parent would be the Inorder Predecessor.
Now, that we understand what an Inorder Predecessor is and how to figure it out in a BST, let's get back to our task. Since our node did not exist in the first place, then we would be inserting it(virtually atleast), which means it would be a leaf on insertion, which means it would have no left subtree, which means it's predecessor is in fact only up the tree. So, in one traversal, you should be able to do this task, without inserting actually.

Difference Between Lowest Level and Last Level in A Binary Tree

Is the Root of a Binary Tree the Lowest Level or are the Leaves Lowest Level.
The elements 71, 65, 84, 69, 67, 83 are inserted in a binary search tree. The element in the
lowest level is?
Trees in computer science traditionally are upside-down trees. We should really be calling them roots, but the term trees has stuck.
So, the root is the topmost level, and the leaves are the lowermost levels. Go figure.
(No computer scientist was ever famous for his grasp of the real world.)

Resources