Tree ADT

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 9

Tree Data Structure-

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.

In any tree, there must be only one root node.

We can never have multiple root nodes in a tree data structure.

2. Edge-

The connecting link between any two nodes is called as an 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.

In a tree, a parent node can have any number of child nodes.

4. Child-

The node which is a descendant of some node is called as a child node.

All the nodes except root node are child nodes.

5. Siblings-

Nodes which belong to the same parent are called as siblings.

In other words, nodes with the same parent are sibling nodes.

6. Degree-

Degree of a node is the total number of children of that node.

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.

Internal nodes are also called as non-terminal nodes.

Every non-leaf node is an internal node.

8. Leaf Node-

The node which does not have any child is called as a leaf node.

Leaf nodes are also called as external nodes or terminal nodes.

9. Level-

In a tree, each step from top to bottom is called as level of a tree.

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.

Height of a tree is the height of root node.

Height of all leaf nodes = 0

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.

Depth of the root node = 0

The terms “level” and “depth” are used interchangeably.

12. Subtree-

In a tree, each child from a node forms a subtree recursively.

Every child node forms a subtree on its parent node.

13. Forest- A forest is a set of disjoint trees.

Advantages of Tree

 Tree reflects structural relationships in the data.


 It is used to represent hierarchies.
 It provides an efficient insertion and searching operations.
 Trees are flexible. It allows to move subtrees around with minimum effort.
ADT of Tree:

 element(v) // returns the content of node v


 root() // returns the root node of the tree.
 parent(v) //returns the prent of node v
 children(v) //returns an iterable container holding the children of node v
 isInternal(v) // returns true if node v is internal
 isExternal(v) // returns true if node v is external
 isRoot(v) // returns true if node v is the root
 size() // returns the number of node in the tree
 isEmpty() // returns true if the tree is empty
 positions() // returns an iterable container of all nodes in the tree.
 elements() // returns an iterable container of the content of all node in the tree.
 replace(v,e) // replace the contents of node v with e. return the old contents of v.

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.

Unlabeled Binary Tree-

A binary tree is unlabeled if its nodes are not assigned any label.

Labeled Binary Tree-

A binary tree is labeled if all its nodes are assigned a 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.

Types of Binary Trees-

 Rooted Binary Tree


 Full / Strictly Binary Tree
 Complete / Perfect Binary Tree
 Almost Complete Binary Tree
 Skewed Binary Tree

Rooted Binary Tree

1. A rooted binary tree is a binary tree that satisfies the following 2 properties-

 It has a root node.


 Each node has at most 2 children.

2. Full / Strictly Binary Tree-


A binary tree in which every node has either 0 or 2 children is called as a Full binary tree.

 Full binary tree is also called as Strictly binary tree.

3. Complete / Perfect Binary Tree-


A complete binary tree is a binary tree that satisfies the following 2 properties-

 Every internal node has exactly 2 children.


 All the leaf nodes are at the same level.
 Complete binary tree is also called as Perfect binary tree.
4. Almost Complete Binary Tree-
An almost complete binary tree is a binary tree that satisfies the following 2 properties-
 All the levels are completely filled except possibly the last level.
 The last level must be strictly filled from left to right.

5. Skewed Binary Tree-

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.

Right skewed binary tree

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.

2. Maximum number of nodes in a binary tree of height H = 2H+1 – 1

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 =

We cannot insert more number of nodes in this binary tree.

3. Total Number of leaf nodes in a Binary Tree = Total Number of nodes with 2 children + 1

Example-

Consider the following binary tree-

Here,

 Number of leaf nodes = 3


 Number of nodes with 2 children = 2
Clearly, number of leaf nodes is one greater than number of nodes with 2 children.

This verifies the above relation.

5. Maximum number of nodes at any level ‘L’ in a binary tree = 2L

Example-

Maximum number of nodes at level-2 in a


binary tree

= 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:

Fig 1.Expression Tree

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

You might also like