0% found this document useful (0 votes)
6 views

Chapter 5

Chapter Five discusses trees as a versatile data structure for representing hierarchical relationships. It covers various types of trees, including binary trees, their properties, and methods for representation using arrays and linked lists. Additionally, it outlines operations on binary search trees, including insertion, deletion, and traversal methods.

Uploaded by

gadisa
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

Chapter 5

Chapter Five discusses trees as a versatile data structure for representing hierarchical relationships. It covers various types of trees, including binary trees, their properties, and methods for representation using arrays and linked lists. Additionally, it outlines operations on binary search trees, including insertion, deletion, and traversal methods.

Uploaded by

gadisa
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 9

CHAPTER FIVE

TREES
Many real life problems can be represented and solved using trees.
Trees are very flexible, versatile and powerful non-liner data structure that can be used to
represent data items possessing hierarchical relationship between the grand father and his
children and grand children as so on.
A tree is a set of nodes and edges that connect pairs of nodes. It is an abstract model of a
hierarchical structure. Rooted tree has the following structure:
 One node distinguished as root.
 Every node C except the root is connected from exactly other node P. P is C's parent, and
C is one of C's children.
 There is a unique path from the root to the each node.
 The number of edges in a path is the length of the path.

Tree Terminologies
Root is a specially designed node (or data items) in a tree. It is the first node in the hierarchical
arrangement of the data items. ‘A‘ is a root node in the Figure below. Each data item in a tree is
called a node. It specifies the data information and links (branches) to other data items.
Consider the following tree

Root: a node without a parent. A


Internal node: a node with at least one child. A, B, F, I, J
External (leaf) node: a node without a child. C, D, E, H, K, L, M, G
Degree of a node is the number of sub trees of a node in a given tree.
Degree of node A is 4
Degree of node B is 2
Degree of node F is 3
Degree of node D is 0
Degree of a tree is the maximum degree of a node in a given tree. In the above tree, degree of a
node A is 4. All the others have less or equal degree. So the degree of the above tree is 4.
Ancestors of a node: parent, grandparent, grand-grandparent, etc of a node.
Ancestors of K A, F, I
Descendants of a node: children, grandchildren, grand-grandchildren etc of a node.
Descendants of F H, I, J, K, L, M
Depth of a node: number of ancestors or length of the path from the root to the node.
Depth of H 2
Height of a tree: depth of the deepest node. 3
Sub tree: a tree consisting of a node and its descendants.

The tree is structured in different levels. The entire tree is leveled in such a way that the root node is
always level 0. Then, its immediate children are at level 1 and their immediate children are at level 2
and soon up to the terminal nodes. That is if a node is at level n, then its children will be at level n+1.

Binary tree data structure


A tree in which each node has at most two children called left child and right child.
A binary tree T is defined as a finite set of elements, called nodes, such that:
 T is empty (i.e. if T has no nodes called null tree or empty tree)
 T contains a special node called Root node of T, and the remaining nodes of T form an
ordered pair of disjointed binary trees T1 and T2, and they are called left and right sub tree of
R. if T1 is non empty then its root is called the left successor of R, similarly if T2 is non
empty then its root is called the right successor of R.

Full or Strictly binary tree: a binary tree where each node has either 0 or 2 children.
The tree is said to be strictly binary tree, if every non-leaf made in a binary tree has non-empty left and
right sub trees. A strictly binary tree with n leaves always contains 2n–1 nodes. That is every node in the
strictly binary tree can have either no children or two children. They are also called 2-tree or extended
binary tree.

Balanced binary tree: a binary tree where each node except the leaf nodes has left and right children
and all the leaves are at the same level.

Complete binary tree: a binary tree in which the length from the root to any leaf node is either h or h-
1 where h is the height of the tree. The deepest level should also be filled from left to right.
Binary search tree (ordered binary tree): a binary tree that may be empty, but if it is not empty it
satisfies the following.
 Every node has a key and no two elements have the same key.
 The keys in the right sub tree are larger than the keys in the root.
 The keys in the left sub tree are smaller than the keys in the root.
 The left and the right sub trees are also binary search trees.

Binary Tree Representation


This section discusses two ways of representing binary tree T in memory:
1. Sequential representation using arrays
2. Linked list representation

Array Representation
An array can be used to store the nodes of a binary tree. The nodes stored in anarray of memory
can be accessed sequentially.
Suppose a binary tree T of depth d. Then at most 2d – 1node can be there in T. (i.e.
SIZE =2d–1) so the array of size “SIZE” to represent the binary tree. Consider the following
binary treeof depth 3. Then SIZE = 23 – 1= 7. Then the array A[7] is declared to hold thenodes.

Figure: binary tree of depth 3

Array representation of the binary tree


The array representation of the binary tree is shown in Figure. To perform any operation often
we have to identify the father, the left child and right child of an arbitrary node.
1. The father of a node having index n can be obtained by (n – 1)/2. For example to find the
father of D, where array index n = 3. Then the father nodes index can be obtained
= (n – 1)/2
= 3 – 1/2
= 2/2
=1 i.e.A[1] is the father D, which is B.

2. The left child of a node having index n can be obtained by (2n+1). For example tofind the left
child of C, where array index n = 2. Then it can be obtained by
= (2n +1)
= 2*2 + 1
=4+1
=5 i.e., A[5] is the left child of C, which is NULL. So no left child for C

3. The right child of a node having array index n can be obtained by the formula (2n+ 2). For
example to find the right child of B, where the array index n = 1. Then
= (2n + 2)
= 2*1 + 2
=4 i.e.A[4] is the right child of B, which is E.

4. If the left child is at array index n, then its right brother is at (n + 1). Similarly, ifthe right child
is at index n, then its left brother is at (n – 1).
The array representation is more ideal for the complete binary tree. The above figure isnot a
complete binary tree. Since there is no left child for node C, i.e., A[5] is vacant. Eventhough
memory is allocated for A[5] it is not used, so wasted unnecessarily.

Linked List Representation


The most popular and practical way of representing a binary tree is using linked list (or pointers).
In linked list, every element is represented as nodes. A node consists of three fields such as:
(a) Left Child (LChild)
(b) Information of the Node (Info)
(c) Right Child (RChild)
The LChild links to the left child node of the parent node. Info holds the information of the every
node and RChild holds the address of right child node of the parent node. The following figure
shows the structure of binary tree node

LChild Info RChild


The following figure shows the linked list representation of the binary tree

B C

D F
E
If a node has left or/and right node, corresponding LChild or RChild is assigned to Null. The
node structure can be logically represented in C/C++ as:

struct Node{
Data type info;
struct Node*LChild;
struct Node*RChild;
}; struct Node*NODE;

Binary Search Trees


A binary tree that may be empty, but if it is not empty it satisfies the following
 Every node has a key and no two elements have the same key.
 The keys in the right sub tree are larger than the keys in the root.
 The keys in the left sub tree are smaller than the keys in the root.
 The left and the right sub trees are also binary search trees.

Operations on Binary Search Tree


The basic operations that are commonly performed on binary tree is listed below
 Create an empty binary tree
 Traversing a binary tree
 Inserting a new node
 Deleting a Node
 Searching for a Node
 Determine the total no: of Nodes
 Determine the total no: of leaf Nodes
 Determine the total no: of non-leaf Nodes
 Find the smallest element in a Node
 Finding the largest element
 Find the Height of the tree
 Finding the Father/Left Child/Right Child/Brother of an arbitrary node
Inserting a Node
Algorithm
Newnode is a pointer variable to hold the address of the newly created node. Data is the
information to be pushed.
1. Input the Data to be pushed and Root node of the tree.
2. Newnode = Create a New Node.
3. If (Root== NULL)
a) Root= Newnode
4. Else If (Data< RootInfo)
(a) Root = Root Lchild
(b) GoTo Step 4
5. Else If (Data> Root Info)
(a) Root = RootRchild
(b) GoTo Step 4
6. If (Data< Root Info)
(a) ROOTLChild = Newnode
7. Else If (Data> RootInfo)
(a) RootRChild = Newnode
8. Else
(a) Display (“DUPLICATE NODE”)
(b) EXIT
9. NewnodeInfo = Data
10. NewnodeLChild = NULL
11. NewnodeRChild = NULL
12. EXIT
Searching a Node
Algorithm
1. Input the Data to be searched and assign the address of the root node to ROOT.
2. If (Data == Root → Info)
(a) Display “The Data exist in the tree”
(b) GoTo Step 6
3. If (Root == NULL)
(a) Display “The DATA does not exist”
(b) GoTo Step 6
4. If(Data > Root →Info)
(a) Root = Root →RChild
(b) GoTo Step 2
5. If(Data < Root →Info)
(a) Root = Root →Lchild
(b) GoTo Step 2
6. Exit
Deleting a Node
This section gives an algorithm to delete a DATA of information from a binary searchtree. First
search and locate the node to be deleted. Then any one of the following conditions arises:
1. The node to be deleted has no children
2. The node has exactly one child (or sub tress, left or right sub tree)
3. The node has two children (or two sub tress, left and right sub tree)
Suppose the node to be deleted is N
If N has no children then simply delete the nodeand place its parent node by the NULL pointer.
If N has one child, check whether it is a right or left child. If it is a right child, thenfind the
smallest element from the corresponding right sub tree. Then replace the smallestnode
information with the deleted node. If N has a left child, find the largest element fromthe
corresponding left sub tree. Then replace the largest node information with the deletednode.
The same process is repeated if N has two children, i.e., left and right child. Randomlyselect a
child and find the small/large node and replace it with deleted node. NOTEthat the tree that we
get after deleting a node should also be a binary search tree.

ALGORITHM
Node is the current position of the tree, which is in under consideration. Loc isthe place where
node is to be replaced. Data is the information of node to be deleted.
1. Find the location Node of the Data to be deleted.
2. If (Node = NULL)
(a) Display “Data is not in tree”
(b) Exit
3. If(Node→ Lchild = NULL)
(a) Loc = Node
(b) Node = Node → RChild
4. If(Node → RChild= =NULL)
(a) Loc = Node
(b) Node = Node → LChild
5. If((Node → Lchild not equal to NULL) && (Node → Rchild not equal to NULL))
(a) Loc = Node → RChild
6. While(Loc → Lchild not equal to NULL)
(a) Loc = Loc→ Lchild
7. Loc → Lchild = Node→ Lchild
8. Loc → RChild= Node → RChild
9. Exit
Traversing Binary Trees Recursively
Tree traversal is one of the most common operations performed on tree data structures.
It is a way in which each node in the tree is visited exactly once in a systematicmanner. There are
three standard ways of traversing a binary tree. They are:
1. Pre Order Traversal (Node-left-right)
2. In order Traversal (Left-node-right)
3. Post Order Traversal (Left-right-node)
Pre Orders Traversal Recursively
To traverse a non-empty binary tree in pre order following steps one to be processed
1. Visit the root node
2. Traverse the left sub tree in preorder
3. Traverse the right sub tree in preorder
That is, in preorder traversal, the root node is visited (or processed) first, beforetraveling through
left and right sub trees recursively. It can be implemented in C/C++ functionas below:
void preorder (Node * Root)
{
If (Root != NULL)
{
cout<<Root → Info;
preorder(Root → L child);
preorder(Root → R child);
}
}

The preorder traversal of a binary tree in Fig. 8.12 is A, B, D, E, H, I, C, F, G, J.

In Order Traversal Recursively


The in order traversal of a non-empty binary tree is defined as follows:
1. Traverse the left sub tree in order
2. Visit the root node
3. Traverse the right sub tree in order
In order traversal, the left sub tree is traversed recursively, before visiting the root.After visiting
the root the right sub tree is traversed recursively, in order fashion. Theprocedure for an in order
traversal is given below:
void inorder (NODE *Root)
{
If (Root != NULL)
{
inorder(Root → L child);
cout<<Root → info;
inorder(Root → R child);
}
}
The in order traversal of a binary tree in Fig. 8.12 is D, B, H, E, I, A, F, C, J, G.

Post Order Traversal Recursively


The post order traversal of a non-empty binary tree can be defined as:
1. Traverse the left sub tree in post order
2. Traverse the right sub tree in post order
3. Visit the root node
In Post Order traversal, the left and right sub tree(s) are recursively processed beforevisiting the
root.
void postorder (NODE *Root)
{
If (Root != NULL)
{
postorder(Root → Lchild);
postorder(Root → Rchild);
cout<<Root à info;
}
}
The post order traversal of a binary tree in Fig. 8.12 is D, H, I, E, B, F, J, G, C, A

You might also like