Tree ADT
Tree ADT
Tree is a non-linear data structure which organizes data in a hierarchical structure and this is a
recursive definition. OR A tree is a connected graph without any circuits. OR If in a graph,
there is one and only one path between every pair of vertices, then graph is called as a tree.
Properties-
There is one and only one path between every pair of vertices in a tree.
A tree with n vertices has exactly (n-1) edges.
A graph is a tree if and only if it is minimally connected.
Any connected graph with n vertices and (n-1) edges is a tree.
1. Root-
The first node from where the tree originates is called as a root node.
2. Edge-
In a tree with n number of nodes, there are exactly (n-1) number of edges.
3. Parent-
The node which has a branch from it to any other node is called as a parent node.
In other words, the node which has one or more children is called as a parent node.
4. Child-
5. Siblings-
In other words, nodes with the same parent are sibling nodes.
6. Degree-
Degree of a tree is the highest degree of a node among all the nodes in the tree.
7. Internal Node-
The node which has at least one child is called as an internal node.
8. Leaf Node-
The node which does not have any child is called as a leaf node.
9. Level-
The level count starts with 0 and increments by 1 at each level or step.
10. Height-
Total number of edges that lies on the longest path from any leaf node to a particular node is called as
height of that node.
11. Depth-
Total number of edges from root node to a particular node is called as depth of that node.
Depth of a tree is the total number of edges from root node to a leaf node in the longest path.
12. Subtree-
Advantages of Tree
Binary Tree
Binary tree is a tree data structure in which each node has at most two children, which are
referred to as the left child and the right child.
A binary tree is unlabeled if its nodes are not assigned any label.
Basic concept
A binary tree is defined as a tree in which no node can have more than two children. The highest degree
of any node is two. This indicates that the degree of a binary tree is either zero or one or two.
In the above fig., the binary tree consists of a root and two sub trees TreeLeft & TreeRight. All nodes to
the left of the binary tree are denoted as left subtrees and all nodes to the right of a binary tree are referred
to as right subtrees.
Implementation
A binary tree has maximum two children; we can assign direct pointers to them. The declaration of tree
nodes is same as in structure to that for doubly linked lists, in that a node is a structure including the key
information plus two pointers (left and right) to other nodes.
1. A rooted binary tree is a binary tree that satisfies the following 2 properties-
A skewed binary tree is a binary tree that satisfies the following 2 properties-
All the nodes except one node has one and only one child.
The remaining node has no child.
A skewed binary tree is a binary tree of n nodes such that its depth is (n-1).
Left skewed binary tree
A left skew tree has node associated with only the left child. It is a binary tree contains only left
subtrees.
A right skew tree has node associated with only the right child. It is a binary tree contains only
right subtrees.
Binary Tree Properties-
1. Minimum number of nodes in a binary tree of height H = H + 1
Example-
To construct a binary tree of height = 4, we need at least 4 + 1 = 5 nodes.
Example-
Maximum number of nodes in a binary tree of height 3
= 23+1 – 1
= 16 – 1
= 15 nodes
Thus, in a binary tree of height = 3, maximum number of nodes that can be inserted =
3. Total Number of leaf nodes in a Binary Tree = Total Number of nodes with 2 children + 1
Example-
Here,
Example-
= 22 =4
Thus, in a binary tree, maximum number of nodes that can be present at level-2 = 4.
Expression tree
Expression tree is nothing but expressions arranged in a tree-like data structure. Each node in an
expression tree is an expression. For example, an expression tree can be used to represent mathematical
formula x < y where x, < and y will be represented as an expression and arranged in the tree like structure.
An expression tree is a representation of expressions arranged in a tree-like data structure. In other
words, it is a tree with leaves as operands of the expression and nodes contain the operators. Similar to
other data structures, data interaction is also possible in an expression tree.
Expression tree is an in-memory representation of a lambda expression. It holds the actual elements of the
query, not the result of the query.
Expression Tree is used to represent expressions. There are different types of expression formats:
Prefix expression
Infix expression and
Postfix expression
Expression Tree is a special kind of binary tree with the following properties:
Each leaf is an operand.
The root and internal nodes are operators.
Subtrees are subexpressions with the root being an operator.
Traversal Techniques
There are 3 standard traversal techniques to represent the 3 different expression formats.
Inorder Traversal
We can produce an infix expression by
recursively printing out the left expression,
then printing out the root, and
recursively printing out right expression.
Postorder Traversal
The postfix expression can be evaluated by
recursively printing out the left expression,
right expression and
then root
Preorder Traversal
We can also evaluate prefix expression by:
printing out the root and
then recursively print out the left and
right expression.
If we apply all these strategies to the sample tree above, the outputs are:
Infix expression: (a+(b*c))+(d*(e + f))
Postfix Expression: a b c * + d e f + * +
Prefix Expression: + + a * b c * d + e f
Construction of Expression Tree
We consider that a postfix expression is given as an input for constructing an expression tree. Following
are the step to construct an expression tree:
1. Read one symbol at a time from the postfix expression.
2. Check if the symbol is an operand or operator.
3. If the symbol is an operand, create a one node tree and pushed a pointer onto a stack
4. If the symbol is an operator, pop two pointer from the stack namely T 1 & T2 and form a new tree
with root as the operator, T1 & T2 as a left and right child
5. A pointer to this new tree is pushed onto the stack
Example
The input is: ab+c*
The first two symbols are operands, we create one-node tree and push a pointer to them onto the stack.
Next, read a'+' symbol, so two pointers to tree are popped,a new tree is formed and push a pointer to it
onto the stack.
Next, 'c' is read, we create one node tree and push a pointer to it onto the stack.
Finally, the last symbol is read ' * ', we pop two tree pointers and form a new tree with a, ' * ' as root, and
a pointer to the final tree remains on the stack.