0% found this document useful (0 votes)
40 views48 pages

Trees, BT, BST

Uploaded by

nvkeerthuvcet
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)
40 views48 pages

Trees, BT, BST

Uploaded by

nvkeerthuvcet
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/ 48

Trees

Trees
• A tree is a nonlinear data structure, compared to
arrays, linked lists, stacks and queues which are
linear data structures.
• A tree can be empty with no nodes or a tree is
a structure consisting of one node called the root
and zero or one or more subtrees.
• Trees are hierarchical data structure.
Tree Terminology
• Path − Path refers to the sequence of nodes along the edges of a tree.
• Root − The node at the top of the tree is called root. There is only one root per tree
and one path from the root node to any node.
• Parent − Any node except the root node has one edge upward to a node called
parent.
• Child − The node below a given node connected by its edge downward is called its
child node.
• Leaf − The node which does not have any child node is called the leaf node.
• Subtree − Subtree represents the descendants of a node.
• Visiting − Visiting refers to checking the value of a node when control is on the node.
• Traversing − Traversing means passing through nodes in a specific order.
• Levels − Level of a node represents the generation of a node. If the root node is at
level 0, then its next child node is at level 1, its grandchild is at level 2, and so on.
• keys − Key represents a value of a node based on which a search operation is to be
carried out for a node.
• 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.
• 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).
• Height of the leaf node will be always zero.
• Depth of root node will be always zero.
• Depth of a Node: The depth of a node is the number of edges
from the root to the node.
Height of a tree

Height of this tree=2


Height of the tree
Why trees?
1. Trees (with some ordering e.g., BST) provide
moderate access/search (quicker than Linked
List and slower than arrays).
2. Trees provide moderate insertion/deletion
(quicker than Arrays and slower than
Unordered Linked Lists).
3. Like Linked Lists and unlike Arrays, Trees don’t
have an upper limit on number of nodes as
nodes are linked using pointers.
Trees applications
1. Manipulate hierarchical data.
2. Make information easy to search (see tree traversal).
3. Manipulate sorted lists of data.
4. As a workflow for compositing digital images for
visual effects.
5. Router algorithms
6. Form of a multi-stage decision-making (see business
chess).
Binary Tree
• A tree whose elements have at most 2
children is called a binary tree.
• Since each element in a binary tree can have
only 2 children, we typically name them the
left and right child.
Types of binary tree
• 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. It is also known
as a proper binary tree.
• 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
– A complete binary tree is just like a full binary
tree, but with two 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 and right-skewed
binary tree.
Binary Tree representation
• A node of a binary tree is represented by a
structure containing a data part and two
pointers to other structures of the same type.
Representation of binary tree using doubly
linked list
• Binary Tree Applications
– For easy and quick access to data
– In router algorithms
– To implement heap data structure
– Syntax tree
Tree Traversal
• Traversal is a process to visit all the nodes of a
tree and may print their values too.
– Because, all nodes are connected via edges (links)
we always start from the root (head) node.
– That is, we cannot randomly access a node in a tree.
There are three ways which we use to traverse a
tree
• In-order Traversal (symmetric traversal)
• Pre-order Traversal (depth first traversal)
• Post-order Traversal
In-order Traversal
• Step 1 − Recursively traverse left subtree.
• Step 2 − Visit root node.
• Step 3 − Recursively traverse right subtree.
Inorder
Pre-order Traversal
• Step 1 − Visit root node.
• Step 2 − Recursively traverse left subtree.
• Step 3 − Recursively traverse right subtree.
Post-order Traversal
• Step 1 − Recursively traverse left subtree.
• Step 2 − Recursively traverse right subtree.
Step 3 − Visit root node.
• Write the inorder, preorder, postorder of the:
Properties of binary tree
1) The maximum number of nodes at level ‘L’
of a binary tree is 2L
2) Maximum number of nodes in a binary tree
of level ‘L’ is 2L – 1
• Time Complexity:
– The worst case time complexity of search and
insert operations is O(h) where h is height of
Binary Search Tree.
– In worst case, we may have to travel from root to
the deepest leaf node.
– The height of a skewed tree may become n and
the time complexity of search and insert operation
may become O(n).
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
in O(log(n)) time.
• Binary Search Tree is a node-based binary tree
data structure which has the following
properties:
– 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.
Binary Search Tree Operations:
• Searching an element in a BST
• Inserting an element in a BST
• Deleting an element in a BST
Searching an element
1. Start from root.
2. Compare the inserting element with root, if less
than root, then recurse for left, else recurse for
right.
3. If element to search is found anywhere, return true,
else return false.
Searching 6
Searching 4 from the below tree
If the value is not found, we eventually reach the left or right child of a leaf node
which is NULL and it gets propagated and returned.
Inserting an element
1. Start from root.
2. Compare the inserting element with root, if less than root,
then recurse for left, else recurse for right.
3. After reaching end, just insert that node at left(if less than
current) else right. Inserting 4
Routine for Inserting a node in BST
Deletion
• Three cases
1) Node to be deleted is leaf
2) Node to be deleted has only one child
3) Node to be deleted has two children
1) Node to be deleted is leaf: Simply remove
from the tree.
2) Node to be deleted has only one child:
– Replace that node with its child node.
– Remove the child node from its original position.
3) Node to be deleted has two children:
– Get the inorder successor of that node.
– Replace the node with the inorder successor.
– Remove the inorder successor from its original
position.
Routine for Deleting a node in BST
BST Traversals
• As same as Binary tree traversals
• Binary Search Tree Applications
– In multilevel indexing in the database
– For dynamic sorting
– For managing virtual memory areas in Unix kernel
Difference between
Difference between

You might also like