0% found this document useful (0 votes)
6 views33 pages

Tree

The document provides an overview of tree data structures, explaining their hierarchical nature and various types, including binary trees and binary search trees. It covers essential terminologies, tree structures, and different types of binary trees such as full, complete, and balanced binary trees. Additionally, it discusses traversal methods and operations like insertion, searching, and deletion in binary search 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)
6 views33 pages

Tree

The document provides an overview of tree data structures, explaining their hierarchical nature and various types, including binary trees and binary search trees. It covers essential terminologies, tree structures, and different types of binary trees such as full, complete, and balanced binary trees. Additionally, it discusses traversal methods and operations like insertion, searching, and deletion in binary search 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/ 33

W E E K 0 7 : D ATA B A S E S A N D D ATA S T R U C T U R E S

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

• A collection of nodes that are connected by edges and has


a hierarchical relationship between the nodes.
INTRODUCTION
Why uses Trees?
• Hierarchical Data Representation (e.g., file systems,
organizational charts, DOM).
• Efficient Searching and Sorting (e.g., Binary Search Trees).
• Applications in: 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.

• Internal node: A node with at least one child is called


Internal Node.

rr
• Neighbor of a Node: Parent or child nodes of that node are
called neighbors of that node.

• Subtree: Any node of the tree along with its descendant.

• Height: The number of edges from the root node to the


deepest leaf node

• Size of the tree: number of nodes in the tree.


TYP ES OF
TREES

rr
B IN A RY T R EES

• A hierarchical data structure in which each node has at


most two children ( the left child and the right child)
• commonly used in computer science for efficient storage
and retrieval of data

rr
TYP ES O F B IN A RY T R EE
TYP ES O F B IN A RY T R EE

Full Binary Tree


• A Binary Tree is a full binary tree if
every node has 0 or 2 children
• In other words, a full binary tree is a
binary tree in which all nodes except
leaf nodes have two children
• A special type of binary tree in
which every parent node/internal
node has either two or no children.
• It is also known as a proper binary
tree.
TYP ES O F B IN A RY SEA R C H T R EE

Degenerate Tree

• A Tree where every internal node


has one child.
• Perform as a linked list
• Each non leaf node has a one child,
either as left or right node
TYP ES O F B IN A RY T R EE

Skewed Binary Tree


• A skewed binary tree is a 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 and right-skewed binary tree.
TYP ES O F B IN A RY T R EE

Skewed Binary Tree


• A skewed binary tree is a 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 and right-skewed binary tree.
TYP ES O F B IN A RY T R EE

Complete Binary Tree

• A Binary Tree is a Complete Binary Tree if


all the levels are completely filled except
possibly the last level and the last level
has all keys as left as possible.
• A complete binary tree is just like a full
binary tree, but with two major
differences:
⚬ Every level except the last 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
TYP ES O F B IN A RY T R EE

Perfect Binary Tree

• A Binary tree in which all the internal


nodes have two children and all leaf
nodes are at the same level.
TYP ES O F B IN A RY T R EE

Balanced Binary Tree

• A binary tree in which the


difference in height (the
number of edges on the
longest path from the
root to a leaf) between
the left and right
subtrees of any node is at
most one. This ensures
that the tree remains
roughly balanced, which
helps optimize operations
such as searching,
insertion, and deletion.
B IN A RY T R EES
(T R AV ER SA L)
• Breadth First Search (BFS) is when the nodes on the same level are
visited before going to the next level in the tree. This means that
the tree is explored in a more sideways direction.
• Depth First Search (DFS) is when the traversal moves down the
tree all the way to the leaf nodes, exploring the tree branch by
branch in a downwards direction.
⚬ in-order
⚬ pre-order
⚬ post-order
B IN A RY TR EES
(T R AV ER SA L)
• In-Order Traversal: At first traverse left subtree then visit the root
and then traverse the right subtree.
• Steps:
⚬ Traverse left subtree
⚬ Visit the root and print the data.
⚬ Traverse the right subtree

• 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.

You might also like