0% found this document useful (0 votes)
5 views

Lab - Trees

The document provides an overview of trees in computer science, defining key terminologies such as root, siblings, internal nodes, and leaf nodes. It explains binary trees and binary search trees, detailing how to search, insert, and delete nodes within these structures. The document outlines specific steps for deletion based on the number of children a node has, including cases for leaf nodes, nodes with one child, and nodes with two children.

Uploaded by

Sajid Ali
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

Lab - Trees

The document provides an overview of trees in computer science, defining key terminologies such as root, siblings, internal nodes, and leaf nodes. It explains binary trees and binary search trees, detailing how to search, insert, and delete nodes within these structures. The document outlines specific steps for deletion based on the number of children a node has, including cases for leaf nodes, nodes with one child, and nodes with two children.

Uploaded by

Sajid Ali
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 15

Slides (Courtesy of LE Arshia Arif)

Data
Structures
&
Object Oriented
Lab No. 12
Programming
Trees

Dr. Tahir Habib Nawaz


Dr. Ayesha Zeb
LE Hamza Sohail
TREE

• In computer science, a tree is an abstract


model of a hierarchical structure
• A (general) tree is a collection of Nodes and
edges such that
There is a single node from which paths to
other nodes emerge; this node is called the root
There is a single,unique path to reach any
node of the tree
Tree Terminologies

 Root: node without parent (A)


 Siblings: Children of the Same parent
 Internal node: node with at least one child,
Non-leaf nodes are called internal nodes
• (A, B, C, F)
 Leaf (aka External node): node without children
(E, I, J,
K, G, H, D)
 Depth of a node: number of ancestors
 Height of a tree: maximum depth of any node (3)
 If a tree is a collection of N nodes, then it
has N-1 edges.
Binary
Tree
A finite set of elements that is either
empty or is partitioned into three
disjoint subsets.
 The first subset contains a single
element called the root of the tree
 The other two subsets are
themselves binary trees, called the
left and right sub-trees of the original
tree
 Each element of a binary tree is called
a node
We call the children of an internal
node left child and right child
Binary Search
Tree
A particular form of binary tree,
such that for any node in the
tree containing data value x:
• The right sub-tree has data
values > x
• The left sub-tree has data
values < x
Pointer-Based
Implementation of a
Binary of
Structure (Search)
Node: Tree:
struct Node
{
Int data;
Node *Left;
Node * Right;
};
Searching a Node in a binary
search tree:
 To search a specific node in a binary search tree one has to traverse
through the tree until the specific node is found.
 If the value of x, to be found, is less than the parent node then one
should move to the left child.
 If the value of x, to be found, is greater than the parent node then
one should move to the right child.
 If the value of x, to be found, is equal to the parent node then it
means that the node is found.
 Otherwise it means that x is not present in the tree.
Example
Insertion
To insert a new node following steps should be
followed:
1. Create a new node
2. Check if the tree is empty
3. Otherwise traverse through the tree in such a way
that
 if the coming data is less than the root data then
move to the left child
 if the coming data is greater than the root data
then move to the right child
 else if the number matches the root data then the
new node would not be inserted.
4. Traverse until you reach the leaf node.
Deletion of a node in a Binary
Search Tree:
There are three cases for deletion in BST:
• Deletion of Leaf with No Child.
• Deletion of node with 1 Child.
• Deletion of node with 2 Child.
Deletion of Leaf (No Children):
1. Search for the node that needs to be deleted.
2.Node to be deleted is either on left side or on right side of its
parent node.
3.If node to be deleted is on left side of its parent node then mark
Left side of parent node to null.
4.If node to be deleted is on right side of its parent node then
mark Right side of parent node to null.
Example
Deletion on node with one Child:
1. Search for the node that needs to be deleted.
2. Node to be deleted is either on left side or on right side of its parent node.
3.Node to be deleted has only child present that can be on left side or
right side.
4.If node to be deleted has left child present, then directly connect
its parent node to left child of the node to be deleted.
5.If node to be deleted has right child present, then directly connect
its parent node to right child of the node to be deleted.
6.In this way node to be deleted will be deleted from the Tree and the
parent node will be directly connected to child of the node to be deleted.
Example
Deletion of Node with two Children:

1.Find a minimum value in


the right subtree;
2.Replace value of the
node to be removed with the
found minimum.

Now, right subtree contains a


duplicate!
3. Apply delete() to the right
subtree to delete a duplicate.

You might also like