Can a binary search tree be skewed? - binary-search-tree

What I am trying to ask is whether a binary search tree is self-blancing or if it can also become skewed?
I tried looking for an unbalanced binary search tree and could not really find anything.
So is a BST different from a self-balancing BST?

is a BST different from a self-balancing BST?
Self-balancing binary search trees are binary search trees which have logic added to them for keeping the tree more or less balanced.
So every self-balancing BST is a BST, but not every BST is self-balancing.
In fact, with a binary search tree we can refer to a data structure without any algorithm that goes with it. For instance:
4
/ \
2 8
\
3
This represents a binary search tree. Nothing more needs to be said about how you would insert or delete a node. It might even be immutable, not allowing any insertions or deletions. It still is a binary search tree.
Can a binary search tree be skewed?
Yes. The following is a valid binary search tree, although not one that will allow for efficient look-ups:
5
/
4
/
3
/
2
/
1
A self-balancing binary search tree, is one that has specific algorithms associated with it for insertion and deletion that will include rebalancing logic.
There are many different self-balancing binary search trees:
AVL tree
Red-black tree
Variants:
AA tree
B-tree
Variants:
B+ tree
Splay tree
Treap
Scapegoat tree
Tango tree

Related

Why is this binary search tree an example of pre-order?

I'm a bit confused. I'm trying to learn about binary search tree's at the moment, and my understanding is that, in pre-order traversing, the left branch node value should be less than the root value. E.g., root: 7, leftchild: 6, rightchild: 8.
But I've seen this example of pre-order traversal that goes: 1 2 4 5 3. And images re-iterate that 1 is the root, and 2 is the leftchild node. But 2 is obviously more than 1.
Am I misunderstanding something?
Your understanding of binary search trees is correct.
But pre-order traversals (and other types of traversals) are not just a thing for binary search trees. You will find examples of such traversals of other types of trees that are not necessarily binary search trees. Preorder, postorder, breadth-first and depth-first traversals are also relevant for any other kind of tree -- they don't have to be search trees, and not even binary.
Only inorder traversals assume the tree is binary. And if that inorder traversal happens to visit the tree values in their proper order, then we are dealing with a binary search tree.

Why is the time complexity of binary search logN but the time complexity of a BST is N?

In Algorithms, 4th edition by Robert Sedgewick, the time complexity table for different algorithms is given as:
Based on this table, the searching time complexity of a BST is N, and of binary search in and of itself is logN.
What is the difference between the two? I have seen explanations about these separately and they made sense, however, I can't seem to understand why the searching time complexity of a BST isn't logN, as we are searching by continually breaking the tree in half and ignoring the other parts.
From binary-search-trees-bst-explained-with-examples
...on average, each comparison allows the operations to skip about half of the tree, so that each lookup, insertion or deletion takes time proportional to the logarithm of the number of items stored in the tree, O(log n) . However, some times the worst case can happen, when the tree isn't balanced and the time complexity is O(n) for all three of these functions.
So, you kind of expect log(N) but it's not absolutely guaranteed.
the searching time complexity of a BST is N, and of binary search in and of itself is logN. What is the difference between the two?
The difference is that a binary search on a sorted array always starts at the middle element (i.e. the median when n is odd). This cannot be guaranteed in a BST. The root might be the middle element, but it doesn't have to be.
For instance, this is a valid BST:
10
/
8
/
5
/
2
/
1
...but it is not a balanced one, and so the process of finding the value 1 given the root of that tree, will include visiting all its nodes. If however the same values were presented in a sorted list (1,2,5,8,10), a binary search would start at 5 and never visit 8 or 10.
Adding self-balancing trees to the table
We can extend the given table with self-balancing search trees, like AVL, and then we get this:
implementation
search
insert
delete
sequential search (unordered list)
𝑁
𝑁
𝑁
binary search (ordered array)
lg𝑁
𝑁
𝑁
BST
𝑁
𝑁
𝑁
AVL
lgN
lgN
lgN

Inorder successor of a binary tree (NOT BST)

Can someone help me to figure out how to find inorder successor of a binary tree for a given node(not a binary search tree)? I know how to find it in a binary search tree: It will be the left-most leaf of the right subtree. However, I am not sure how it is done if the tree is not a BST.
I don't think I can go to the right child and then to the leftmost leaf node. (OR is there a difference between finding inorder successor in a BST and normal BT)?
Thank you.
Is there a difference between finding inorder successor in a BST and normal BT?
No, there isn't. An inorder ordering is one that applies to binary trees in general.
The only difference in effect, is that an inorder traversal on a BST will produce a sequence of values that is ordered by value. But that is just a nice property that applies to a BST. It does not affect the meaning of what inorder represents.

Binary search trees node sizes

I'm slightly confused about how the order of nodes can be arranged in a binary search tree. Can there be node of a subtree in a binary search tree on the left that is larger than the root node?
For example, would the following be a binary search tree?
2
/ \
1 4
/ \
3
What confuses me above is whether or not the right subtree of the 1 (3) can be larger than the original root node (2).
No, there cannot be nodes to the left that are larger than the root. A binary search tree has the following properties (from wiki):
The left subtree of a node contains only nodes with keys less than the node's key.
The right subtree of a node contains only nodes with keys greater than or equal to the node's key.
Both the left and right subtrees must also be binary search trees.

Binary Search Tree formula for the number of structurally different trees that can exist with nodes that have either 0 or 1 children

I am trying to write a formula to find:
"The number of structurally different binary trees that can exist with nodes that have either 0 or 1 children".
How would I go about doing this?
Seems to me that a "binary tree" that has nodes with only 0 or 1 children is a chain. If by "structurally different" you mean that you treat differently whether a given non-terminal node has a left child or a right child, then observe that you can describe that tree with a binary number that is N-1 bits long. So the number of different trees for a given N would be 2**N-1.
(And, obviously, if you mean how many different "shapes" of the "tree" can exist for a given N, the answer is 1.)