I was wondering if this would be considered a binary tree - binary-tree

Root node -----> node
So there is only one sub tree which only has one node. I was wondering if this would be allowed.

Yes it is a valid binary tree: you can consider the root node as a node that has two childrens, one of which is null.
The non-null child is also considered to be a leaf (has both childrens set to null).

Related

Binary Tree/ Binary Search Tree

Can a Binary Tree / Binary Search Tree only have the parent and one of the left or right nodes?
Or is it mandatory to have both the left and right nodes?
Binary implies two. Binary trees have two children pointers, not necessarily with references to anything. Parent references are not necessary - that is up to implementation.
If nodes were mandatory to have both children elements, then the tree would be infinite and leaves would not exist.
So, options for a node are
leaf
left/right child only
both children
Of course, there are alternative ways to think about a "tree". For example, take the binary heap array implementation.
It is not mandatory, you can create other types of links if you want. Define up and down pointers like this.
For a node x define up pointer u(x) as:
if x is the right child, then u(x) is the parent of x
if x is left child or root, then u(x) is the lowest node on the right path of x
For a node x define down pointer d(x) as:
if x's left subtree does not exist, then d(x) = x
if left subtree exists, then d(x) points to the rightmost node of x's left subtree
This is so called ring representation of a tree.
Actually, there is nothing mandatory other than just one rule (i.e. the maximum number of children that any binary tree node can have is two).
Technically:
Even a NULL is considered as a tree node when we define TreeNode *root = NULL.
Though, it has no practical significance, we do pass them as argument while calling insert, delete and display methods.
So, as a coder, assuming that a binary tree will always comprise of at least a parent node can prove fatal if the sanity of root is not checked before processing a binary tree.
A binary search tree is also a binary tree with additional properties. Any Binary tree node can have either 0 or 1 or 2 child nodes. So to answer your question, its okay to have only left or right nodes

What if I add new node at non leaf node in binary tree?

What should be the behavior..?
As we are adding to non leaf node, so it should update that node and keep children nodes.
or
Update non leaf node and discard children as we are adding and not updating.
or
Operation itself is invalid.
It depends upon the tree insertion logic. You add a node to a tree, the tree determines if it ends up being a leaf node or an intermediate node. You've tagged your question binary-tree and binary-search-tree. This would be my code for adding a node to a binary search tree.
def add(self, root, item):
if root is None:
return
if item < root.data:
root.left = add(self, root.left, item)
else:
root.right = add(self, root.right, item)
return root
Ofcourse this tree does not cater to duplicates.
It just depends on your binary tree.
There are two main forms. The first is the canonical binary tree with "left" and "right" children. If you specify that a node shall have two children or none, you can't easily insert extra parents. You need a dummy sibling for the grandchild you have just created. if you relax that rule, you can easily enough insert a new parent between node and its old parent.
The other form is the anary tree as a binary tree. This has "child" and "next" pointers. So in binary form its the same as the canonical tree, but the meaning is different and you draw it differently. Now of course we have to specify whehter we are inserting an extra sibling (the next pointer) or an extra child (the child pointer). But typically you specify that the node you insert shall not itself have any siblings (next pointer is null). it can also of course have a null child.
As we are adding to non leaf node, so it should update that node and keep children nodes.
That would actually mean REPLACING not adding IMHO. Note that, though perfectly fine to do that with BT; in case of BST, it would possibly trigger restructuring of the BST itself!
Update non leaf node and discard children as we are adding and not updating.
I would say that would be OVERKILL in the name of adding.
Think of a case when you have price of items stored in tree nodes. If you ADD price of a new item, should that lead to the destruction of old data? Perhaps no!
Operation itself is invalid.
Addition can't be invalid scenario here, as there is no prerequisite to keep the binary tree as it is.
So, here the effect of addition should be purely scenario-based.

Given a BST, and a node in BST, make that node, as new root of the tree

Given a BST, and a node in BST, make that node, as new root of the tree. But still maintain the tree as BST after making this node as root.
I tried as follows:
Take given node as root, if its on left of original root then make original root as right child of it and left child of original root as a left child of new root (similarly if new root is on right of original root). Now, there are two cases:
If node (new root's position in original structure) is leaf, then no worries at all
Problem is when node (new root's position in original structure) is internal node, then what can be done?
In the general case, your algorithm needs perform a series of tree manipulations in the form of sub-tree rotation (as described in #Quicky's answer).
The algorithm should look like this:
While node_to_be_root is not the root of the whole tree:
Rotate the sub-tree where the node_to_be_root is the pivot and its parent is the root of the sub-tree (after this rotation, the node_to_be_root is going to be the sub-tree's root).
For case 2 you need to perform a tree rotation as explained here
Here is the Algorithm i can think of
Create a new node with contents same as of the node (which has to be made a new node)
Compare new node content with current root node .
2.1. If it is greater then make the current node as left child of it otherwise right child
2.2 Also make the other child of current root node as child of new node based on condition 1
Delete the other node for which we have made a copy in step 1
To delete the node here is the algo
Node to be deleted is leaf: Simply remove from the tree.
Node to be deleted has only one child: Copy the child to the node and delete the child
Node to be deleted has two children: Find inorder(here it means finding last left child of right child of to be deleted node) successor of the node. Copy contents of the inorder successor to the to be deleted node and then ultimately delete the last left child..
Deleting the node is better explained at http://quiz.geeksforgeeks.org/binary-search-tree-set-2-delete/

Is the root node an internal node?

So I've looked around the web and a couple of questions here in stackoverflow here are the definition:
Generally, an internal node is any node that is not a leaf (a node with no children)
Non-leaf/Non-terminal/Internal node – has at least one child or descendant node with degree not equal to 0
As far as i understand it, it is a node which is not a leaf.
I was about to conclude that the root is also an internal node but there seems to be some ambiguity on its definition as seen here:
What is an "internal node" in a binary search tree?
As the wonderful picture shows, internal nodes are nodes located between the root of the tree and the leaves
If we follow that definition then the root node isn't going to be counted as an internal node. So is a root node an internal node or not?
Statement from a book : Discrete Mathematics and Its Applications - 7th edition By Rosen says,
Vertices that have children are called internal vertices. The root is an internal vertex unless it is the only vertex in the graph, in which case it is a leaf.
Supportive Theorem:
For any positive integer n, if T is a full binary tree with n internal vertices, then T
has n + 1 leaves and a total of 2n + 1 vertices.
case 1:
O <- 1 internal node as well as root
/ \
O O <- 2 Leaf Nodes
case 2: Trivial Tree
O <- 0 internal vertices (no internal vertices) , this is leaf
IMHO when you are talking about a tree with more than one node we can say the root node is an internal node. When there is only one node (the root node) the question of internal node doesn't arise. Hence we can vacuously say it is an internal node.
Yes root node is an internal node.
[More explanation]
A root node is never called as a leaf node even if it is the only node present in the tree.
For ex. if a tree has only one node then we say that it is a tree with only root node, we never say that the tree has a single leaf node.
Since internal node means a non-leaf node and because root node is never considered as leaf node I would say that in case of single node tree root node is an internal node.
"A node with no children is a leaf or external node. A non-leaf node is an internal node."
Source: "Introduction To Algorithms-3rd edition" page number 1176, last line.
So, root is also an internal node except when it is the only node of the tree.

A tree data structure where every left node is greater than every right node in a max situation?

Does such a tree exist and have a name, or is it just a figment of my imagination? I used to think heaps have this property but it just seems that the only requirement is for the children to be less than the parent.
It's exactly the opposite, but you may be thinking of a binary search tree, which has the following properties:
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 the node's key.
Both the left and right subtrees must also be binary search trees.
There must be no duplicate nodes.
So every left node is guaranteed to be less than every right node. You can find the max by going right from the root node until you can't go right any more.

Resources