Lecture 6
Lecture 6
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
⚫ 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.
Code
inorder(root->left)
display(root->data)
inorder(root->right)
Preorder Traversal
Code
display(root->data)
preorder(root->left)
preorder(root->right)
Postorder Traversal
Code
postorder(root->left)
postorder(root->right)
display(root->data)
Example (Inorder Traversal)
Implementation
⚫ 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
struct node
{
int data;
struct node *left;
struct node *right;
};
Implementation
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
// 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