Binary search trees node sizes - binary-search-tree

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.

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.

Can a binary search tree be skewed?

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

Is a given key a member of a binary tree - probabilistic answer

The Problem:
Given a BST with N nodes, with a domain of cardinality D (domain being the possible values for the node keys).
Given a key that is in the domain but may or may not be a member of the BST.
At the start, our confidence that the node is in the tree should be 1/D, but as we go deeper into the tree both D and N are split approximately in half. That would suggest that our confidence that our key is a member of the tree should remain constant until we hit the bottom or discover the key. However, I'm not sure if that reasoning is complete, since it seems more like we are choosing N nodes from D.
I was thinking something along the lines of this, but the reasoning here still doesn't seem complete. Can somebody point me in the right direction?
Apriori, the probability that your key in is the tree is N/D.
Without loss of generality, let assume that the node's value range is [1..D].
When you walk down the tree, either:
The current node matches your key, hence P = 1
The current node has value C which is larger than your key, you go left, but you don't know how many items are in the left sub-tree. Now you can make one of these assumptions:
The tree is balanced. The range in the subtree is [1..C-1], and there are (D-1)/2 nodes in the subtree. Hence, P = ((D-1)/2)/(C-1)
The tree is not balanced. The range in the subtree is [1..C-1], and the maximum likelihood estimation for the number of nodes in the subtree is N * (C-1)/D. Hence, P = (N*(C-1)/D)/(C-1) = N/D. (no change)
If you know more about how the tree was constructed - you can make a better MLE for the number of nodes in the subtree.
The current node has value C which is smaller than your key, you go right, but you don't know how many items are in the right sub-tree.
...

Remove a node in binary search tree

I am reading a book about removing a node from a binary search tree right now and the procedure described in the book seems unnecessarily complicated to me.
My question is specifically about removing a node that has both left and right subtree. In my opinion, node-to-remove should be replaced by the rightmost node in its left subtree or by its left node if its left subtree only has one node.
In case No.1, if we remove 40, it will be replaced by 30; in case No.2, if we remove 40, it will be replaced 35.
But in the book, it says the replacement should be found from node-to-remove's right subtree which could involve some complex manipulations.
Am I missing something here? Please point it out.
What you have pointed out is correct, the deleted node should be replace by either its in order successor which is the left most node in the right sub-tree or its in-order predecessor which is the right most node in the left sub-tree. This allows the tree to be traversed correctly. Most binary search tree data structures allow the deletion to be performed either way but in some cases special cases you might want to implement deletion such that the tree remains balanced.
More details and sample code is available on Wikipedia.
In case no.1 if you remove node 40 it will be replace by 50.
In case no.2 if you remove node 40 it will be replace by 50.
So basically when we delete any node that has 2 child then the removal should be as below.
We go the right child of the node, and then extreme left of that child.
Below figures shown some example, how to delete a node from binary search tree. This is also taken from one book, but it is clearly explained.

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