Difference Between Lowest Level and Last Level in A Binary Tree - data-structures

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.)

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!

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

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.

What is the number of nodes at a particular level in a balanced binary search tree?

I was asked this question in a phone screen interview and I was not able to answer it. For example, in a BST, I know that the maximum number of nodes is given by 2^h (assuming the root node at height = 0)
I wanted to ask, is there a similar mathematical outcome for a balanced binary search tree as well (For AVL, Red Black trees?), i.e. the number of nodes at a particular level k.
Thanks!
A balanced binary tree starts with one node, which has two descendants. Each of those then has two descendants again. So there will be 1, 2, 4, 8 and so on nodes per level.
As a formula you can use 2^(level-1). The last row might not be completely full, so it can have less elements.
As the balancing step is costly, implementations usually do not rebalance after every mutation of the tree. They will rather apply a heuristic to find out when a rebalancing will make the most sense. So in practice, levels might have less nodes than if the tree were perfectly balanced and there might be additional levels from nodes being inserted in the wrong places.

priority generation in treap data structure

I'm studying the treap data structure. When inserting node, treap radomly generate node's priority. But what if 69 node's generated priority is 13 in the picture above?
Parent's priority must higher than child's priority. Do treap's binary tree attribute collide with heap attribute?
I want to know. Thanks.
Assuming you have treap from your picture without 69 node and want to add (69, 13) node:
1. Split existing treap to 2 treaps L and R by key 69 (here all old treap will be L)
2. Create treap M with single node (69, 13)
3. Merge M with L, then result with R
For this case node (69, 13) becomes a new root, and old treap will be it's left child.
If node 69 had priority 13, then it would be the root of the tree and node 31 would be its left child. The descendants of node 31 would be as in your diagram, except of course for node 69.
It's always possible to arrange a treap so that it respects both the heap and the binary search properties. In fact, in the absence of equal values or equal priorities, there is only one possible arrangement.
The random assignment of priorities makes it likely that the treap will be reasonably balanced. It might not be perfectly balanced, but on the positive side building the treap is fast and uncomplicated.

Hoffman tree Vs, Binary balanced tree

Given n Real Positive numbers A1,A2....An build a binary tree T such as:
Every number is a leaf.
Weight of T will be minimal as possible. Weight of tree equals to sum of each leaf times its hight.
This question was given to me during a test in Algorithms and Data Structures. My answer in brief was to build a binary tree such that each leaf is A1 to An. Weight of T will be sum of logn*Ai.
I did not get points for this answer. The answer that was awarded full points was to sort the numbers by frequencies and build a Hoffman Tree.
My question is why my answer was ignored?
If A1 to An are all very small numbers, for example ranging from 0 to 1, then the hight of each leaf will become the dominent factor in calculating the weight of the tree.
Help would be apprecieted.
In the original array A there may be some elements with many more occurences than the others. You want to construct the tree in a way, that the most frequent elements are higher in the tree than the somewhat rare ones.
Consider the example on this page - "A quick tutorial on generating a huffman tree".
The generated huffman tree has weight 228, which is optimal.
The best perfectly balanced tree you could get for the same set has weight 241 (5 and 6 with depth 2, other elements with depth 3), the worst one 294 (switching 5 and 6 with 1 and 2).
Your solution would find something between those, rather than the optimum.

Resources