Inorder successor of a binary tree (NOT BST) - binary-search-tree

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.

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

Binary Search tree time complexity

I am right now working with a Binary Search tree. I wonder what is the Tim complexity for a Binary Search tree. More Specific what is the worst case Time complexity for the operation height, leaves and toString for a Binary Search tree and why?
All three operations have a O(n) worst-case time complexity.
For height: all nodes will be visited when the tree is degenerate, and all nodes except one have exactly one child.
For leaves: each node will have to be visited in order to check whether they are a leave.
For toString: obviously all nodes need to be visited.

What is the most possible height when the binary search tree haw n nodes?

Is there a mathematical type for the most possible height of a tree with exactly n nodes?
It can be anything. If you are not implementing a balanced binary search tree (like AVL tree or Red-Black tree), then the height of the tree will depend on the inputs you give. In the worst-case, height can be equal to the number of nodes(if each value is greater than the previous one or each value is less than the previous one). If you need more info, please consider describing the specific use case for which this question was asked.

when to use inorder, preorder and postorder traversal

I understand the code behind how to do inorder, preorder, and postorder traversal on a binary search tree. However, I'm confused about the application.
When would you use each? It would be really helpful to illustrate cases of when each method of traversal makes the most sense.
Thanks!
Inorder traversal simply processes the items in the defined order. If, for example, you have a BST of a list of words or names, inorder traversal would print them out in order.
Preorder and postorder traversal most often apply to trees other than binary search trees. For example, to evaluate an expression like A + B * C, you can create a tree like this:
To evaluate the expression, you traverse the tree in postorder, applying each operator to the values from each of its sub-trees.
Preorder traversal could be used for roughly the same purpose if you wanted (for example) to produce output in a language something like Lisp, so the expression should come out as (add A (mul B C)).