Trees - Binary Tree and (Binary Search Tree)
Trees - Binary Tree and (Binary Search Tree)
I
T
2
0 Data Structure
8
Lecture
11 Dr. Belal Murshed
Data Structures and Algorithms
I
T
2 Trees
0
8
Introduction
Trees - Introduction
• Arrays
I
• Linked List
T
2
• Stacks
0
• Queues
8
• Tree
• Graph
3 Dr. Belal Murshed
Data Structures and Algorithms
Trees - Introduction
• Definition
I oA tree is a hierarchical data structure that
T organizes data elements, called nodes, by
connecting them with links, called branches
2 Leaves
• Examples
0
oDates Tree
8 oPalm Tree
• Tree has Branches
Roots
Trees - Introduction
• Tree in nature consists of Leaves
I oRoots
T oBranches
2 oLeaves
Branches
0 • Tree in Computing Roots
Trees - Introduction
• Array’s members
I oElements
T • Element of array
Trees - Introduction
• Relationship
I oArrays
T • Predecessor - Successor
2 oLinked List
• Predecessor - Successor
0 oTree
8 • Parent - Child
Trees - Introduction
• Size Measurement
I o Arrays
• Length of array
T • # of elements i.e. 11
o Linked List
2 • Length of linked list
• # of nodes i.e. 7 Level 0 ……….……..
0 o Tree
• Height or Depth of Tree Level 1….…..
8 • # of edges along longest path
from the root to a leaf in that Level 2 ...
tree
• Height is the maximum distance Level 3 ……..
from root to leaf Hieght=3
• Tree with one node has height 0
8 Dr. Belal Murshed
Data Structures and Algorithms
Trees - Introduction
• Types of Node
I oChild Node
T • A node in a tree can have several successors, which
we refer to as children
2 • 2 & 6 are the children of 5
0 oParent Node
• A node’s predecessor would be its parent
8
• 5 is parent of 2 and 6 & 8 is parent of 7 and 9
Trees - Introduction
• Node Types (Cont.)
I oRoot Node
T • A node which has no parent(parentless)
• 5 is the root node
2 oLeaf Node
• A node which has no child (childless)
0
• 1, 3, 7, 9 are leaf nodes
8 oInterior Node
• A node which is neither a root nor a leaf
• 2, 4, 6, 8 are interior nodes
Trees - Summary
• Abstract Data Type (ADT)
I • Hierarchical Data Structure
• Nodes
T o Root, Parent, Child, Leaf, Interior
2 • Parent – Child relationship
• Size is measured in terms of height
0 o # of edges from root to leaf along the longest path
• Logical Data Structure
8
• Implementation
o Array
o Linked List
I
T
2 Trees
0
8
Types of Tree
Trees - Introduction
• Types of Tree
I oGeneral Tree
T • Any number of roots
• Any number of children
2 per node
0 oBinary Tree
• Only one root
8
• Maximum 2 children per
node
L M N O P Q
A B C D E F G H I J K L M N O P Q
16 Dr. Belal Murshed
Data Structures and Algorithms
I
T
2 Binary Trees
0
8
Introduction
0
8
22
g h Dr. Belal Murshed
Data Structures and Algorithms
𝒏 = 𝟐𝒉+𝟏 − 𝟏 ⇒ n + 1 = 2ℎ+1
2 log(n + 1) = log 2ℎ+1
0 log(n + 1) = h + 1 𝑙𝑜𝑔2
ℎ + 1 = 𝑙𝑜𝑔2 (𝑛 + 1)
8 ℎ = 𝑙𝑜𝑔2 𝑛 + 1 − 1
ℎ = 𝑙𝑜𝑔2 𝑛 + 1 − 1 ⇒ ℎ = 𝑙𝑜𝑔2 𝑛 + 1
upper Below
𝒉 = 𝒍𝒐𝒈𝟐 𝒏 ,
𝒉 𝑖𝑠 𝑡ℎ𝑒 𝑚𝑖𝑛𝑖𝑚𝑢𝑚 𝒍𝒐𝒈𝟐 𝒏
26 Dr. Belal Murshed
Data Structures and Algorithms
I
T
2 Binary Trees
0
8
Traversal
• Pre Order
I oRoot – Left - Right
T
2
0
8 Traversal: a b d g h e i c f j
• In Order
I oLeft - Root –Right
T
2
0
8 Traversal: g d h b e i a f j c
I
T
2 Binary Trees
0
8
Implementation
I
T
2 Binary Trees
0
8
Implementation
BinaryTree
// A binary tree node has data, pointer to left child
// and a pointer to right child
I struct Node {
int data;
T struct Node *left, *right;
};
2
// Utility function to create a new tree node
0 Node* newNode(int data)
{
8 Node* temp = new Node;
temp->data = data;
temp->left = temp->right = NULL;
return temp;
}
37 Dr. Belal Murshed
Data Structures and Algorithms
Inorder BinaryTree
// Given a binary tree, print its nodes in inorder
void printInorder(struct Node* node)
I {
if (node == NULL)
T return;
BinaryTree
// Driver code
int main()
I {
struct Node* root = newNode(1);
T root->left = newNode(2);
root->right = newNode(3);
2 root->left->left = newNode(4);
root->left->right = newNode(5);
0
// Function call
8 cout << "Inorder traversal of binary tree is \n";
printInorder(root);
return 0;
}
39 Dr. Belal Murshed
Data Structures and Algorithms
I
T Binary Tree
2 Implementation
0
8
Traversal
I
T Binary Tree
2 Implementation
0
8
Searching
I
T
Binary Search Tree
2 (BST)
0
8
Introduction
BST - Introduction
• Value of left child is
I smaller than root
T • Value of right child is
2 greater or equal to the
0 root
8 • This order property is
applicable to each and
every node
47 Dr. Belal Murshed
Data Structures and Algorithms
BST - Introduction
44
I
24 65
T
2 56 88
20 36
0
8 15 28 40 62
8 19 30 42 58 64
BST - Introduction
• Summary
I oSearching is efficient
T
2 oLeft child is always smaller
0
oRight child is always greater or equal
8
oSearching will take O (log2 n)
I
T
Binary Search Tree
2 (BST)
0
8
Operations
BST - Operations
• Traversal
I oAccessing/visiting all values of a tree once
T • Searching
oFinding a key value
2 • Insertion
oAdding a value in a tree
0
• Deletion
8 oRemoving a value from tree
• Creation
oCreating a tree from empty tree
51 Dr. Belal Murshed
Data Structures and Algorithms
BST - Traversal
• Pre Order Traversal
I o8,3,1,6,4,7,10,14,13
T • In Order Traversal
o1,3,4,6,7,8,10,13,14
2 • Post Order Traversal
o1,4,7,6,3,13,14,10,8
0
• Depth First Search
8 o8,3,1,6,4,7,10,14,13
• Breadth First Search
o8,3,10,1,6,14,4,7,13
52 Dr. Belal Murshed
Data Structures and Algorithms
BST - Searching
• Order Property should be followed
I 1) Start from root
2) Compare with root
T 3) Go right if the key value is greater than or equal
to root
2 4) Go left if the key value is less than the root
0 • Keep doing this till you find it
• return True
8 • or reach to an empty position
o return False
BST - Searching
• Key = 6 root
I oTrue
T • Key = 13
2 oTrue
0 • Key = 0
8 oFalse
• Key = 20
oFalse
54 Dr. Belal Murshed
Data Structures and Algorithms
BST - Insertion
• Order Property should be maintained
I 1) Start from root
T 2) Go right if the new value is greater than or
2 equal to the root
3) Go left if the new value is less than the root
0 4) Keep doing this till you come to an empty
8 position
• Insert the value
BST - Insertion
• Key = 0 root
I oNull
T • Key = 30
2 oNull
0
8
BST - Deletion
• There are 3 possible cases
I oDeletion of a leaf node
T
2 oDeleting a node with one child
0
oDeleting a node with two children
8
2 6 14 6 14
0
4 8 12 18 4 8 18
8
2 16 2 16
8 4 8 12 18 2 8 12 18
2 16 16
6 14
0 6 18
8 4 8 18
4 8 16
2 16 15 17
2
15 17
Dr. Belal Murshed
Data Structures and Algorithms
10 25 43
64 Dr. Belal Murshed
Data Structures and Algorithms
0 59
8 25 67
13 32 63 72
10 15 43
65 Dr. Belal Murshed
Data Structures and Algorithms
2 10 15 25 43
0
8
2 10 15 25 43
0
8
BST - Creation
• Creating a BST is really nothing more than a
I series of insertions (calling the insert
T method over and over)
2 oCreate a new node
oCall the insertion method
0
oNew node will be inserted as a leaf
8 oRepeat this process as long as you want to
insert
BST - Creation
• Let’s assume we insert the following data
I values, in their order of appearance into
T an initially empty BST:
10
10, 14, 6, 2, 5, 15, and 17
2
0 Step 1:
Create a new node with value 10
8 Insert node into tree
The tree is currently empty
New node becomes the root
BST - Creation
10, 14, 6, 2, 5, 15, and 17
I
T Step 2:
Create a new node with value 14 10
2 This node belongs in the right subtree of
node 10 14
0 Since 14 > 10
of node 10
BST - Creation
10, 14, 6, 2, 5, 15, and 17
I
Step 3:
T Create a new node with value 6 10
2 This node belongs in the left subtree of
node 10
6 14
0 Since 6 < 10
node 10
BST - Creation
10, 14, 6, 2, 5, 15, and 17
I Step 4:
Create a new node with value 2
T This node belongs in the left subtree of 10
node 10
2 Since 2 < 10
6 14
0 The root of the left subtree is 6
The new node belongs in the left subtree
8 of node 6 2
Since 2 < 6
BST - Creation
10, 14, 6, 2, 5, 15, and 17
I Step 5:
T Create a new node with value 5
10
This node belongs in the left subtree of
2 node 10
Since 5 < 10 6 14
0 The new node belongs in the left subtree
of node 6
8 Since 5 < 6
2
BST - Creation
10, 14, 6, 2, 5, 15, and 17
I Step 6:
Create a new node with value 15
T This node belongs in the right subtree of 10
node 10
2 Since 15 > 10
6 14
0 The new node belongs in the right
subtree of node 14
8 Since 15 > 14 2 15
The right subtree of node 14 is empty
So node 15 becomes right child of node 5
14
BST - Creation
10, 14, 6, 2, 5, 15, and 17
I Step 7:
Create a new node with value 17
T This node belongs in the right subtree of 10
node 10
2 Since 17 > 10
6 14
0 The new node belongs in the right
subtree of node 14
8 Since 17 > 14 2 15
And the new node belongs in the right
subtree of node 15 5 17
Since 17 > 15
I
T
Binary Search Tree
2 (BST)
0
8
Implementation
Class – BinarySearchTree
public class BinarySearchTree {
private intBSTnode root;
I
// Constructor
T public BinarySearchTree() {
2 root = null;
}
0
8 }
// All methods will be written here
I
T
2 Binary Search Tree
0
8
Operations
BST - Operations
• Traversal
I oAccessing/visiting all values of a tree once
T • Searching
oFinding a key value
2 • Insertion
oAdding a value in a tree
0
• Deletion
8 oRemoving a value from tree
• Creation
oCreating a tree from empty tree
80 Dr. Belal Murshed
Data Structures and Algorithms
BST - Traversal
I
• Pre Order Traversal
T
• In Order Traversal
2
• Post Order Traversal
0
• Depth First Search
8
• Breadth First Search
I
T
2 Binary Search Tree
0
8
Searching
(Iterative Solution)
I
T
2 Binary Search Tree
0
8
Searching
(Recursive Solution)
}
BST – Searching - Recursive
if (p == null) {
return(false);
if (data == p.data) {
return(true); root
}
I
else if (data < p.data) {
return(recurSearch(p.left, data));
}
T
else {
return(recurSearch(p.right, data));
}
} 2
0
8
I
T
2 Binary Search Tree
0
8
Insertion
BST – Insertion
public intBSTnode insert(intBSTnode p, int data) {
if (p == null) {
I p = new intBSTnode(data);
}
T else {
if (data < p.data) {
2 p.left = insert(p.left, data);
}
0 else {
p.right = insert(p.right, data);
8 }
}
return p;
}
else {
2 }
p.right = insert(p.right, data);
}
0
return p;
I
T
2 Binary Search Tree
0
8
Deletion
BST – Deletion
• Find node to be deleted
I
• Find Parent of node
T
• Check node is a “leaf”
2
• Has only Left Child
0
• Has only Right Child
8
• Find Smallest node
• Find Largest node
93 Dr. Belal Murshed
Data Structures and Algorithms
T else {
if (data == p.getData())
return p;
else if (data <p.getData())
2 return findNode(p.getLeft(), data); 59
else
return findNode(p.getRight(), data);
0 }
18 67
}
8
13 32 63 72
10 15 25 43
10 15 25 43
T
59
2
18 67
0
13 32 63 72
8
10 15 25 43
T
59
2
18 67
0
13 32 63 72
8
10 15 25 43
T
59
2
18 67
0
13 32 63 72
8
10 15 25 43
10 15 25 43
10 15 25 43
BST – Deletion
newnode2delete = minNode(node2delete.getRight());
I saveValue = newnode2delete.getData();
p = delete(p, saveValue);
T node2delete.setData(saveValue);
return p;
2 }
0
8
I
T
2
0
8 Allah (Alone) is Sufficient for us,
and He is the Best Disposer of
affairs (for us).(Quran 3 : 173)
I
T
2
0
8