CH # 6 (Trees)
CH # 6 (Trees)
Chapter # 6
The Trees
By:
Muhammad Imran
Assistant Prof. of Computer Science
Govt. Millat Degree College Mumtazabad, Multan
Trees
A data structure is said to be linear if its elements form a sequence or a linear list. Previous linear
data structures that we have studied like an array, stacks, queues and linked lists organize data in
linear order. A data structure is said to be non linear if its elements form a hierarchical
classification where, data items appear at various levels.
Trees and Graphs are widely used non-linear data structures. Tree and graph structures represents
hierarchial relationship between individual data elements. Graphs are nothing but trees with
certain restrictions removed.
In linear data structure, data is organized in sequential order and in non-linear data structure, data
is organized in random order. Tree is a very popular data structure used in wide range of
applications. A tree data structure can be defined as follows...
Tree is a non-linear data structure which organizes data in hierarchical structure and this
is a recursive definition.
In tree data structure, every individual element is called as Node. Node in a tree data structure,
stores the actual data of that particular element and link to next element in hierarchical structure.
A tree is hierarchical collection of nodes. One of the nodes, known as the root, is at the
top of the hierarchy. Each node can have at most one link coming into it.
In a tree data structure, if we have N number of nodes then we can have a maximum
of N-1 number of links.
Example
Terminologies in Trees
In a tree data structure, we use the following terminology...
1. Root
In a tree data structure, the first node is called as Root Node. Every tree must have root node.
We can say that root node is the origin of tree data structure. In any tree, there must be only one
root node. We never have multiple root nodes in a tree. Here ‘A’ is the root node.
2. Edge
In a tree data structure, the connecting link between any two nodes is called as EDGE. In a tree
with 'N' number of nodes there will be a maximum of 'N-1' number of edges.
3. Parent Node
In a tree data structure, the node which is predecessor of any node is called as PARENT NODE.
In simple words, the node which has branch from it to any other node is called as parent node.
Parent node can also be defined as "The node which has child / children".
4. Child
In a tree data structure, the node which is descendant of any node is called as CHILD Node. In
simple words, the node which has a link from its parent node is called as child node. In a tree,
any parent node can have any number of child nodes. In a tree, all the nodes except root are child
nodes. In the above figure B and C are children of A. similarly G and H are children of C.
5. Siblings Nodes
In a tree data structure, nodes which belong to same Parent are called as SIBLINGS. In simple
words, the nodes with same parent are called as Sibling nodes. In the above example B & C are
siblings. D,E,F are siblings. G and H are siblings. I and J are siblings.
6. Leaf Nodes
In a tree data structure, the node which does not have a child is called as LEAF Node. In simple
words, a leaf is a node with no child.
In a tree data structure, the leaf nodes are also called as External Nodes. External node is also a
node with no child. In a tree, leaf node is also called as 'Terminal' node. In the above tree
D ,I ,J ,F ,K and H are Leaf Nodes.
7. Internal Nodes
In a tree data structure, the node which has atleast one child is called as INTERNAL Node. In
simple words, an internal node is a node with atleast one child.
In a tree data structure, nodes other than leaf nodes are called as Internal Nodes. The root node
is also said to be Internal Node if the tree has more than one node. Internal nodes are also
called as 'Non-Terminal' nodes. In the above tree A, B, C, E and G are Internal Nodes.
8. External Nodes
The leaf / terminal nodes are called as External nodes. In the above tree D,I,J,F,K and H are
External Nodes.
10. Degree
In a tree data structure, the total number of children of a node is called as DEGREE of that
Node. In simple words, the Degree of a node is total number of children it has. The highest
degree of a node among all the nodes in a tree is called as 'Degree of Tree'. In the above tree
degree of B is 3, Degree of A is 2, and Degree of F is 0.
11. Level
In a tree data structure, the root node is said to be at Level 0 and the children of root node are at
Level 1 and the children of the nodes which are at Level 1 will be at Level 2 and so on... In
simple words, in a tree each step from top to bottom is called as a Level and the Level count
starts with '0' and incremented by one at each level (Step).
In a tree data structure, the total number of egdes from leaf node to a particular node in the
longest path is called as HEIGHT of that Node. In a tree, height of the root node is said to
be height of the tree. In a tree, height of all leaf nodes is '0'.
In a tree data structure, the total number of egdes from root node to a particular node is called
as DEPTH of that Node. In a tree, the total number of edges from root node to a leaf node in the
longest path is said to be Depth of the tree. In simple words, the highest depth of any leaf node
in a tree is said to be depth of that tree. In a tree, depth of the root node is '0'.
14. Path
In a tree data structure, the sequence of Nodes and Edges from one node to another node is called
as PATH between that two Nodes.Length of a Path is total number of nodes in that path. In
below example the path A - B - E - J has length 4.
In a tree data structure, each child from a node forms a subtree recursively. Every child node will
form a subtree on its parent node.
Binary Tree
In a normal tree, every node can have any number of children. Binary tree is a special type of
tree data structure in which every node can have a maximum of 2 children. One is known as left
child and the other is known as right child.
A tree in which every node can have a maximum of two children is called as Binary Tree.
In a binary tree, every node can have either 0 children or 1 child or 2 children but not more than
2 children.
Example
A binary tree in which every node has either two or zero number of children is called
Strictly Binary Tree
Strictly binary tree is also called as Full Binary Tree or Proper Binary Tree or 2-Tree
Example
A binary tree in which every internal node has exactly two children and all leaf nodes are
at same level is called Complete Binary Tree. Complete binary tree is also called as Perfect
Binary Tree.
The full binary tree obtained by adding dummy nodes to a binary tree is called as
Extended Binary Tree.
In above figure, a normal binary tree is converted into full binary tree by adding dummy nodes in
shape of rectangle.
We define two terms: Internal nodes and external nodes. An internal node is a tree
node having at least one–key and possibly some children. It is some times convenient to have
another types of nodes, called an external node, and pretend that all null child links point to such
a node. An external node doesn’t exist, but serves as a conceptual place holder for nodes to be
inserted. We draw internal nodes using circles, with letters as labels. External nodes are denoted
by squares. The square node version is sometimes called an extended binary tree. A binary tree
with n internal nodes has n+1 external nodes. Following Figure shows a sample
tree illustrating both internal and external nodes.
1. Array Representation
1. Array Representation
In array representation of binary tree, we use a one dimensional array (1-D Array) to represent a
binary tree.
To represent a binary tree of depth 'n' using array representation, we need one dimensional array
with a maximum size of 2n+1 - 1.
The above example of binary tree represented using Linked list representation is shown as
follows...
Displaying (or) visiting order of nodes in a binary tree is called as Binary Tree Traversal.
1. In - Order Traversal
In this traversal method, the left subtree is visited first, then the root and later the right
sub-tree. We should always remember that every node may represent a subtree itself.
If a binary tree is traversed in-order, the output will produce sorted key values in an
ascending order. The steps for traversing a binary tree in inorder traversal are:
We start from A, and following in-order traversal, we move to its left subtree B. B is also
traversed in-order. The process goes on until all the nodes are visited. The output of inorder
traversal of this tree will be −
D→B→E→A→F→C→G
Example 2: What will be the traversing order if we use in-order traversal algorithm
I-D-J-B-F-A-G-K-C–H
2. Pre - Order Traversal ( root - leftChild - rightChild )
In this traversal method, the root node is visited first, then the left subtree and finally the
right subtree. The steps for traversing a binary tree in preorder traversal are:
We start from A, and following pre-order traversal, we first visit A itself and then move to
its left subtree B. B is also traversed pre-order. The process goes on until all the nodes are
visited. The output of pre-order traversal of this tree will be −
A→B→D→E→C→F→G
Example 2: What will be the traversing order if we use pre-order traversal algorithm
A-B-D-I-J-F-C-G-K–H
3. Post - Order Traversal ( leftChild - rightChild - root )
In this traversal method, the root node is visited last, hence the name. First we traverse
the left subtree, then the right subtree and finally the root node. The steps for traversing a binary tree in postorder
traversal are:
We start from A, and following pre-order traversal, we first visit the left subtree B. B is also
traversed post-order. The process goes on until all the nodes are visited. The output of post-order
traversal of this tree will be −
D→E→B→F→G→C→A
Example 2: What will be the traversing order if we use post-order traversal algorithm
I-J-D-F-B-K-G-H-C-A
Expression Trees:
Expression tree is a binary tree, because all of the operations are binary. It is also possible for a
node to have only one child, as is the case with the unary minus operator. The leaves of an
expression tree are operands, such as constants or variable names, and the other (non leaf) nodes
contain operators. Once an expression tree is constructed we can traverse it in three ways:
• Inorder Traversal
• Preorder Traversal
• Postorder Traversal
Following Figure shows some more expression trees that represent arithmetic expressions given
in infix form.
An expression tree can be generated for the infix and postfix expressions.
An algorithm to convert a postfix expression into an expression tree is as follows:
Example 1:
Construct an expression tree for the postfix expression: a b + c d e + * *
Solution:
The first two symbols are operands, so we create one-node trees and push pointers to
them onto a stack.
Next, a ‘+’ is read, so two pointers to trees are popped, a new tree is formed, and a pointer to it is
pushed onto the stack.
Next, c, d, and e are read, and for each one–node tree is created and a pointer to the
corresponding tree is pushed onto the stack.
Continuing, a ‘*’ is read, so we pop two tree pointers and form a new tree with a ‘*’ as root.
Finally, the last symbol is read, two trees are merged, and a pointer to the final tree is left on the
stack.
Do yourself.
Basic Operations
Example
10,12,5,4,20,8,7,15 and 13
Example 2:
Construct a Binary Search Tree by inserting the following sequence of numbers...
Insertion in a BST
Value in a BST can be inserted by first locating the proper place of the particular value.
For example if we want to insert 15 in the above BST. It will become the left child of 18.
Deletion in a BST