Difference between ordered and unordered (rooted) trees - algorithm

I am reading algorithms by Robert Sedwick. Some definitions from the book are shown below.
A tree (also an ordered tree) is a node (called the root) connected to
a sequence of disjoint trees. Such a sequence is called a forest.
A rooted tree (or unordered tree) is a node (called the root)
connected to a multiset of rooted trees. (such a multiset is called an
unordered forest.
My questions on the above text are
I am having difficutlty in understanding above defintions. Can any one please explain with examples.
What does author mean by disjoint trees?
What does author mean by multiset rooted trees?
Thanks for your time and help

A tree by this definition is more or less what we normally understand by tree: a node connected to an ordered sequence of (sub)trees. It's a recursive definition: if the sequence is empty, the node is called leaf, and if not, each of the tree in the sequence is also "a node connected to an ordered sequence of (sub)trees."
By disjoint the author means that the subtrees don't have nodes in common.
The definition means that the subtrees of a rooted tree are not in a particular order and that they may be repeated. A multiset is kind of like a set that allows multiples.
An ordered tree (a "tree" of the first definition) has the subtrees in a particular order, and the subtree sequence cannot contain the same tree twice, because the subtrees must be disjoint. A rooted tree does not have these restrictions; by this definition a root might have a subtree twice, in a structure that resembles a cycle.
I don't have Sedwick's book to check if or why this definition makes sense; a more common definition or rooted tree would use a normal set for subtrees, rather than a multiset. Perhaps the intention is to allow multiple links between a node and its children while disallowing other kinds of cycles, such as links between siblings and cousins.

Related

In binary trees, are sibling nodes necessarily ordered?

Just been learning about binary trees in school, and two rules of binary trees is that
every node has at most 2 child nodes
there exists linear ordering defined for the children of each node (ordered pair)
Now, all types of binary trees (full, complete, etc.) are binary trees so they must satisfy these 2 conditions.
However, I saw on GeeksForGeeks this example:
How is 'linear ordering', ordered pair, defined here?
It seems that for the sibling nodes in this picture, some left ones are larger than the right one, some right nodes are larger than the left one.
If asked to check if a given tree is a binary tree, how do I make sure of the second property, that the children of each node have to be ordered?
Thanks
This is one of the complicated ways to introduce a binary tree.
two rules of binary trees is that
every node has at most 2 child nodes
there exists linear ordering defined for the children of each node (ordered pair)
Simple ways of introducing binary trees I could think of are "at most two children and no cycles" or "at most two children and unique path between any pair of vertices".
But fine. You bring up the point of linear order. Lets discuss that.
Here
A linear ordering on a finite collection of objects may be described
as follows: each object has exactly one immediate predecessor object
and one immediate successor object with two exceptions: A first object
has no predecessor and a last object has no successor.
If you have learnt about traversal so far, with the above definition, I would take binary tree traversals as linear order - preorder, postorder, inorder, level order. This applies to all types of binary trees (full, complete, etc.) which includes the complete binary tree you posted as an image.

Why the tree is not a binary tree?

I am a beginner in the field of data structures, I am studying binary trees and in my textbook there's a tree which is not a binary tree but I am not able to make out why the tree is not a binary tree because every node in the tree has atmost two children.
According to Wikipedia definition of binary tree is "In computer science, a binary tree is a treedata structure in which each node has at most two children, which are referred to as the left child and the right child."
The tree in the picture seems to satisfy the condition as mentioned in the definition of binary tree.
I want an explanation for why the tree is not a binary tree?
This is not even a tree, let alone binary tree. Node I has two parents which violates the tree property.
I got the answer, This not even a tree because a tree is connected acyclic graph also a binary tree is a finite set of elements that is either empty or is partitioned into three disjoint subsets. The first subset contains a single element called the root of the tree. The other two subsets are themselves binary trees called the left and right subtrees of the original tree.
Here the word disjoint answers the problem.
It's not a binary tree because of node I
This can be ABEI or ACFI
This would mean the node can be represented by 2 binary numbers which is incorrect
Each node has either 0 or 1 parents. 0 in the case of the root node. 1 otherwise. I has 2 parents E and F

What is the difference between ordered tree and rooted tree? - An example

I know the definition of these two types of trees. However, I cannot understand their structures. Could you give me an example ?
A rooted Tree has only ONE Element which is considerd as root element.
In the examples below the root element is green and the data structure is represented in form of a binary tree with two child elements.
An Ordered Binary Tree is a form of rooted tree with a specific order.
Started from the root element the smaller number is on the left side and the higher number on the right side.
Ordered Binary Tree
An Unordered Binary Tree on the other side has no specific order of the elements.
Unordered Root Tree
The structure of an unordered tree looks like follows,
As can be seen in the above figure, the root of the tree is node A, but its children are not ordered from left to right or in any particular order. However, we can re-draw the tree as follows, in order to make it an ordered rooted tree.
An ordered tree can be either a binary tree or else a general tree. The binary nature of a tree and the ordered nature are two separate concepts. The overall idea is, a rooted tree is not an ordered rooted tree unless it is depicted according to a particular order or some additional textual information is given to notify the order.

Joining of binary trees

Suppose we have a set of binary trees with their inorder and preorder traversals given,and where no tree is a subtree of another tree in the given set. Now another binary tree Q is given.find whether it can be formed by joining the binary trees from the given set.(while joining each tree in the set should be considered atmost once) joining operation means:
Pick the root of any tree in the set and hook it to any vertex of another tree such that the resulting tree is also a binary tree.
Can we do this using LCA (least common ancestor)?or does it needs any special datastructure to solve?
I think a Binary tree structure should be enough. I don't think you NEED any other special data structure.
And I don't understand how you would use LCA for this. As far as my knowledge goes, LCA is used for knowing the lowest common Ancestor for two NODES in the same tree. It would not help in comparing two trees. (which is what I would do to check if Q can be formed)
My solution in words.
The tree Q that has to be checked if it can be made from the set of trees, So I would take a top-down approach. Basically comparing Q with the possible trees formed from the set.
Logic:
if Q.root does not match with any of the roots of the trees in the set (A,B,C....Z...), No solution possible.
if Q.root matches a Tree root (say A) check corresponding children and mark A as used/visited. (Which is what I understand from the question: a tree can be used only once)
We should continue with A in our solution only if all of Q's children match the corresponding children of A. (I would do Depth First traversal, Breadth First would work as well).
We can add append a new tree from the set (i.e. append a new root (tree B) only at leaf nodes of A as we have to maintain binary tree). Keep track of where the B was appended.
Repeat same check with corresponding children comparison as done for A. If no match, remove B and try to add C tree at the place where B was Added.
We continue to do this till we run out of nodes in Q. (unless we want IDENTICAL MATCH, in which case we would try other tree combinations other than the ones that we have, which match Q but have more nodes).
Apologies for the lengthy verbose answer. (I feel my pseudo code would be difficult to write and be riddled with comments to explain).
Hope this helps.
An alternate solution: Will be much less efficient (try only if there are relatively less number of trees) : forming all possible set of trees ( first in 2s then 3s ....N) and and Checking the formed trees if they are identical to Q.
the comparing part can be referred here:
http://www.geeksforgeeks.org/write-c-code-to-determine-if-two-trees-are-identical/

Binary Tree Definition

I see this definition of a binary tree in Wikipedia:
Another way of defining binary trees is a recursive definition on directed graphs. A binary tree is either:
A single vertex.
A graph formed by taking two binary trees, adding a vertex, and adding an edge directed from the new vertex to the root of each binary tree.
How then is it possible to have a binary tree with one root and one left son, like this:
O
/
O
This is a binary tree, right? What am I missing here?
And please don't just say "Wikipedia can be wrong", I've seen this definition in a few other places as well.
Correct. A tree can be empty (nil)
Let's assume you have two trees: one, that has one vertex, and one which is empty (nil). They look like this:
O .
Notice that I used a dot for the (nil) tree.
Then I add a new vertex, and edges from the new vertex to the existing two trees (notice that we do not take edges from the existing trees and connect them to the new vertes - it would be impossible.). So it looks like it now:
O
/ \
O .
Since edges leading to (nil) are not drawn, here it is what is at the end:
O
/
O
I hope it clarifies.
It depends on the algorithm you use for binary-tree: as for icecream, there are many flavors :)
One example is when you have a mix of node pointers and leaf pointers on a node, and a balancing system that decide to create a second node (wether it's the root or the other) when you are inserting new values on a full node: instead of creating a root and 2 leafs, by splitting it, it's much more economical to create just another node.
Wikipedia can be wrong. Binary trees are finite data structures, a subtree must be allowed to be empty otherwise binary trees would be infinite. The base case for the recursive definition of a binary tree must allow either a single node or the empty tree.
Section 14.4 of Touch of Class: An Introduction to Programming Well Using Objects
and Contracts, by Bertrand Meyer, Springer Verlag, 2009. © Bertrand Meyer, 2009. has a better recursive definition of a binary tree
Definition: binary tree
A binary tree over G, for an arbitrary data type G, is a finite set of items called
nodes, each containing a value of type G, such that the nodes, if any, are
divided into three disjoint parts:
• A single node, called the root of the binary tree.
• (Recursively) two binary trees over G, called the left subtree and right subtree.
The definition explicitly allows a binary tree to be empty (“the nodes, if any”).
Without this, of course, the recursive definition would lead to an infinite
structure, whereas our binary trees are, as the definition also prescribes, finite.
If not empty, a binary tree always has a root, and may have: no subtree; a
left subtree only; a right subtree only; or both.

Resources