0% found this document useful (0 votes)
22 views32 pages

Lecture 6

The document discusses different types of tree data structures including binary trees and binary search trees. It defines key tree terminology like nodes, edges, root, height and describes different tree traversal algorithms like inorder, preorder and postorder. It also explains different types of binary trees such as full, perfect, complete and balanced binary trees.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views32 pages

Lecture 6

The document discusses different types of tree data structures including binary trees and binary search trees. It defines key tree terminology like nodes, edges, root, height and describes different tree traversal algorithms like inorder, preorder and postorder. It also explains different types of binary trees such as full, perfect, complete and balanced binary trees.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 32

Lecture 6: Tree

Md. Nazmul Abdal


Lecturer
Department of CSE
University of Liberal Arts Bangladesh (ULAB)
Introduction

⚫ A tree is a nonlinear hierarchical data structure that consists of nodes


connected by edges.
⚫ Different tree data structures allow quicker and easier access to the
data as it is a non-linear data structure.
⚫ Every tree is a combination of
⚫ A node carrying data
⚫ Two subtrees
Syntax
struct node {
int data;
struct node* left;
struct node* right;
}
Terminologies

Node
⚫ A node is an entity that contains a key or value and pointers to
its child nodes.
⚫ The last nodes of each path are called leaf nodes or external
nodes that do not contain a link/pointer to child nodes.
⚫ The node having at least a child node is called an internal node.

Edge
⚫ It is the link between any two nodes.

Root
⚫ It is the topmost node of a tree.
Terminologies

Height of a Node
⚫ The height of a node is the number of edges from the node to the
deepest leaf (ie. the longest path from the node to a leaf node).
Depth of a Node
⚫ The depth of a node is the number of edges from the root to the node.
Height of a Tree
⚫ The height of a Tree is the height of the root node or the depth of the
deepest node.
Terminologies

Degree of a Node
⚫ The degree of a node is the total number of branches of that node.

Forest
⚫ A collection of disjoint trees is called a forest.
Tree Traversal

⚫ Traversing a tree means visiting every node in the tree.

⚫ You might want to add all the values in the tree or find the largest one.
For all these operations, you will need to visit each node of the tree.

⚫ Linear data structures like arrays, stacks, queues, and linked list have
only one way to read the data.

⚫ But a hierarchical data structure like a tree can be traversed in


different ways.
Inorder Traversal

⚫ First, visit all the nodes in the left subtree

⚫ Then the root node

⚫ Visit all the nodes in the right subtree

Code

inorder(root->left)

display(root->data)

inorder(root->right)
Preorder Traversal

⚫ Visit root node

⚫ Visit all the nodes in the left subtree

⚫ Visit all the nodes in the right subtree

Code

display(root->data)

preorder(root->left)

preorder(root->right)
Postorder Traversal

⚫ Visit all the nodes in the left subtree

⚫ Visit all the nodes in the right subtree

⚫ Visit the root node

Code

postorder(root->left)

postorder(root->right)

display(root->data)
Example (Inorder Traversal)
Implementation

#include <stdio.h> // preorderTraversal traversal


#include <stdlib.h> void preorderTraversal(struct
node* root) {
struct node { if (root == NULL) return;
int item; printf("%d ->", root->item);
struct node* left; preorderTraversal(root->left);
struct node* right; preorderTraversal(root->right);
}; }

// Inorder traversal // postorderTraversal traversal


void inorderTraversal(struct node* void postorderTraversal(struct
root) { node* root) {
if (root == NULL) return; if (root == NULL) return;
inorderTraversal(root->left); postorderTraversal(root->left);
printf("%d ->", root->item); postorderTraversal(root->right);
inorderTraversal(root->right); printf("%d ->", root->item);
} }
Implementation

// Create a new Node


struct node* createNode(value) { int main() {
struct node* newNode = malloc(sizeof(struct struct node* root = createNode(1);
node));
insertLeft(root, 12);
newNode->item = value;
insertRight(root, 9);
newNode->left = NULL;
newNode->right = NULL;
insertLeft(root->left, 5);
return newNode;
insertRight(root->left, 6);
}
// Insert on the left of the node
printf("Inorder traversal \n");
struct node* insertLeft(struct node* root, int value) {
inorderTraversal(root);
root->left = createNode(value);
return root->left;
printf("\nPreorder traversal \n");
}
preorderTraversal(root);
// Insert on the right of the node
struct node* insertRight(struct node* root, int value)
printf("\nPostorder traversal \n");
{
postorderTraversal(root);
root->right = createNode(value);
}
return root->right;
}
Binary Tree

⚫ A binary tree is a tree data structure in which each parent node can have
at most two children. Each node of a binary tree consists of three items:
⚫ data item
⚫ address of left child
⚫ address of right child
Full Binary Tree

⚫ A full Binary tree is a special type of binary tree in which every parent
node/internal node has either two or no children.
Perfect Binary Tree

⚫ A perfect binary tree is a type of binary tree in which every internal node
has exactly two child nodes and all the leaf nodes are at the same level.
Complete Binary Tree

⚫ Complete binary tree is just like a full binary tree, but with some major
differences:
⚫ Every level must be completely filled
⚫ All the leaf elements must lean towards the left.
⚫ The last leaf element might not have a right sibling i.e. a complete
binary tree doesn't have to be a full binary tree.
Degenerate or Pathological Tree

⚫ A degenerate or pathological tree is the tree having a single child either


left or right.
Skewed Binary Tree

⚫ A skewed binary tree is a pathological/degenerate tree in which the tree


is either dominated by the left nodes or the right nodes.
⚫ Thus, there are two types of skewed binary tree:
⚫ Left-skewed binary tree
⚫ Right-skewed binary tree.
Balanced Binary Tree

⚫ It is a type of binary tree in which the difference between the height of


the left and the right subtree for each node is either 0 or 1.
Binary Tree Representation

struct node
{
int data;
struct node *left;
struct node *right;
};
Implementation

#include <stdio.h> // Preorder traversal


#include <stdlib.h> void preorderTraversal(struct node* root) {
if (root == NULL) return;
struct node { printf("%d ->", root->item);
int item; preorderTraversal(root->left);
struct node* left; preorderTraversal(root->right);
struct node* right; }
};
// Postorder traversal
// Inorder traversal void postorderTraversal(struct node* root) {
void inorderTraversal(struct node* root) { if (root == NULL) return;
if (root == NULL) return; postorderTraversal(root->left);
inorderTraversal(root->left); postorderTraversal(root->right);
printf("%d ->", root->item); printf("%d ->", root->item);
inorderTraversal(root->right); }
}
Implementation

// Create a new Node


int main() {
struct node* createNode(value) {
struct node* root = createNode(1);
struct node* newNode = malloc(sizeof(struct node));
insertLeft(root, 2);
newNode->item = value;
insertRight(root, 3);
newNode->left = NULL;
insertLeft(root->left, 4);
newNode->right = NULL;
return newNode;
printf("Inorder traversal \n");
}
inorderTraversal(root);
// Insert on the left of the node
struct node* insertLeft(struct node* root, int value) {
printf("\nPreorder traversal \n");
root->left = createNode(value);
preorderTraversal(root);
return root->left;
}
printf("\nPostorder traversal \n");
// Insert on the right of the node
postorderTraversal(root);
struct node* insertRight(struct node* root, int value) {
}
root->right = createNode(value);
return root->right;
}
Binary Search Tree

⚫ Binary search tree is a data structure that quickly allows us to maintain a


sorted list of numbers.
⚫ It is called a binary tree because each tree node has a maximum of two
children.
⚫ It is called a search tree because it can be used to search for the presence
of a number.
⚫ The properties that separate a binary search tree from a regular binary
tree is:
⚫ All nodes of left subtree are less than the root node
⚫ All nodes of right subtree are more than the root node
⚫ Both subtrees of each node are also BSTs i.e. they have the above
two properties
Binary Search Tree
Search Operation

Algorithm

If root == NULL
return NULL;
If number == root->data
return root->data;
If number < root->data
return search(root->left)
If number > root->data
return search(root->right)
Insertion Operation

Algorithm

If node == NULL
return createNode(data)
if (data < node->data)
node->left = insert(node->left, data);
else if (data > node->data)
node->right = insert(node->right, data);
return node;
Deletion Operation

Case I
In the first case, the node to be deleted is the leaf node. In such a case, simply delete the
node from the tree.

Case II
In the second case, the node to be deleted lies has a single child node. In such a case
follow the steps below:
1. Replace that node with its child node.
2. Remove the child node from its original position.

Case III
In the third case, the node to be deleted has two children. In such a case follow the steps
below:
1. Get the inorder successor of that node.
2. Replace the node with the inorder successor.
3. Remove the inorder successor from its original position.
Deletion Operation
Implementation

#include <stdio.h> // Traverse root


#include <stdlib.h> printf("%d -> ", root->key);
struct node { // Traverse right
int key; inorder(root->right);
struct node *left, *right; }
}; }
// Create a node // Insert a node
struct node *newNode(int item) { struct node *insert(struct node *node, int key) {
struct node *temp = (struct node *)malloc(sizeof(struct node)); // Return a new node if the tree is empty
temp->key = item; if (node == NULL) return newNode(key);
temp->left = temp->right = NULL; // Traverse to the right place and insert the node
return temp; if (key < node->key)
} node->left = insert(node->left, key);
// Inorder Traversal else
void inorder(struct node *root) { node->right = insert(node->right, key);
if (root != NULL) { return node;
// Traverse left }
inorder(root->left);
Implementation

// Find the inorder successor // Find the node to be deleted


struct node *minValueNode(struct node *node) { if (key < root->key)
struct node *current = node; root->left = deleteNode(root->left, key);
else if (key > root->key)
// Find the leftmost leaf root->right = deleteNode(root->right, key);
while (current && current->left != NULL)
current = current->left; else {
// If the node is with only one child or no child
return current; if (root->left == NULL) {
} struct node *temp = root->right;
// Deleting a node free(root);
struct node *deleteNode(struct node *root, int key) { return temp;
// Return if the tree is empty } else if (root->right == NULL) {
if (root == NULL) return root; struct node *temp = root->left;
free(root);
return temp;
}
Implementation

// Driver code
// If the node has two children int main() {
struct node *temp = minValueNode(root->right); struct node *root = NULL;
root = insert(root, 8);
// Place the inorder successor in position of the root = insert(root, 3);
node to be deleted
root = insert(root, 1);
root->key = temp->key;
root = insert(root, 6);
root = insert(root, 7);
// Delete the inorder successor
root = insert(root, 10);
root->right = deleteNode(root->right, temp->key);
root = insert(root, 14);
}
root = insert(root, 4);
return root;
printf("Inorder traversal: ");
}
inorder(root);
printf("\nAfter deleting 10\n");
root = deleteNode(root, 10);
printf("Inorder traversal: ");
inorder(root);
}
Thank You

You might also like