Left balanced binary trees - binary-tree

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

Related

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.

Matrix tree data structure

Recently I heard from professor in my university about such data structure as "matrix tree". I can understand what it is, but where is it useful?
I'll try to give short explanation for this structure:
We have a tree root - special node. Then we have left and right "children" (subtrees). Both are binary trees. And if there is no some numbers in the tree but we add their "descendants" then we add this missing numbers as parasitic (so subtrees is nearly full). In left tree all nodes are even numbers. Others are in right tree. And for N we can say that N = 2^L(2*Y - 1) where N - node value (even, in this case), L - level number and Y - position in level.
Example (even subtree):
8
/ \
4 12
/ \ /\
2 6 10 14
If we exclude, for example, 4, it become parasitic (special flag in node) and that's all.

2-3 tree insertion

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

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

about complete binary tree

is this possible that a node in the complete binary tree has just one child?
thanks
Can this be a complete binary tree?
23
/ \
12 15
/ \
9 11
/ \ \
10 5 13
OK, first to make the difference between a perfect and a complete binary tree. In a perfect binary tree every node has two children(if not a leaf) or no children(if a leaf). So a perfect binary tree of level N has totally 2^(N + 1) - 1 nodes. But if we talk about complete binary tree - this means every level, except the last is full, and the last level may not be full. Also in a complete binary tree, the last level nodes must be filled from left to right.
So if you talk about perfect binary tree, it is not possible. But if you mean the complete binary tree, it is possible to have only one child.
I would say it is possible:
*
/ \
/ \
* x
/ \ /
* * *
this is a
binary tree in which
every level, except possibly the last,
is completely filled, and all nodes
are as far left as possible
And node x has just one child.
Citing Wikipedia:
A complete binary tree is a binary
tree in which every level, except
possibly the last, is completely
filled, and all nodes are as far left
as possible.
Which means no.
From the other answers:
A complete binary tree is a binary tree in which every level, except possibly the last, is completely filled, and all nodes are as far left as possible.
23
/ \
12 15
/ \
9 11 <- not the last level, but not completely filled!
/ \ \
10 5 13 <- last level: not completely filled, but that's okay
So this example tree is not complete, according to this definition.

Resources