2-3 tree insertion - data-structures

3 trees at school and after trying to find examples how to insert and build a 2-3 tree the answers that I found were different from what I learned. I want an 2-3 tree with m-1 like the following. I know the answer but I don't know how to build it. Can someone please show me how to build one using these elements which I got in this 2-3 and from where to begin
45_
14 25 50_
1 3_ 14 17 _ 25 27 30 45 _ _ 50 57 _

2-3 tree can have different no of elements in a particular node. The possible number of children each node can have is 2 0r 3.
Now if the parent consists on on element and has 2 children like
(a)
/ \
(b) (c)
Then ba which is what essentially happens in the case of a binary search tree. If the parent consists of 2 elements (a,b) and the children are q, w, e then qb and a
These are the conditions that have t be checked when you are inserting elements in a 2-3 tree. This will help you a lot. :)

There is an implementation of a 2 3 tree at Implementing a 2 3 Tree in C++.

Related

Build BST with an array and query a new node insertion level/depth

I am trying to build a BST based on an array.
my question is once I have a new value (TreeNode value) and want to insert it into my bst, and return the level of this TreeNode after its insertion. is there a (quick) way to know its level/depth information? Moreover, is there a way to know its level without actually build the BST. For example, I have an array [3,1,2], and I want to insert 4, is there a way to know what is the depth/level of 4 without actually build the BST and search for 4 to get is depth?
Thank you.
(If you don't mind, python code is preferred. sorry for any inconvenience.)
A complete BST has 2^k-1 nodes for some k>0. The k-bit binary fractions between 0 and 1 (not inclusive) listed in ascending order with trailing zeros removed serve as "instructions" for deciding where to place each node in the sorted list. For example, if k=3, then the tree has 7 nodes. The binary fractions are
001
01
011
1
101
11
111
Now suppose the sorted list to be placed in the tree is [10,20,30,40,50,60,70]. Interpret a 0 to mean "go left: and every 1 except the final one to mean "go right." The final 1 means "stop here." We start at the root. The match-up is
001 <-> 10
01 <-> 20
011 <-> 30
1 <-> 40
101 <-> 50
11 <-> 60
111 <-> 70
Following the "instructions", you'll end up with the list elements in the tree as follows:
40
20 60
10 30 50 70
Voila! A BST. This works for all k. Proving why is an interesting task! I will leave it to you to turn this observation into a working algorithm. There are several ways to do that.

Given a list of keys, how do we find the almost complete binary search tree of that list?

I saw an answer here with the idea implemented in Python (not very familiar with Python) - I was looking for a more general algorithm.
EDIT:
For clarification:
Say we are given a list of integer keys: 23 44 88 12 74 32 7 39 10
That list was chosen arbitrarily. We are to create an almost complete (or complete) binary search tree from that list. There is supposed to be only one such tree...how do we find it?
A binary search tree is constructed so that all items on a node's left subtree are less than the node, and all nodes on the right subtree are greater than the node.
A complete (or almost complete) binary tree is one in which all levels except possibly the last are completely full, and the bottom level is filled to the left.
So, for example, this is an almost-complete binary search tree:
4
/ \
2 5
/ \
1 3
This is not:
3
/ \
2 4
/ \
1 5
Because the bottom level of the tree is not filled from the left.
If the number of items is one less than a power of two (i.e. 3, 7, 15, etc.), then building the tree is easy. Start by sorting the list. Then, take the middle element as the root. So if you have [1,2,3,4,5,6,7], and the root node is 4.
You do the same thing recursively for the right and left halves of the array.
If the number of items is not one less than a power of two, you have to adjust the starting point (the root node) so that the bottom row is left-filled. Note that you might have to apply that adjustment recursively, as well, whenever your subtree length is not one less than a power of two.
Since this is a homework assignment, I'll leave that for you to figure out.

Left balanced binary trees

I am reading a book on data structures and it says that a left balanced binary tree is a tree in which the leaves only occupy the leftmost positions in the last level.
This seemed a little vague to me. Does this mean that leaves are only on the left side of a root and are distributed throughout the whole level, or leave exists only on the left side of the entire tree. Exactly what constitute left balanced?
I am not sure if my guess even covers any of the answer, so if anyone could help, it would be greatly appreciated :-).
You can think of a left-balanced binary tree as a balanced binary tree where the left sub-tree of each node is filled before the right sub-tree. In more informal terms, this is a tree wherein the nodes at the bottom-most level are all on the left side of the entire tree.
Take this tree for example:
This tree is balanced, but not left-balanced. If node 67 were removed, however, the tree would be left-balanced.
It seems to me that the definition of left balanced binary tree is the same of complete binary tree.
I don't really know the answer, but based on the description in the book it sounds to me like this...
For starters, lets think of it this way. Every "row" in the tree has a number, starting at zero and counting up. The row with the highest number is considered to be the last level.
Remember that leaf nodes are nodes without any children. So the tree meets the condition that every leaf node in the last level must be on the left, like so:
50
/ \
/ \
35 70
/ \ / \
10 34 57 90
/ / /
9 7 78
If we were to add a "98" as the right-child of 90 or a "59" as the right-child of 57, then this tree would no longer be left-balanced.
Edit: After reading Brandon E Taylor's answer, you should definitely not choose this one. After looking it over and reading the description again, not only does his make more sense, but it fits the description much better.
In addition to Brandon E Taylor's answer, a left balanced binary tree is a binary tree that when represented in an array there shouldn't exist any gaps in between the elements representing the tree nodes.
for a tree of size 6 for example, here are some possible array representations with the following criteria
1- let _ denote an empty slot in the array
2- let i be the index of a slot in an array (i is 1-based)
2- for any parent at index i the left child is at index (2*i) and the right child is at index (2*i)+1
1 2 3 4 _ _ <- left balanced
6 3 2 4 1 <- left balanced
4 2 1 _ 6 <- not left balanced
to elaborate further more, let's represent user265312's answer:
50 35 70 10 34 57 90 9 _ 7 _ _ _ 78 _ <- not left balanced
meanwhile Brandon's answer (after removing node 67) doesn't violate the left balancing rule:
50 17 72 12 23 54 76 9 14 19 _ _ _ _ _ <- left balanced

When two trees are equal?

If in-order traversal of two binrary trees (not binary search trees) are the same, does it guarantee that the two trees are the same?
if answer is no, then what about both in-order and pre-order traversal are the same?
Definitely not. The two trees
b
/ \
a d
/ \
c e
and
d
/ \
b e
/ \
a c
both have an inorder traversal of a b c d e. They are, in fact, rotations, an operation which preserves inorder traversal.
NO, and its seen with this simple example
3 2
2 1 3
1 0
0
Both have same inorder traversals [0,1,2,3]
But if inorder and preorder traversals are same then the trees are equal.
I'm thinking "no."
It's possible for two trees to be balanced differently, but have the same "order" of node values. For instance, if, of the set
1,2,3,4,5,6,7
You build a tree:
4
2 6
1 3 5 7
in-order traversal will yield 1,2,3,4,5,6,7.
however, if you choose a different root node (if the list is not sorted correctly beforehand)
5
4 6
2 7
1 3
These two trees will give the same in-order traversal result, but are (clearly) not identical.
or even
7
6
5
4
3
2
1
et cetera.
This is also related to a problem with BSP (binary space partition) trees, typically used in game development collision detection and visibility determination.
The BSP stores triangles in a tree. Each node contains a triangle or facet. The left node contains all children that are "behind" the facet, while the right child contains everything that is in "front." Recurse as expected.
If you pick the left-most facet in the scene as your root, the right child will then own every other facet. If you make a bad decision for the right child, the same thing will happen. It's perfectly possible for one to build a BSP compiler that, through idiotic analysis, builds a "tree" that is actually a list (as in my last example above). The problem is to partition the data set so that each node divides the remaining list as equally as possible. This is one of the reasons that BSPs are typically generated at compile time, as building one for a very complex geometry can take hours to find the/an optimal solution.
Inorder and any one of pre-order or post-order, uniquely define a tree structure. Nothing less.
One thing you can do is use level order
5
4 6
2 7
1 3
lvel order- 5 4 6 2 N N 7 1 3 N N N N N N

Binary search on unsorted arrays?

I came across this document Binary Search Revisited where the authors have proved/explained that binary search can be used for unsorted arrays (lists) as well. I haven't grokked much of the document on a first reading.
Have any of you already explored this ?
I've just read the paper. To me the author uses the term binary search to address the Bisection method used to find the zeros of a continuous function.
The examples in the paper are clearly inspired to problems like find the zero into an
interval (with translation on the y axe) or find the max/min of a function in tabular data.
The arrays the essay consider are not random filled ones, you will find a rule to construct them (it is the rule tied to the function used to dump them)
Said that it is a good chance of tinkering about different algorithms belonging to a common family in order to find similarity and differences. A good chance to expand your experiences.
Definitely not a new concept or an undervalued one.
Lookin for 3 into that unsorted list with binary or bisection :
L = 1 5 2 9 38 11 3
1-Take mid point in the whole list L : 9
3 < 9 so delete the right part of the list (38 11 3)
here you can already understand you will never find 3
2-Take mid point in the remaining list 1 5 2 : 5
3 > 5 so delete the right part of the list (5 2)
remains 1
Result : 3 unfound
Two remarks:
1-the binary or bisection algorithm consider right and left as an indication of the order
So i have rudely applied the usual algo considering right is high and left is low
If you consieder the opposit, ie right is low and left is high, then, trying to find 3 in this slighty similar list will lead to " 3 unfound"
L' = L = 1 5 2 9 3 38 11
3 < 9 / take right part : 3 38 11
mid point 38
3 < 38 take right part : 11
3 unfound
2- if you accept to re apply systematicly the algorithm on the dropped part of the list than it leads to searching the element in a list of n elements Complexity will be O(n) exactly the same as running all the list from beg to end to search your value.
The time of search could be slightly shorter.
Why ? let's consider you look one by one from beg. to end for the value 100000 in a sorted list. You will find it at the end of your list ! :-)
If now this list is unorderd and your value 100000 is for example exactly at the mid point ... bingo !!
Binary Search can be implemented on a rotated unsorted array/list.

Resources