SlideShare a Scribd company logo
1 of 32
UNIT 3 TREES
UNIT 3 :TREES
Submitted By
Anusruti Mitra(MTECH DS)
BINARY TREE
 Fundamental data storage structures used in programming.
 Combines advantages of an ordered array and a linked list.
 Searching as fast as in ordered array.
 Insertion and deletion as fast as in linked list.
Types of binary tree
Binary Search tree
 Binary search trees (BST), sometimes
called ordered or sorted binary trees
 Values in left sub tree less than parent
 Values in right sub tree greater than parent
 Fast searches in a Binary Search tree, maximum of log n
comparisons
Operations in Binary Search Tree
 Insretion: We start at the root and recursively go down the tree
searching for a location in a BST to insert a new node. If the element to
be inserted is already in the tree, we are done (we do not insert
duplicates). The new node will always replace a NULL reference.
 Searching: Searching in a BST always starts at the root. We compare a
data stored at the root with the key we are searching for (let us call it as
toSearch). If the node does not contain the key we proceed either to the
left or right child depending upon comparison. If the result of
comparison is negative we go to the left child, otherwise - to the right
child.
 Deletion: When removing a node from a binary search tree it is
mandatory to maintain the in-order sequence of the nodes. There are
many possibilities to do this
Deleting a node with no children: simply remove the node from
the tree.
Deleting a node with one child: remove the node and replace it
with its child.
Deleting a node with two children: call the node to be deleted D.
Inorder Traversal
 The node’s left subtree is traversed
 The node’s data is processed
 The node’s right subtree is traversed
 9, 5, 1, 7, 2, 12, 8, 4, 3, 11
Preorder traversal
 Visit root node
 Visit all the nodes in the left subtree
 Visit all the nodes in the right subtree
 8, 5, 9, 7, 1, 12, 2, 4, 11, 3
Postorder traversal
 visit all the nodes in the left subtree
 visit the root node
 visit all the nodes in the right subtree
 9, 1, 2, 12, 7, 5, 3, 11, 4, 8
Splay
 Splay is a binary search tree in which recently accessed
keys are quick to access again.
 When an element is accessed, it is moved to the root using
series of rotation operations so that it is quick to access
again.
 The tree is also approximately balanced while rearranging.
The process of rearranging is called splaying.
Zig-zig step
 This step is done when p is not the
root and x and p are either both right
children or are both left children.
 The picture below shows the case
where x and p are both left children.
 The tree is rotated on the edge
joining p with its parent g, then
rotated on the edge joining x with p.
 Zig-zig steps are the only thing that
differentiate splay trees from
the rotate to root method
Zig-Zag operation
 this step is done when p is not
the root and x is a right child
and p is a left child or vice
versa.
 The tree is rotated on the
edge between p and x, and
then rotated on the resulting
edge between x and g.
AVL TREES
Introduction
•An AVL tree is a subtype of binary search tree.
•A BST is a data structure composed of nodes.
It has the following guarantees:
•Each tree has a root node (at the top).
•The root node has zero or more child nodes.
•Each child node has zero or more child nodes,
and so on.
•Each node has up to two children.
•For each node, its left descendents are less than
the current node, which is less than the right
descendents.
Why Avl Tree?
AVL trees have an additional guarantee:
The difference between the depth of right
and left subtrees cannot be more than
one. In order to maintain this guarantee,
an implementation of an AVL will
include an algorithm to rebalance the tree
when adding an additional element
would upset this guarantee.
ALGORITHM
INSERTION
Let the newly inserted node be w
1) Perform standard BST insert for w.
2) Starting from w, travel up and find the first unbalanced node. Let z be
the first unbalanced node, y be the child of z that comes on the path from
w to z and x be the grandchild of z that comes on the path from w to z.
3) Re-balance the tree by performing appropriate rotations on the subtree
rooted with z. There can be 4 possible cases that needs to be handled as
x, y and z can be arranged in 4 ways. Following are the possible 4
arrangements:
a) y is left child of z and x is left child of y (Left Left Case)
b) y is left child of z and x is right child of y (Left Right Case)
c) y is right child of z and x is right child of y (Right Right Case)
d) y is right child of z and x is left child of y (Right Left Case)
Time Complexity
O(logN)
Space Complexity
O(1)
Application
AVL trees are beneficial in the cases where you
are designing some database where insertions
and deletions are not that frequent but you have
to frequently look-up for the items present in
there.
RED BLACK TREE
Introduction
Red-Black Tree is a self-balancing Binary Search Tree
(BST) where every node follows following rules.
RedBlackTree
1) Every node has a color either red or black.
2) Root of tree is always black.
3) There are no two adjacent red nodes (A red node
cannot have a red parent or red child).
4) Every path from a node (including root) to any of its
descendant NULL node has the same number of black
nodes.
Why Red-Black Trees?
Most of the BST operations (e.g., search, max, min,
insert, delete.. etc) take O(h) time where h is the
height of the BST. The cost of these operations may
become O(n) for a skewed Binary tree. If we make
sure that height of the tree remains O(Logn) after
every insertion and deletion, then we can guarantee
an upper bound of O(Logn) for all these operations.
The height of a Red-Black tree is always O(Logn)
where n is the number of nodes in the tree.
ALGORITHM
In Red-Black tree, we use two tools to do balancing.
1) Recoloring
2) Rotation
Color of a NULL node is considered as BLACK.
Let x be the newly inserted node.
1) Perform standard BST insertion and make the color of newly inserted nodes as RED.
2) If x is root, change color of x as BLACK (Black height of complete tree increases by 1).
3) Do following if color of x’s parent is not BLACK and x is not root.
….a) If x’s uncle is RED (Grand parent must have been black from property 4)
……..(i) Change color of parent and uncle as BLACK.
……..(ii) color of grand parent as RED.
……..(iii) Change x = x’s grandparent, repeat steps 2 and 3 for new x.
b) If x’s uncle is BLACK, then there can be four configurations for x, x’s parent (p) and x’s
grandparent (g) (This is similar to AVL Tree)
……..i) Left Left Case (p is left child of g and x is left child of p)
……..ii) Left Right Case (p is left child of g and x is right child of p)
……..iii) Right Right Case (Mirror of case i)
……..iv) Right Left Case (Mirror of case ii)
Following are operations to be performed in four subcases when uncle is BLACK.
Complexity
Thus, the power of a red-black tree is that the
average and worst-case scenario to search, insert,
and delete from the tree is always O(log n) time,
guaranteed. The space complexity of a red-black
tree is no different from a BST, and depends on the
number of total nodes: O(n).
Applications
Most of the self-balancing BST library functions
like map and set in C++ (OR TreeSet and
TreeMap in Java) use Red Black Tree
It is used to implement CPU Scheduling Linux.
Completely Fair Scheduler uses it.
A B-Tree of order m is an m way tree in which:
•All the leaf nodes must be at same level.
•All nodes except root must have at least [m/2]-1 keys and
maximum of m-1 keys.
•All non leaf nodes except root (i.e. all internal nodes)
must have at least m/2 children.
•If the root node is a non leaf node, then it must have at
least 2 children.
•A non leaf node with n-1 keys must have n number of
children.
•All the key values within a node must be in Ascending
Order.
B-TREE
ALGORITHM
Searching operation in B-tree:
Searching is similar to searching a binary search
tree. Starting at the root, the tree is recursively traversed
from top to bottom. At each level, the search reduces its field
of view to the child pointer (subtree) whose range includes
the search value. A subtree's range is defined by the values,
or keys, contained in its parent node. These limiting values
are also known as separation values.
Binary search is typically (but not necessarily) used within
nodes to find the separation values and child tree of interest.
Insertion operation in B-tree:
Insertions are done at the leaf node level. The following
algorithm needs to be followed in order to insert an item into B Tree.
1)Initialize x as root.
2) While x is not leaf, do following
..a) Find the child of x that is going to be traversed next. Let the
child be y.
..b) If y is not full, change x to point to y.
..c) If y is full, split it and change x to point to one of the two parts
of y. If k is smaller than mid key in y, then set x as the first part of y.
Else second part of y. When we split y, we move a key from y to its
parent x.
3) The loop in step 2 stops when x is leaf. x must have space for 1 extra
key as we have been splitting all nodes in advance. So simply insert k to
x.
Deletion operation in B-tree:
Deletion is also performed at the leaf nodes. The node which is to be deleted
can either be a leaf node or an internal node. Following algorithm needs to be followed in order
to delete a node from a B tree.
1.Locate the leaf node.
2.If there are more than m/2 keys in the leaf node then delete the desired key from the node.
3.If the leaf node doesn't contain m/2 keys then complete the keys by taking the element from
eight or left sibling.
-----If the left sibling contains more than m/2 elements then push its largest element up to
its parent and move the intervening element down to the node where the key is deleted.
------f the right sibling contains more than m/2 elements then push its smallest element up
to the parent and move intervening element down to the node where the key is deleted.
4.If neither of the sibling contain more than m/2 elements then create a new leaf node by
joining two leaf nodes and the intervening element of the parent node.
5.If parent is left with less than m/2 nodes then, apply the above process on the parent too.
If the node which is to be deleted is an internal node, then replace the node
with its in-order successor or predecessor. Since, successor or predecessor will always be on
the leaf node hence, the process will be similar as the node is being deleted from the leaf node.
Time complexity
 Worst case search time
complexity: Θ(logn)
 Average case search time
complexity: Θ(logn)
 Best case search time
complexity: Θ(logn)
Space complexity
 Average case Space
complexity: Θ(n)
 Worst case Space
complexity: Θ(n)
Complexity
Application of B-tree
B-Tree has many important applications in computer
science.
One of the main reason of using B tree is its capability to
store large number of keys in a single node and large key
values by keeping the height of the tree relatively small.
B-trees are preferred when decision points, called nodes,
are on hard disk rather than in random-access memory
(RAM)
B-trees save time by using nodes with many branches
(called children), compared with binary trees, in which each
node has only two children, thereby speeding up the process.
Definition of 2-3 trees
A 2-3 Tree is a specific form of a B tree. A 2-3 tree is a
search tree. However, it is very different from a binary search
tree.
Here are the properties of a 2-3 tree:
1. each node has either one value or two value
2. a node with one value is either a leaf node or has exactly
two children (non-null). Values in left subtree < value in
node < values in right subtree
3. a node with two values is either a leaf node or has
exactly three children (non-null). Values in left subtree <
first value in node < values in middle subtree < second
value in node < value in right subtree.
4. all leaf nodes are at the same level of the tree
ALGORITHM
Search operation in 2-3 trees
To search a key K in given 2-3 tree T, we follow the following procedure:
Base cases:
1. If T is empty, return False (key cannot be found in the tree).
2. If current node contains data value which is equal to K, return True.
3. If we reach the leaf-node and it doesn’t contain the required key
value K, return False.
Recursive Calls:
1. If K < currentNode.leftVal, we explore the left subtree of the current
node.
2. Else if currentNode.leftVal < K < currentNode.rightVal, we explore
the middle subtree of the current node.
3. Else if K > currentNode.rightVal, we explore the right subtree of the
current node.
Insertion operation in 2-3 trees
The insertion algorithm into a two-three tree is quite
different from the insertion algorithm into a binary search
tree. In a two-three tree, the algorithm will be as follows:
1. If the tree is empty, create a node and put value into
the node
2. Otherwise find the leaf node where the value belongs.
3. If the leaf node has only one value, put the new value
into the node
4. If the leaf node has more than two values, split the
node and promote the median of the three values to
parent.
5. If the parent then has three values, continue to split
and promote, forming a new root node if necessary
Complexity of 2-3 tree
Log n
Application
1. In 2-3 trees you have faster inserts at the expense of
slower searches(since height is more compared to
AVL trees).
2. A persistent data structure based on 2-3 tree is better
than the one based on AVL trees
Trees in data structure

More Related Content

What's hot (20)

Hash table
Hash tableHash table
Hash table
 
Introduction to stack
Introduction to stackIntroduction to stack
Introduction to stack
 
Trees, Binary Search Tree, AVL Tree in Data Structures
Trees, Binary Search Tree, AVL Tree in Data Structures Trees, Binary Search Tree, AVL Tree in Data Structures
Trees, Binary Search Tree, AVL Tree in Data Structures
 
Binary Search Tree
Binary Search TreeBinary Search Tree
Binary Search Tree
 
Selection sort
Selection sortSelection sort
Selection sort
 
DATA STRUCTURES
DATA STRUCTURESDATA STRUCTURES
DATA STRUCTURES
 
AVL Tree in Data Structure
AVL Tree in Data Structure AVL Tree in Data Structure
AVL Tree in Data Structure
 
Array data structure
Array data structureArray data structure
Array data structure
 
Data structure Stack
Data structure StackData structure Stack
Data structure Stack
 
stack presentation
stack presentationstack presentation
stack presentation
 
1.5 binary search tree
1.5 binary search tree1.5 binary search tree
1.5 binary search tree
 
Skip list vinay khimsuriya_200430723005
Skip list vinay khimsuriya_200430723005Skip list vinay khimsuriya_200430723005
Skip list vinay khimsuriya_200430723005
 
Circular queue
Circular queueCircular queue
Circular queue
 
Binary tree
Binary  treeBinary  tree
Binary tree
 
Data Structures- Part5 recursion
Data Structures- Part5 recursionData Structures- Part5 recursion
Data Structures- Part5 recursion
 
Hash tables
Hash tablesHash tables
Hash tables
 
Insertion sort
Insertion sort Insertion sort
Insertion sort
 
Sorting Algorithms
Sorting AlgorithmsSorting Algorithms
Sorting Algorithms
 
Linked list
Linked list Linked list
Linked list
 
Stack project
Stack projectStack project
Stack project
 

Similar to Trees in data structure

Similar to Trees in data structure (20)

Balance tree. Short overview
Balance tree. Short overviewBalance tree. Short overview
Balance tree. Short overview
 
trees in data structure
trees in data structure trees in data structure
trees in data structure
 
VCE Unit 05.pptx
VCE Unit 05.pptxVCE Unit 05.pptx
VCE Unit 05.pptx
 
BINARY SEARCH TREE
BINARY SEARCH TREEBINARY SEARCH TREE
BINARY SEARCH TREE
 
Biary search Tree.docx
Biary search Tree.docxBiary search Tree.docx
Biary search Tree.docx
 
Tree Data Structure by Daniyal Khan
Tree Data Structure by Daniyal KhanTree Data Structure by Daniyal Khan
Tree Data Structure by Daniyal Khan
 
Binary Tree - Algorithms
Binary Tree - Algorithms Binary Tree - Algorithms
Binary Tree - Algorithms
 
Binary tree
Binary treeBinary tree
Binary tree
 
Binary Search Tree
Binary Search TreeBinary Search Tree
Binary Search Tree
 
TREES.pptx
TREES.pptxTREES.pptx
TREES.pptx
 
Binary Search Tree
Binary Search TreeBinary Search Tree
Binary Search Tree
 
Binary search tree
Binary search treeBinary search tree
Binary search tree
 
Binary Search Tree
Binary Search TreeBinary Search Tree
Binary Search Tree
 
Trees
TreesTrees
Trees
 
Red black trees
Red black treesRed black trees
Red black trees
 
Unit iv data structure-converted
Unit  iv data structure-convertedUnit  iv data structure-converted
Unit iv data structure-converted
 
15-btrees.ppt
15-btrees.ppt15-btrees.ppt
15-btrees.ppt
 
nptel 2nd presentation.pptx
nptel 2nd presentation.pptxnptel 2nd presentation.pptx
nptel 2nd presentation.pptx
 
unit06-3-Trees.pdf
unit06-3-Trees.pdfunit06-3-Trees.pdf
unit06-3-Trees.pdf
 
Binary trees1
Binary trees1Binary trees1
Binary trees1
 

Recently uploaded

Pigging Solutions Piggable Sweeping Elbows
Pigging Solutions Piggable Sweeping ElbowsPigging Solutions Piggable Sweeping Elbows
Pigging Solutions Piggable Sweeping ElbowsPigging Solutions
 
AI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsAI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsMemoori
 
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
"Federated learning: out of reach no matter how close",Oleksandr LapshynFwdays
 
Build your next Gen AI Breakthrough - April 2024
Build your next Gen AI Breakthrough - April 2024Build your next Gen AI Breakthrough - April 2024
Build your next Gen AI Breakthrough - April 2024Neo4j
 
SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024Lorenzo Miniero
 
"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii SoldatenkoFwdays
 
Bluetooth Controlled Car with Arduino.pdf
Bluetooth Controlled Car with Arduino.pdfBluetooth Controlled Car with Arduino.pdf
Bluetooth Controlled Car with Arduino.pdfngoud9212
 
SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024Scott Keck-Warren
 
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Mark Simos
 
Gen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfGen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfAddepto
 
Dev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebDev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebUiPathCommunity
 
Install Stable Diffusion in windows machine
Install Stable Diffusion in windows machineInstall Stable Diffusion in windows machine
Install Stable Diffusion in windows machinePadma Pradeep
 
Transcript: New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024Transcript: New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024BookNet Canada
 
Science&tech:THE INFORMATION AGE STS.pdf
Science&tech:THE INFORMATION AGE STS.pdfScience&tech:THE INFORMATION AGE STS.pdf
Science&tech:THE INFORMATION AGE STS.pdfjimielynbastida
 
Unblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesUnblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesSinan KOZAK
 
Benefits Of Flutter Compared To Other Frameworks
Benefits Of Flutter Compared To Other FrameworksBenefits Of Flutter Compared To Other Frameworks
Benefits Of Flutter Compared To Other FrameworksSoftradix Technologies
 
New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024BookNet Canada
 
Pigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food ManufacturingPigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food ManufacturingPigging Solutions
 
Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Scott Keck-Warren
 

Recently uploaded (20)

Pigging Solutions Piggable Sweeping Elbows
Pigging Solutions Piggable Sweeping ElbowsPigging Solutions Piggable Sweeping Elbows
Pigging Solutions Piggable Sweeping Elbows
 
AI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsAI as an Interface for Commercial Buildings
AI as an Interface for Commercial Buildings
 
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
 
Build your next Gen AI Breakthrough - April 2024
Build your next Gen AI Breakthrough - April 2024Build your next Gen AI Breakthrough - April 2024
Build your next Gen AI Breakthrough - April 2024
 
SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024
 
"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko
 
Bluetooth Controlled Car with Arduino.pdf
Bluetooth Controlled Car with Arduino.pdfBluetooth Controlled Car with Arduino.pdf
Bluetooth Controlled Car with Arduino.pdf
 
SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024
 
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
 
Gen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfGen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdf
 
Dev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebDev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio Web
 
Install Stable Diffusion in windows machine
Install Stable Diffusion in windows machineInstall Stable Diffusion in windows machine
Install Stable Diffusion in windows machine
 
Transcript: New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024Transcript: New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
 
Vulnerability_Management_GRC_by Sohang Sengupta.pptx
Vulnerability_Management_GRC_by Sohang Sengupta.pptxVulnerability_Management_GRC_by Sohang Sengupta.pptx
Vulnerability_Management_GRC_by Sohang Sengupta.pptx
 
Science&tech:THE INFORMATION AGE STS.pdf
Science&tech:THE INFORMATION AGE STS.pdfScience&tech:THE INFORMATION AGE STS.pdf
Science&tech:THE INFORMATION AGE STS.pdf
 
Unblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesUnblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen Frames
 
Benefits Of Flutter Compared To Other Frameworks
Benefits Of Flutter Compared To Other FrameworksBenefits Of Flutter Compared To Other Frameworks
Benefits Of Flutter Compared To Other Frameworks
 
New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
 
Pigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food ManufacturingPigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food Manufacturing
 
Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024
 

Trees in data structure

  • 1. UNIT 3 TREES UNIT 3 :TREES Submitted By Anusruti Mitra(MTECH DS)
  • 2. BINARY TREE  Fundamental data storage structures used in programming.  Combines advantages of an ordered array and a linked list.  Searching as fast as in ordered array.  Insertion and deletion as fast as in linked list.
  • 4. Binary Search tree  Binary search trees (BST), sometimes called ordered or sorted binary trees  Values in left sub tree less than parent  Values in right sub tree greater than parent  Fast searches in a Binary Search tree, maximum of log n comparisons
  • 5. Operations in Binary Search Tree  Insretion: We start at the root and recursively go down the tree searching for a location in a BST to insert a new node. If the element to be inserted is already in the tree, we are done (we do not insert duplicates). The new node will always replace a NULL reference.  Searching: Searching in a BST always starts at the root. We compare a data stored at the root with the key we are searching for (let us call it as toSearch). If the node does not contain the key we proceed either to the left or right child depending upon comparison. If the result of comparison is negative we go to the left child, otherwise - to the right child.  Deletion: When removing a node from a binary search tree it is mandatory to maintain the in-order sequence of the nodes. There are many possibilities to do this Deleting a node with no children: simply remove the node from the tree. Deleting a node with one child: remove the node and replace it with its child. Deleting a node with two children: call the node to be deleted D.
  • 6. Inorder Traversal  The node’s left subtree is traversed  The node’s data is processed  The node’s right subtree is traversed  9, 5, 1, 7, 2, 12, 8, 4, 3, 11
  • 7. Preorder traversal  Visit root node  Visit all the nodes in the left subtree  Visit all the nodes in the right subtree  8, 5, 9, 7, 1, 12, 2, 4, 11, 3
  • 8. Postorder traversal  visit all the nodes in the left subtree  visit the root node  visit all the nodes in the right subtree  9, 1, 2, 12, 7, 5, 3, 11, 4, 8
  • 9. Splay  Splay is a binary search tree in which recently accessed keys are quick to access again.  When an element is accessed, it is moved to the root using series of rotation operations so that it is quick to access again.  The tree is also approximately balanced while rearranging. The process of rearranging is called splaying.
  • 10. Zig-zig step  This step is done when p is not the root and x and p are either both right children or are both left children.  The picture below shows the case where x and p are both left children.  The tree is rotated on the edge joining p with its parent g, then rotated on the edge joining x with p.  Zig-zig steps are the only thing that differentiate splay trees from the rotate to root method
  • 11. Zig-Zag operation  this step is done when p is not the root and x is a right child and p is a left child or vice versa.  The tree is rotated on the edge between p and x, and then rotated on the resulting edge between x and g.
  • 12. AVL TREES Introduction •An AVL tree is a subtype of binary search tree. •A BST is a data structure composed of nodes. It has the following guarantees: •Each tree has a root node (at the top). •The root node has zero or more child nodes. •Each child node has zero or more child nodes, and so on. •Each node has up to two children. •For each node, its left descendents are less than the current node, which is less than the right descendents.
  • 13. Why Avl Tree? AVL trees have an additional guarantee: The difference between the depth of right and left subtrees cannot be more than one. In order to maintain this guarantee, an implementation of an AVL will include an algorithm to rebalance the tree when adding an additional element would upset this guarantee.
  • 14. ALGORITHM INSERTION Let the newly inserted node be w 1) Perform standard BST insert for w. 2) Starting from w, travel up and find the first unbalanced node. Let z be the first unbalanced node, y be the child of z that comes on the path from w to z and x be the grandchild of z that comes on the path from w to z. 3) Re-balance the tree by performing appropriate rotations on the subtree rooted with z. There can be 4 possible cases that needs to be handled as x, y and z can be arranged in 4 ways. Following are the possible 4 arrangements: a) y is left child of z and x is left child of y (Left Left Case) b) y is left child of z and x is right child of y (Left Right Case) c) y is right child of z and x is right child of y (Right Right Case) d) y is right child of z and x is left child of y (Right Left Case)
  • 16. Application AVL trees are beneficial in the cases where you are designing some database where insertions and deletions are not that frequent but you have to frequently look-up for the items present in there.
  • 17. RED BLACK TREE Introduction Red-Black Tree is a self-balancing Binary Search Tree (BST) where every node follows following rules. RedBlackTree 1) Every node has a color either red or black. 2) Root of tree is always black. 3) There are no two adjacent red nodes (A red node cannot have a red parent or red child). 4) Every path from a node (including root) to any of its descendant NULL node has the same number of black nodes.
  • 18. Why Red-Black Trees? Most of the BST operations (e.g., search, max, min, insert, delete.. etc) take O(h) time where h is the height of the BST. The cost of these operations may become O(n) for a skewed Binary tree. If we make sure that height of the tree remains O(Logn) after every insertion and deletion, then we can guarantee an upper bound of O(Logn) for all these operations. The height of a Red-Black tree is always O(Logn) where n is the number of nodes in the tree.
  • 19. ALGORITHM In Red-Black tree, we use two tools to do balancing. 1) Recoloring 2) Rotation Color of a NULL node is considered as BLACK. Let x be the newly inserted node. 1) Perform standard BST insertion and make the color of newly inserted nodes as RED. 2) If x is root, change color of x as BLACK (Black height of complete tree increases by 1). 3) Do following if color of x’s parent is not BLACK and x is not root. ….a) If x’s uncle is RED (Grand parent must have been black from property 4) ……..(i) Change color of parent and uncle as BLACK. ……..(ii) color of grand parent as RED. ……..(iii) Change x = x’s grandparent, repeat steps 2 and 3 for new x. b) If x’s uncle is BLACK, then there can be four configurations for x, x’s parent (p) and x’s grandparent (g) (This is similar to AVL Tree) ……..i) Left Left Case (p is left child of g and x is left child of p) ……..ii) Left Right Case (p is left child of g and x is right child of p) ……..iii) Right Right Case (Mirror of case i) ……..iv) Right Left Case (Mirror of case ii) Following are operations to be performed in four subcases when uncle is BLACK.
  • 20. Complexity Thus, the power of a red-black tree is that the average and worst-case scenario to search, insert, and delete from the tree is always O(log n) time, guaranteed. The space complexity of a red-black tree is no different from a BST, and depends on the number of total nodes: O(n).
  • 21. Applications Most of the self-balancing BST library functions like map and set in C++ (OR TreeSet and TreeMap in Java) use Red Black Tree It is used to implement CPU Scheduling Linux. Completely Fair Scheduler uses it.
  • 22. A B-Tree of order m is an m way tree in which: •All the leaf nodes must be at same level. •All nodes except root must have at least [m/2]-1 keys and maximum of m-1 keys. •All non leaf nodes except root (i.e. all internal nodes) must have at least m/2 children. •If the root node is a non leaf node, then it must have at least 2 children. •A non leaf node with n-1 keys must have n number of children. •All the key values within a node must be in Ascending Order. B-TREE
  • 23. ALGORITHM Searching operation in B-tree: Searching is similar to searching a binary search tree. Starting at the root, the tree is recursively traversed from top to bottom. At each level, the search reduces its field of view to the child pointer (subtree) whose range includes the search value. A subtree's range is defined by the values, or keys, contained in its parent node. These limiting values are also known as separation values. Binary search is typically (but not necessarily) used within nodes to find the separation values and child tree of interest.
  • 24. Insertion operation in B-tree: Insertions are done at the leaf node level. The following algorithm needs to be followed in order to insert an item into B Tree. 1)Initialize x as root. 2) While x is not leaf, do following ..a) Find the child of x that is going to be traversed next. Let the child be y. ..b) If y is not full, change x to point to y. ..c) If y is full, split it and change x to point to one of the two parts of y. If k is smaller than mid key in y, then set x as the first part of y. Else second part of y. When we split y, we move a key from y to its parent x. 3) The loop in step 2 stops when x is leaf. x must have space for 1 extra key as we have been splitting all nodes in advance. So simply insert k to x.
  • 25. Deletion operation in B-tree: Deletion is also performed at the leaf nodes. The node which is to be deleted can either be a leaf node or an internal node. Following algorithm needs to be followed in order to delete a node from a B tree. 1.Locate the leaf node. 2.If there are more than m/2 keys in the leaf node then delete the desired key from the node. 3.If the leaf node doesn't contain m/2 keys then complete the keys by taking the element from eight or left sibling. -----If the left sibling contains more than m/2 elements then push its largest element up to its parent and move the intervening element down to the node where the key is deleted. ------f the right sibling contains more than m/2 elements then push its smallest element up to the parent and move intervening element down to the node where the key is deleted. 4.If neither of the sibling contain more than m/2 elements then create a new leaf node by joining two leaf nodes and the intervening element of the parent node. 5.If parent is left with less than m/2 nodes then, apply the above process on the parent too. If the node which is to be deleted is an internal node, then replace the node with its in-order successor or predecessor. Since, successor or predecessor will always be on the leaf node hence, the process will be similar as the node is being deleted from the leaf node.
  • 26. Time complexity  Worst case search time complexity: Θ(logn)  Average case search time complexity: Θ(logn)  Best case search time complexity: Θ(logn) Space complexity  Average case Space complexity: Θ(n)  Worst case Space complexity: Θ(n) Complexity
  • 27. Application of B-tree B-Tree has many important applications in computer science. One of the main reason of using B tree is its capability to store large number of keys in a single node and large key values by keeping the height of the tree relatively small. B-trees are preferred when decision points, called nodes, are on hard disk rather than in random-access memory (RAM) B-trees save time by using nodes with many branches (called children), compared with binary trees, in which each node has only two children, thereby speeding up the process.
  • 28. Definition of 2-3 trees A 2-3 Tree is a specific form of a B tree. A 2-3 tree is a search tree. However, it is very different from a binary search tree. Here are the properties of a 2-3 tree: 1. each node has either one value or two value 2. a node with one value is either a leaf node or has exactly two children (non-null). Values in left subtree < value in node < values in right subtree 3. a node with two values is either a leaf node or has exactly three children (non-null). Values in left subtree < first value in node < values in middle subtree < second value in node < value in right subtree. 4. all leaf nodes are at the same level of the tree
  • 29. ALGORITHM Search operation in 2-3 trees To search a key K in given 2-3 tree T, we follow the following procedure: Base cases: 1. If T is empty, return False (key cannot be found in the tree). 2. If current node contains data value which is equal to K, return True. 3. If we reach the leaf-node and it doesn’t contain the required key value K, return False. Recursive Calls: 1. If K < currentNode.leftVal, we explore the left subtree of the current node. 2. Else if currentNode.leftVal < K < currentNode.rightVal, we explore the middle subtree of the current node. 3. Else if K > currentNode.rightVal, we explore the right subtree of the current node.
  • 30. Insertion operation in 2-3 trees The insertion algorithm into a two-three tree is quite different from the insertion algorithm into a binary search tree. In a two-three tree, the algorithm will be as follows: 1. If the tree is empty, create a node and put value into the node 2. Otherwise find the leaf node where the value belongs. 3. If the leaf node has only one value, put the new value into the node 4. If the leaf node has more than two values, split the node and promote the median of the three values to parent. 5. If the parent then has three values, continue to split and promote, forming a new root node if necessary
  • 31. Complexity of 2-3 tree Log n Application 1. In 2-3 trees you have faster inserts at the expense of slower searches(since height is more compared to AVL trees). 2. A persistent data structure based on 2-3 tree is better than the one based on AVL trees