Chapter 6 Tree
Chapter 6 Tree
7/16/2024
Definition of Tree Structures
Binary Trees and Binary Search Trees
Basic Tree Operations
Traversing in a Binary Tree
General Trees and Their Implementations
7/16/2024 2
A tree structure is a way of representing
the hierarchical nature of a structure in a
graphical form.
7/16/2024 3
7/16/2024 4
The root of a tree is the node with no parents.
There can be at most one root node in a tree
(node A in the above example
An edge refers to the link from parent to child
(all links in the figure).
A node with no children is called leaf node
(E,J,K,H and I).
Children of same parent are called siblings (B,
C, D are siblings of A and E, F are the siblings
of B). 7/16/2024 5
A node p is an ancestor of a node q if there
exists a path from root to q and p appears on
the path. For example, A, C and G are the
ancestors for K
The node q is called a descendant of p.
Set of all nodes at a given depth is called
level of the tree (B, C and D are same level).
The root node is at level zero.
7/16/2024 6
7/16/2024 7
The depth of a node is the length of the path from the root to the node
(depth of G is 2, A - C - G).
The height of a node is the length of the path from that node to the
deepest node.
The height of a tree is the length of the path from the root to the
A (rooted) tree with only one node (the root) has a height of zero.
7/16/2024 12
7/16/2024 13
Strict Binary Tree: A binary tree is
called strict binary tree if each node has
exactly two children or no children.
7/16/2024 14
Full Binary Tree: A binary tree is called full binary tree if each node has
exactly two children and all leaf nodes are at same level.
7/16/2024 15
Complete binary tree
A binary tree is called complete binary tree if all leaf nodes are at height h
or h - 1 and also without any missing number in the sequence.
7/16/2024 16
7/16/2024 17
Binary Search Tree is a node-based binary tree
data structure which has the following
properties:
The left subtree of a node contains only nodes
with keys lesser than the node’s key.
The right subtree of a node contains only
nodes with keys greater than the node’s key.
The left and right subtree each must also be a
binary search tree.
7/16/2024 18
7/16/2024 19
Searching a key
To search a given key in Binary Search Tree,
we first compare it with root, if the key is
present at root, we return root.
If key is greater than root’s key, we recur for
right subtree of root node.
Otherwise we recur for left subtree.
7/16/2024 20
Illustration to search 6 in above tree:
7/16/2024 21
1.int SearchBST (
2.Node *RootNodePtr, int X)
3.{
4.if(RootNodePtr == NULL)
5.return 0; // 0 means (false, not found).
6.else
7.if(RootNodePtr ->Num == X)
8.return 1; // 1 means (true, found).
9.else if(RootNodePtr ->Num > X)
10.return(SearchBST(RootNodePtr ->Left, X));
11.else
12.return(SearchBST(RootNodePtr ->Right, X));
13.}
7/16/2024 22
Insertion of a key
A new key is always inserted at leaf.
We start searching a key from root till we
hit a leaf node.
Once a leaf node is found, the new node is
added as a child of the leaf node.
7/16/2024 23
7/16/2024 24
void InsertBST( ) NP->Left = InsNodePtr;
{ Inserted=1;
Node *InsNodePtr; }
cin>>InsNodePtr->Num; }
InsNodePtr->Left=NULL; else {
if(RootNodePtr== NULL)
{
RootNodePtr=InsNodePtr;
NP->Right = InsNodePtr;
else
Inserted=1;
}
{
else
Node *NP=RootNodePtr;
NP = NP->Right;
int Inserted=0;
}
while(Inserted ==0)
}
{
}
if(NP->Num > InsNodePtr->Num)
}
{
{
Binary search tree can be traversed in three
ways.
PreOrder 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.
Example:
7/16/2024 26
7/16/2024 27
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
7/16/2024 28
Store values on leaf nodes and operators on internal nodes:
7/16/2024 29
7/16/2024 30
Preorder traversal: + – A * B C + D / E F ==> Prefix notation
Inorder traversal: A – B * C + D + E / F ==> Infix notation
Postorder traversal: A B C * – D E F / + + ==> Postfix notation
7/16/2024 31
Preorder traversal
1. Process the value in the root (e.g. print the root value).
2. Traverse the left subtree with a preorder traversal.
3. Traverse the right subtree with a preorder traversal.
Inorder traversal :-
prints the node values in ascending order:
1. Traverse the left subtree with an inorder traversal.
2. Process the value in the root (e.g. print the root value).
3. Traverse the right subtree with an inorder traversal.
Postorder traversal
1. Traverse the left subtree with a postorder traversal.
2. Traverse the right subtree with a postorder traversal.
7/16/2024 32
3. Process the value in the root (e.g. print the root value).
7/16/2024 33
7/16/2024 34