Trees
Trees
College of Informatics
Department of Computer Science
Data Structure and Algorithm Analysis
Chapter Five
Trees
Daniel Bekele (MSc)
Trees
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.
1
Tree Terminologies
Consider the following tree.
2
Root: a node with out 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
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
3
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
Subtree: a tree consisting of a node and its descendants.
4
Binary tree: a tree in which each node has at most two
children called left child and right child.
Full binary tree: a binary tree where each node has either
0 or 2 children.
5
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.
6
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 subtree are larger than the keys in
the root.
The keys in the left subtree are smaller than the keys in
the root.
The left and the right subtrees are also binary search
trees.
7
Example of Binary search tree (ordered binary tree):
8
Data Structure of a Binary Tree
struct DataModel {
Declaration of data fields
DataModel * Left, *Right;
};
DataModel *RootDataModelPtr=NULL;
Operations on Binary Search Tree
Consider the following definition of binary search tree.
struct Node
{
int num;
Node * Left, *Right;
};
Node *RootNodePtr=NULL;
9
Insertion
When a node is inserted the definition of binary search
tree should be preserved. Suppose there is a binary search
tree whose root node is pointed by RootNodePtr and we
want to insert a node (that stores 17) pointed by
InsNodePtr.
Case 1: There is no data in the tree (RootNodePtr is NULL)
The node pointed by InsNodePtr should be made the
root node.
10
Case 2: There is data
Search the appropriate position.
11
Function call:
if(RootNodePtr = = NULL)
RootNodePtr=InsNodePtr;
else
InsertBST(RootNodePtr, InsNodePtr);
12
Implementation:
void InsertBST(Node *RNP, Node *INP) {
//RNP=RootNodePtr and INP=InsNodePtr
int Inserted=0;
while(Inserted = =0) {
if(RNP->num > INP->num) {
if(RNP->Left = = NULL) {
RNP->Left = INP;
Inserted=1;
}
else
RNP = RNP->Left;
}
13
else {
if(RNP->Right = = NULL) {
RNP->Right = INP;
Inserted=1;
}
else
RNP = RNP->Right;
}
}
}
14
A recursive version of the function can also be given as
follows.
void InsertBST(Node *RNP, Node *INP) {
if(RNP->num > INP->num) {
if(RNP->Left==NULL)
RNP->Left = INP;
else
InsertBST(RNP->Left, INP);
}
else {
if(RNP->Right==NULL)
RNP->Right = INP;
else
InsertBST(RNP->Right, INP);
}
}
15
Traversing
Binary search tree can be traversed in three ways.
Pre order traversal: traversing binary tree in the order
of parent, left and right.
Inorder traversal: traversing binary tree in the order of
left, parent and right.
Postorder traversal: traversing binary tree in the order
of left, right and parent.
16
Example:
Preorder traversal : 10, 6, 4, 8, 7, 15, 14, 12, 11, 13, 18, 16, 17, 19
Inorder traversal : 4, 6, 7, 8, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19
► Used to display nodes in ascending order.
Postorder traversal : 4, 7, 8, 6, 11, 13, 12, 14, 17, 16, 19, 18, 15, 10
17
Application of binary tree traversal
Store values on leaf nodes and operators on internal nodes
Preorder traversal: used to generate mathematical
expression in prefix notation.
Inorder traversal: used to generate mathematical
expression in infix notation.
Postorder traversal: used to generate mathematical
expression in postfix notation.
18
Example:
19
Function calls:
Preorder(RootNodePtr);
Inorder(RootNodePtr);
Postorder(RootNodePtr);
Implementation:
void Preorder (Node *CurrNodePtr) {
if(CurrNodePtr ! = NULL) {
cout<< CurrNodePtr->num;
Preorder(CurrNodePtr->Left);
Preorder(CurrNodePtr->Right);
}
}
20
void Inorder (Node *CurrNodePtr) {
if(CurrNodePtr ! = NULL) {
Inorder(CurrNodePtr->Left);
cout<< CurrNodePtr->num;
Inorder(CurrNodePtr->Right);
}
}
21
Searching
To search a node (whose num value is Number) in a binary
search tree (whose root node is pointed by RootNodePtr), one
of the three traversal methods can be used.
Function call:
ElementExists = SearchBST (RootNodePtr, Number);
// ElementExists is a Boolean variable defined as:
bool ElementExists = false;
Implementation:
bool SearchBST (Node *RNP, int x) {
if(RNP = = NULL)
return(false);
22
else if(RNP->num = = x)
return(true);
else if(RNP->num > x)
return(SearchBST(RNP->Left, x));
else
return(SearchBST(RNP->Right, x));
}
23
Function call:
SearchedNodePtr = SearchBST (RootNodePtr, Number);
// SearchedNodePtr is a pointer variable defined as:
Node *SearchedNodePtr=NULL;
Implementation:
Node *SearchBST (Node *RNP, int x) {
if((RNP = = NULL) || (RNP->num = = x))
return(RNP);
else if(RNP->num > x)
return(SearchBST(RNP->Left, x));
else
return(SearchBST (RNP->Right, x));
}
24
Deletion
To delete a node (whose num value is N) from binary search
tree (whose root node is pointed by RootNodePtr), four cases
should be considered. When a node is deleted the definition of
binary search tree should be preserved.
25
Consider the following binary search tree.
26
Case 1: Deleting a leaf node (a node having no child), e.g. 7
27
Case 2: Deleting a node having only one child, e.g. 2
Approach 1: Deletion by merging
28
Approach 2: Deletion by copying- the following is done
Copy the node containing the largest element in the left (or
the smallest element in the right) to the node containing the
element to be deleted and delete the copied node
29
Case 3: Deleting a node having two children, e.g. 6
Approach 1: Deletion by merging
If the deleted node is the left child of its parent, one of the
following is done
The left child of the deleted node is made the left child of
the parent of the deleted node, and
The right child of the deleted node is made the right child of
the node containing largest element in the left of the deleted
node
OR
The right child of the deleted node is made the left child of
the parent of the deleted node, and
The left child of the deleted node is made the left child of
the node containing smallest element in the right of the
deleted node
30
If the deleted node is the right child of its parent, one of
the following is done
The left child of the deleted node is made the right
child of the parent of the deleted node, and
The right child of the deleted node is made the right
child of the node containing largest element in the left
of the deleted node
OR
The right child of the deleted node is made the right
child of the parent of the deleted node, and
The left child of the deleted node is made the left child
of the node containing smallest element in the right of
the deleted node
31
32
33
Approach 2: Deletion by copying- the following is done
Copy the node containing the largest element in the left (or
the smallest element in the right) to the node containing the
element to be deleted and delete the copied node
34
35
Case 4: Deleting the root node, 10
Approach 1: Deletion by merging
If the tree has only one node the root node pointer is made
to point to nothing (NULL)
If the root node has left child
the root node pointer is made to point to the left child
the right child of the root node is made the right child of the
node containing the largest element in the left of the root
node
If root node has right child
the root node pointer is made to point to the right child
the left child of the root node is made the left child of the
node containing the smallest element in the right of the root
node
36
37
38
Approach 2: Deletion by copying-
Copy the node containing the largest element in the left (or the
smallest element in the right) to the node containing the
element to be deleted and delete the copied node
39
≈ End ≈
40