Tree
Tree
TREE
Data
Structure
INTRODUCTION
OUTLINE
TREE
STRUCTUR
E
BINARY
TREES
BINARY
SEARCH
TREES
INTRODUCTION
What is a Tree DS?
• Non- linear data structure
• A hierarchical structure
• Uses to represent and organize data in a way that is easy
to navigate and search rr
⚬ Databases
⚬ Networking (e.g., routing)
⚬ Artificial Intelligence (e.g., decision trees, random
forest).
INTRODUCTION
Structure of a Tree DS
rr
INTRODUCTION
Structure of a Tree DS
rr
TER MIN O LO GIES
Terminologies Associate with Tree DS
• Parent Node: The node which is an immediate predecessor of a
node is called the parent node of that node.
• Child Node: The node which is the immediate successor of a node
is called the child node of that node.
• Root Node: The topmost node of a tree or the node which does
rr
not have any parent node is called the root node.
• Leaf Node: The nodes which do not have any child nodes are
called leaf nodes.
• Ancestor of a Node: Any predecessor nodes on the path of the
root to that node are called Ancestors of that node.
• Descendant: A node x is a descendant of another node y if and
only if y is an ancestor of x
• Sibling: Children of the same parent node are called siblings.
TER MIN O LO GIES
Terminologies Associate with Tree DS
• Level of a node: The count of edges on the path from the
root node to that node. The root node has level 0.
rr
• Neighbor of a Node: Parent or child nodes of that node are
called neighbors of that node.
rr
B IN A RY T R EES
rr
TYP ES O F B IN A RY T R EE
TYP ES O F B IN A RY T R EE
Degenerate Tree
• In-order:
10,20,30,100,150,200,300
BIN A RY SEA R C H TR EES
(T R AV ER SA L)
• Pre-Order Traversal: At first visit the root then traverse left
subtree and then traverse the right subtree.
• Steps:
⚬ Visit the root and print the data.
⚬ Traverse left subtree
⚬ Traverse the right subtree
• Pre-order:
100,20,10,30,200,150,300
B IN A RY T R EES
(T R AV ER SA L)
• Post-Order Traversal: At first traverse left subtree then traverse
the right subtree and then visit the root.
• Steps:
⚬ Traverse left subtree
⚬ Traverse the right subtree
⚬ Visit the root and print the data.
• Post-order:
10,30,20,150,300,200,100
BIN A RY SEA R C H TR EES
• BST is a data structure used in
computer science for organizing
and storing data in a sorted
manner.
• Each node in a Binary Search
Tree has at most two children, a
left child and a right child,
• The left child containing values
less than the parent node and
the right child containing values
greater than the parent node.
• Allows for efficient searching,
insertion, and deletion
operations on the data stored in
the tree.
BIN A RY SEA R C H TR EES
• Properties of BST:
• 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.
• There must be no duplicate
nodes(BST may have duplicate
values with different handling
approaches).
BIN A RY SEA R C H TR EES
• Insertion in BST
⚬ Initilize the current node (say, currNode or node) with root node
⚬ Compare the key with the current node.
⚬ Move left if the key is less than or equal to the current node value.
⚬ Move right if the key is greater than current node value.
⚬ Repeat steps 2 and 3 until you reach a leaf node.
⚬ Attach the new key as a left or right child based on the
comparison with the leaf node’s value.
BIN A RY SEA R C H TR EES
(IN SERT IO N )
BIN A RY SEA R C H TR EES
(IN SERT IO N )
BIN A RY SEA R C H TR EES
(SER A C H O P ER AT IO N )
BIN A RY SEA R C H TR EES
(SER A C H O P ER AT IO N )
BIN A RY SEA R C H TR EES
(D ELET IO N )
• Delete a Leaf Node
⚬ Simply, the node can be deleted by assigning null to the
deleting node
BIN A RY SEA R C H TR EES
(D ELET IO N )
• Delete a Node with Single Child in BST
⚬ Deleting a single child node is also simple in BST. Copy the child
to the node and delete the node.
BIN A RY SEA R C H TR EES
(D ELET IO N )
• Delete a Node with Both Children in BST
⚬ Deleting a node with both children is not so simple.
⚬ Have to delete the node is such a way, that the resulting tree
follows the properties of a BST.
⚬ The trick is to find the inorder successor of the node.
⚬ Copy contents of the inorder successor to the node, and delete
the inorder successor.