The document discusses algorithms and data structures, focusing on binary search trees (BSTs). It provides the following key points:
- BSTs are an important data structure for dynamic sets that can perform operations like search, insert, and delete in O(h) time where h is the height of the tree.
- Each node in a BST contains a key, and pointers to its left/right children and parent. The keys must satisfy the BST property - all keys in the left subtree are less than the node's key, and all keys in the right subtree are greater.
- Rotations are a basic operation used to restructure trees during insertions/deletions. They involve reassigning child
The document discusses red-black trees, which are binary search trees augmented with node colors to guarantee a height of O(log n). It first defines the properties of red-black trees, then proves their height is O(log n), and finally describes insertion and deletion operations. The key points are that nodes can be red or black, insertion can violate properties so recoloring is needed, and rotations are used to restructure the tree during insertion and deletion while maintaining the red-black properties.
Content of slide
Tree
Binary tree Implementation
Binary Search Tree
BST Operations
Traversal
Insertion
Deletion
Types of BST
Complexity in BST
Applications of BST
An AVL tree is a self-balancing binary search tree that guarantees search, insertion, and deletion operations will take O(log n) time on average. It achieves this by ensuring the heights of the left and right subtrees of every node differ by at most one. When an insertion or deletion causes a height imbalance of two, rotations are performed to rebalance the tree.
The document discusses binary trees and their representations and operations. It defines binary trees as trees where each node has at most two child nodes. It also defines complete binary trees as trees where every node has two children except leaf nodes. The document discusses array and linked representations of binary trees and various traversal operations like preorder, inorder and postorder traversals. It also provides code snippets for inserting and deleting nodes from a binary tree.
Each node in a doubly linked list contains two pointers - one pointing to the next node and one pointing to the previous node. This allows traversal in both directions through the list. A doubly linked list supports common operations like insertion and deletion at both ends of the list as well as anywhere in the list by updating the relevant pointer fields of the nodes. While requiring more space per node, doubly linked lists allow easy traversal backwards through the list and reversing of the list.
An array is a data structure that stores fixed number of items of the same type. It allows fast access of elements using indices. Basic array operations include traversing elements, inserting/deleting elements, searching for elements, and updating elements. Arrays are zero-indexed and elements are accessed via their index.
This document provides an overview of trees as a non-linear data structure. It begins by discussing how trees are used to represent hierarchical relationships and defines some key tree terminology like root, parent, child, leaf, and subtree. It then explains that a tree consists of nodes connected in a parent-child relationship, with one root node and nodes that may have any number of children. The document also covers tree traversal methods like preorder, inorder, and postorder traversal. It introduces binary trees and binary search trees, and discusses operations on BSTs like search, insert, and delete. Finally, it provides a brief overview of the Huffman algorithm for data compression.
The document discusses red-black trees, which are binary search trees augmented with node colors to guarantee a height of O(log n). It first defines the properties of red-black trees, then proves their height is O(log n), and finally describes insertion and deletion operations. The key points are that nodes can be red or black, insertion can violate properties so recoloring is needed, and rotations are used to restructure the tree during insertion and deletion while maintaining the red-black properties.
Content of slide
Tree
Binary tree Implementation
Binary Search Tree
BST Operations
Traversal
Insertion
Deletion
Types of BST
Complexity in BST
Applications of BST
An AVL tree is a self-balancing binary search tree that guarantees search, insertion, and deletion operations will take O(log n) time on average. It achieves this by ensuring the heights of the left and right subtrees of every node differ by at most one. When an insertion or deletion causes a height imbalance of two, rotations are performed to rebalance the tree.
The document discusses binary trees and their representations and operations. It defines binary trees as trees where each node has at most two child nodes. It also defines complete binary trees as trees where every node has two children except leaf nodes. The document discusses array and linked representations of binary trees and various traversal operations like preorder, inorder and postorder traversals. It also provides code snippets for inserting and deleting nodes from a binary tree.
Each node in a doubly linked list contains two pointers - one pointing to the next node and one pointing to the previous node. This allows traversal in both directions through the list. A doubly linked list supports common operations like insertion and deletion at both ends of the list as well as anywhere in the list by updating the relevant pointer fields of the nodes. While requiring more space per node, doubly linked lists allow easy traversal backwards through the list and reversing of the list.
An array is a data structure that stores fixed number of items of the same type. It allows fast access of elements using indices. Basic array operations include traversing elements, inserting/deleting elements, searching for elements, and updating elements. Arrays are zero-indexed and elements are accessed via their index.
This document provides an overview of trees as a non-linear data structure. It begins by discussing how trees are used to represent hierarchical relationships and defines some key tree terminology like root, parent, child, leaf, and subtree. It then explains that a tree consists of nodes connected in a parent-child relationship, with one root node and nodes that may have any number of children. The document also covers tree traversal methods like preorder, inorder, and postorder traversal. It introduces binary trees and binary search trees, and discusses operations on BSTs like search, insert, and delete. Finally, it provides a brief overview of the Huffman algorithm for data compression.
The document discusses recursion, including:
1) Recursion involves breaking a problem down into smaller subproblems until a base case is reached, then building up the solution to the overall problem from the solutions to the subproblems.
2) A recursive function is one that calls itself, with each call typically moving closer to a base case where the problem can be solved without recursion.
3) Recursion can be linear, involving one recursive call, or binary, involving two recursive calls to solve similar subproblems.
The document discusses Red Black Trees, which are self-balancing binary search trees. It defines their key properties, including that every node is either red or black, the root is black, and red nodes can only have black children. It explains how operations like insertion and deletion are performed in O(log n) time by fixing violations of these properties through rotations and recoloring of nodes.
Tree is a non-linear data structure that can represent hierarchical relationships. It consists of nodes connected by edges. The root node has child nodes, and nodes may be leaf nodes or have child nodes themselves. Binary trees restrict nodes to having at most two children. General trees can be converted to binary trees by making the first child the left child and next siblings the right child. Binary trees can be built from input data by comparing values to determine left or right placement, or from traversal operations.
Breadth First Search & Depth First SearchKevin Jadiya
The slides attached here describes how Breadth first search and Depth First Search technique is used in Traversing a graph/tree with Algorithm and simple code snippet.
Binary search trees are binary trees where all left descendants of a node are less than the node's value and all right descendants are greater. This structure allows for efficient search, insertion, and deletion operations. The document provides definitions and examples of binary search tree properties and operations like creation, traversal, searching, insertion, deletion, and finding minimum and maximum values. Applications include dynamically maintaining a sorted dataset to enable efficient search, insertion, and deletion.
This document discusses double-ended queues or deques. Deques allow elements to be added or removed from either end. There are two types: input restricted deques where elements can only be inserted at one end but removed from both ends, and output restricted deques where elements can only be removed from one end but inserted from both ends. Deques can function as stacks or queues depending on the insertion and removal ends. The document describes algorithms for common deque operations like insert_front, insert_back, remove_front, and remove_back. It also lists applications of deques like palindrome checking and task scheduling.
linear search and binary search, Class lecture of Data Structure and Algorithms and Python.
Stack, Queue, Tree, Python, Python Code, Computer Science, Data, Data Analysis, Machine Learning, Artificial Intellegence, Deep Learning, Programming, Information Technology, Psuedocide, Tree, pseudocode, Binary Tree, Binary Search Tree, implementation, Binary search, linear search, Binary search operation, real-life example of binary search, linear search operation, real-life example of linear search, example bubble sort, sorting, insertion sort example, stack implementation, queue implementation, binary tree implementation, priority queue, binary heap, binary heap implementation, object-oriented programming, def, in BST, Binary search tree, Red-Black tree, Splay Tree, Problem-solving using Binary tree, problem-solving using BST, inorder, preorder, postorder
This document presents selection sort, an in-place comparison sorting algorithm. It works by dividing the list into a sorted part on the left and unsorted part on the right. It iterates through the list, finding the smallest element in the unsorted section and swapping it into place. This process continues until the list is fully sorted. Selection sort has a time complexity of O(n^2) in all cases. While it requires no extra storage, it is inefficient for large lists compared to other algorithms.
Binary trees are a non-linear data structure where each node has at most two children, used to represent hierarchical relationships, with nodes connected through parent-child links and traversed through preorder, inorder, and postorder methods; they can be represented through arrays or linked lists and support common operations like search, insert, and delete through comparing node values and restructuring child pointers.
This document discusses the implementation of a single linked list data structure. It describes the nodes that make up a linked list, which have an info field to store data and a next field pointing to the next node. The document outlines different ways to represent linked lists, including static arrays and dynamic pointers. It also provides algorithms for common linked list operations like traversing, inserting, and deleting nodes from the beginning, end, or a specified position within the list.
The document discusses various tree data structures and algorithms related to binary trees. It begins with an introduction to different types of binary trees such as strict binary trees, complete binary trees, and extended binary trees. It then covers tree traversal algorithms including preorder, inorder and postorder traversal. The document also discusses representations of binary trees using arrays and linked lists. Finally, it explains algorithms for operations on binary search trees such as searching, insertion, deletion and rebalancing through rotations in AVL trees.
Linked lists are linear data structures where each node contains a data field and a pointer to the next node. There are two types: singly linked lists where each node has a single next pointer, and doubly linked lists where each node has next and previous pointers. Common operations on linked lists include insertion and deletion which have O(1) time complexity for singly linked lists but require changing multiple pointers for doubly linked lists. Linked lists are useful when the number of elements is dynamic as they allow efficient insertions and deletions without shifting elements unlike arrays.
This PPT is all about the Tree basic on fundamentals of B and B+ Tree with it's Various (Search,Insert and Delete) Operations performed on it and their Examples...
The document discusses different searching algorithms. It describes sequential search which compares the search key to each element in the list sequentially until a match is found. The best case is 1 comparison, average is N/2 comparisons, and worst case is N comparisons. It also describes binary search which divides the sorted list in half at each step, requiring log(N) comparisons in the average and worst cases. The document also covers indexing which structures data for efficient retrieval based on key values and includes clustered vs unclustered indexes.
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.
The document discusses divide and conquer algorithms. It describes divide and conquer as a design strategy that involves dividing a problem into smaller subproblems, solving the subproblems recursively, and combining the solutions. It provides examples of divide and conquer algorithms like merge sort, quicksort, and binary search. Merge sort works by recursively sorting halves of an array until it is fully sorted. Quicksort selects a pivot element and partitions the array into subarrays of smaller and larger elements, recursively sorting the subarrays. Binary search recursively searches half-intervals of a sorted array to find a target value.
1) Trees are hierarchical data structures that store data in nodes connected by edges. They are useful for representing hierarchical relationships.
2) Binary search trees allow quick search, insertion, and deletion of nodes due to the organizational property that the value of each node is greater than all nodes in its left subtree and less than all nodes in its right subtree.
3) Common tree traversal algorithms include preorder, inorder, and postorder traversals, which visit nodes in different orders depending on whether the left/right children or root is visited first.
This document provides an overview of linear search and binary search algorithms.
It explains that linear search sequentially searches through an array one element at a time to find a target value. It is simple to implement but has poor efficiency as the time scales linearly with the size of the input.
Binary search is more efficient by cutting the search space in half at each step. It works on a sorted array by comparing the target to the middle element and determining which half to search next. The time complexity of binary search is logarithmic rather than linear.
This document discusses data structures and linked lists. It provides definitions and examples of different types of linked lists, including:
- Single linked lists, which contain nodes with a data field and a link to the next node.
- Circular linked lists, where the last node links back to the first node, forming a loop.
- Doubly linked lists, where each node contains links to both the previous and next nodes.
- Operations on linked lists such as insertion, deletion, traversal, and searching are also described.
The document discusses asymptotic notations that are used to describe the time complexity of algorithms. It introduces big O notation, which describes asymptotic upper bounds, big Omega notation for lower bounds, and big Theta notation for tight bounds. Common time complexities are described such as O(1) for constant time, O(log N) for logarithmic time, and O(N^2) for quadratic time. The notations allow analyzing how efficiently algorithms use resources like time and space as the input size increases.
The document discusses red-black trees, which are binary search trees augmented with node colors to guarantee a height of O(log n). It reviews the properties of red-black trees, including that every node is red or black, leaves are black, red nodes have black children, and all paths from root to leaf contain the same number of black nodes. It then proves that these properties ensure a height of O(log n) and thus search, minimum, maximum, successor and predecessor operations take O(log n) time. Insert and delete operations also take O(log n) time but require special handling to maintain the red-black tree properties during modification.
The document discusses red-black trees, which are self-balancing binary search trees. It describes the basic operations on dynamic sets like search, insert, and delete. It then explains the properties and structure of red-black trees, including that they ensure height is O(log n) through coloring nodes red or black. The insert and fixup algorithms are shown to maintain these properties and run in O(log n) time, providing efficient search tree operations.
The document discusses recursion, including:
1) Recursion involves breaking a problem down into smaller subproblems until a base case is reached, then building up the solution to the overall problem from the solutions to the subproblems.
2) A recursive function is one that calls itself, with each call typically moving closer to a base case where the problem can be solved without recursion.
3) Recursion can be linear, involving one recursive call, or binary, involving two recursive calls to solve similar subproblems.
The document discusses Red Black Trees, which are self-balancing binary search trees. It defines their key properties, including that every node is either red or black, the root is black, and red nodes can only have black children. It explains how operations like insertion and deletion are performed in O(log n) time by fixing violations of these properties through rotations and recoloring of nodes.
Tree is a non-linear data structure that can represent hierarchical relationships. It consists of nodes connected by edges. The root node has child nodes, and nodes may be leaf nodes or have child nodes themselves. Binary trees restrict nodes to having at most two children. General trees can be converted to binary trees by making the first child the left child and next siblings the right child. Binary trees can be built from input data by comparing values to determine left or right placement, or from traversal operations.
Breadth First Search & Depth First SearchKevin Jadiya
The slides attached here describes how Breadth first search and Depth First Search technique is used in Traversing a graph/tree with Algorithm and simple code snippet.
Binary search trees are binary trees where all left descendants of a node are less than the node's value and all right descendants are greater. This structure allows for efficient search, insertion, and deletion operations. The document provides definitions and examples of binary search tree properties and operations like creation, traversal, searching, insertion, deletion, and finding minimum and maximum values. Applications include dynamically maintaining a sorted dataset to enable efficient search, insertion, and deletion.
This document discusses double-ended queues or deques. Deques allow elements to be added or removed from either end. There are two types: input restricted deques where elements can only be inserted at one end but removed from both ends, and output restricted deques where elements can only be removed from one end but inserted from both ends. Deques can function as stacks or queues depending on the insertion and removal ends. The document describes algorithms for common deque operations like insert_front, insert_back, remove_front, and remove_back. It also lists applications of deques like palindrome checking and task scheduling.
linear search and binary search, Class lecture of Data Structure and Algorithms and Python.
Stack, Queue, Tree, Python, Python Code, Computer Science, Data, Data Analysis, Machine Learning, Artificial Intellegence, Deep Learning, Programming, Information Technology, Psuedocide, Tree, pseudocode, Binary Tree, Binary Search Tree, implementation, Binary search, linear search, Binary search operation, real-life example of binary search, linear search operation, real-life example of linear search, example bubble sort, sorting, insertion sort example, stack implementation, queue implementation, binary tree implementation, priority queue, binary heap, binary heap implementation, object-oriented programming, def, in BST, Binary search tree, Red-Black tree, Splay Tree, Problem-solving using Binary tree, problem-solving using BST, inorder, preorder, postorder
This document presents selection sort, an in-place comparison sorting algorithm. It works by dividing the list into a sorted part on the left and unsorted part on the right. It iterates through the list, finding the smallest element in the unsorted section and swapping it into place. This process continues until the list is fully sorted. Selection sort has a time complexity of O(n^2) in all cases. While it requires no extra storage, it is inefficient for large lists compared to other algorithms.
Binary trees are a non-linear data structure where each node has at most two children, used to represent hierarchical relationships, with nodes connected through parent-child links and traversed through preorder, inorder, and postorder methods; they can be represented through arrays or linked lists and support common operations like search, insert, and delete through comparing node values and restructuring child pointers.
This document discusses the implementation of a single linked list data structure. It describes the nodes that make up a linked list, which have an info field to store data and a next field pointing to the next node. The document outlines different ways to represent linked lists, including static arrays and dynamic pointers. It also provides algorithms for common linked list operations like traversing, inserting, and deleting nodes from the beginning, end, or a specified position within the list.
The document discusses various tree data structures and algorithms related to binary trees. It begins with an introduction to different types of binary trees such as strict binary trees, complete binary trees, and extended binary trees. It then covers tree traversal algorithms including preorder, inorder and postorder traversal. The document also discusses representations of binary trees using arrays and linked lists. Finally, it explains algorithms for operations on binary search trees such as searching, insertion, deletion and rebalancing through rotations in AVL trees.
Linked lists are linear data structures where each node contains a data field and a pointer to the next node. There are two types: singly linked lists where each node has a single next pointer, and doubly linked lists where each node has next and previous pointers. Common operations on linked lists include insertion and deletion which have O(1) time complexity for singly linked lists but require changing multiple pointers for doubly linked lists. Linked lists are useful when the number of elements is dynamic as they allow efficient insertions and deletions without shifting elements unlike arrays.
This PPT is all about the Tree basic on fundamentals of B and B+ Tree with it's Various (Search,Insert and Delete) Operations performed on it and their Examples...
The document discusses different searching algorithms. It describes sequential search which compares the search key to each element in the list sequentially until a match is found. The best case is 1 comparison, average is N/2 comparisons, and worst case is N comparisons. It also describes binary search which divides the sorted list in half at each step, requiring log(N) comparisons in the average and worst cases. The document also covers indexing which structures data for efficient retrieval based on key values and includes clustered vs unclustered indexes.
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.
The document discusses divide and conquer algorithms. It describes divide and conquer as a design strategy that involves dividing a problem into smaller subproblems, solving the subproblems recursively, and combining the solutions. It provides examples of divide and conquer algorithms like merge sort, quicksort, and binary search. Merge sort works by recursively sorting halves of an array until it is fully sorted. Quicksort selects a pivot element and partitions the array into subarrays of smaller and larger elements, recursively sorting the subarrays. Binary search recursively searches half-intervals of a sorted array to find a target value.
1) Trees are hierarchical data structures that store data in nodes connected by edges. They are useful for representing hierarchical relationships.
2) Binary search trees allow quick search, insertion, and deletion of nodes due to the organizational property that the value of each node is greater than all nodes in its left subtree and less than all nodes in its right subtree.
3) Common tree traversal algorithms include preorder, inorder, and postorder traversals, which visit nodes in different orders depending on whether the left/right children or root is visited first.
This document provides an overview of linear search and binary search algorithms.
It explains that linear search sequentially searches through an array one element at a time to find a target value. It is simple to implement but has poor efficiency as the time scales linearly with the size of the input.
Binary search is more efficient by cutting the search space in half at each step. It works on a sorted array by comparing the target to the middle element and determining which half to search next. The time complexity of binary search is logarithmic rather than linear.
This document discusses data structures and linked lists. It provides definitions and examples of different types of linked lists, including:
- Single linked lists, which contain nodes with a data field and a link to the next node.
- Circular linked lists, where the last node links back to the first node, forming a loop.
- Doubly linked lists, where each node contains links to both the previous and next nodes.
- Operations on linked lists such as insertion, deletion, traversal, and searching are also described.
The document discusses asymptotic notations that are used to describe the time complexity of algorithms. It introduces big O notation, which describes asymptotic upper bounds, big Omega notation for lower bounds, and big Theta notation for tight bounds. Common time complexities are described such as O(1) for constant time, O(log N) for logarithmic time, and O(N^2) for quadratic time. The notations allow analyzing how efficiently algorithms use resources like time and space as the input size increases.
The document discusses red-black trees, which are binary search trees augmented with node colors to guarantee a height of O(log n). It reviews the properties of red-black trees, including that every node is red or black, leaves are black, red nodes have black children, and all paths from root to leaf contain the same number of black nodes. It then proves that these properties ensure a height of O(log n) and thus search, minimum, maximum, successor and predecessor operations take O(log n) time. Insert and delete operations also take O(log n) time but require special handling to maintain the red-black tree properties during modification.
The document discusses red-black trees, which are self-balancing binary search trees. It describes the basic operations on dynamic sets like search, insert, and delete. It then explains the properties and structure of red-black trees, including that they ensure height is O(log n) through coloring nodes red or black. The insert and fixup algorithms are shown to maintain these properties and run in O(log n) time, providing efficient search tree operations.
The document discusses red-black trees and their operations. It reviews the properties of red-black trees that guarantee logarithmic height. It then describes that inserting nodes can violate properties and requires recoloring nodes or rotating the tree. The key steps of insertion involve adding the new node as red, then fixing any violations by moving them up the tree through recoloring and rotations.
Red-black trees are a self-balancing binary search tree. They ensure fast lookups, inserts, and deletes by keeping the height of the tree shallow. Insertion into a red-black tree may violate its balancing properties, so a fixup procedure is used. It recolors nodes red or black and performs rotations to ensure no more than one red link between any node and its children. This preserves the tree's balancing and keeps operations efficient.
The document discusses red-black trees, which are binary search trees augmented with node colors to guarantee a height of O(log n). It describes the properties that red-black trees must satisfy, including that every node is red or black, leaves are black, and if a node is red its children are black. It then proves that these properties ensure the height is O(log n) by showing a subtree has at least 2^bh - 1 nodes, where bh is the black-height. Finally, it notes that common operations like search, insert and delete run in O(log n) time on red-black trees.
Red-black trees are self-balancing binary search trees. They guarantee an O(log n) running time for operations by ensuring that no path from the root to a leaf is more than twice as long as any other. Nodes are colored red or black, and properties of the coloring are designed to keep the tree balanced. Inserting and deleting nodes may violate these properties, so rotations are used to restore the red-black properties and balance of the tree.
Short overview of balance tree: Data Structures, Binary search tree, BST Problem, Self Balancing BST, Usage, Red Black Trees, Red Black Insertion, AVL Tree, Rotations, B-Tree
BST+ RedBlackTrees CNN stands for Convolutional Neural Network.pptxssuser7b7f4e
It's a type of deep learning algorithm commonly used for image recognition and classification tasks. It's inspired by the human visual system and consists of multiple layers of neurons that perform convolutions, pooling, and activation functions to extract features from input images. CNNs have revolutionized fields like computer vision, achieving state-of-the-art performance in tasks such as object detection, image segmentation, and facial recognition.
Advanced data structures and implementationUmang Gupta
This document provides an overview of several advanced data structures and their implementations, including: top-down splay trees, red-black trees, top-down red-black trees, deterministic skip lists, AA-trees, treaps, k-d trees, and pairing heaps. For each data structure, key properties and implementation techniques are summarized, such as rotation types for splay trees, balancing rules and operations for red-black trees, and insertion and search processes for skip lists. Code implementations are referenced for several of the data structures.
This document discusses binary search trees and balanced binary search trees like AVL trees. It begins with an overview of binary search trees and their properties. It then introduces the concept of balance factors and describes how AVL trees maintain a balance factor between -1 and 1 for every node through rotations. The document provides examples of single and double rotations performed during insertion to rebalance the tree. It concludes with an algorithm for inserting a node into an AVL tree that searches back up the tree to perform necessary rotations after an insertion causes imbalance.
Red-black trees are a type of binary search tree where each node is colored red or black. They satisfy properties related to the coloring of nodes including that the root and leaves are black, children of red nodes are black, and all paths from the root to leaves contain the same number of black nodes. Red-black trees were introduced by R. Bayer in 1972 and have important applications in self-balancing binary search trees, but their combinatorial properties are not well studied. The document provides examples and recurrence relations for the number of red-black trees of a given size classified by their structure.
A binary tree is composed of nodes where each node contains a value and pointers to its left and right children. A binary tree traversal involves systematically visiting each node by traversing either breadth-first or depth-first. Breadth-first traversal visits nodes by level while depth-first traversal can be pre-order, in-order, or post-order depending on when the node is visited. Threaded binary trees reduce the number of null pointers by using them to point to other nodes for more efficient traversals.
This document provides information about different tree data structures including binary trees, binary search trees, AVL trees, red-black trees, splay trees, and B-trees. Binary search trees allow for fast searching and maintain an ordered structure. AVL and red-black trees are self-balancing binary search trees that ensure fast search, insert, and delete times by keeping the tree balanced. B-trees are multiway search trees that allow for efficient storage and retrieval of data in databases and file systems.
This document discusses binary search trees and red-black trees. It begins by defining binary search trees and describing their basic operations like search, insertion, and deletion which take time proportional to the height of the tree. It then introduces red-black trees, which add color attributes to nodes in a binary search tree to ensure the tree remains balanced, allowing operations to take O(log n) time. The properties of red-black trees are described, including that they guarantee all paths from a node to leaves contain the same number of black nodes.
- Red-black trees are a self-balancing binary search tree where each node is colored either red or black. This coloring ensures the tree remains balanced during insertions and deletions.
- Rotations are used to rebalance the tree if inserting a new node violates the red-black tree properties. Various cases for the position of the new node are considered.
- Searching, insertion, and deletion operations take O(log n) time due to the tree remaining balanced, improving on other unsorted data structures. Red-black trees are widely used for applications requiring efficient sorted data structures.
- A red-black tree is a self-balancing binary search tree where each node is colored red or black. It maintains the properties that the black height of each path is equal and there are no adjacent red nodes, ensuring O(log n) time for operations.
- Common operations like insertion, deletion, and searching on a red-black tree work similarly to a binary search tree but may require rotations and color changes to maintain the red-black properties.
- Red-black trees are widely used in applications that require efficient search structures for frequently updated data like process memory management and functional programming data structures.
The document defines common tree terminology and describes trees and binary trees. It discusses tree traversal methods including preorder, inorder, and postorder traversal. It also covers binary search trees, including their representation, properties, and common operations like searching, insertion, deletion, finding the minimum/maximum, and finding predecessors and successors. Key operations on binary search trees like searching, insertion, and deletion run in O(h) time where h is the tree height.
Trees are hierarchical data structures that can represent relationships between data items. They are useful for representing organizational charts, file systems, and programming environments. Key tree concepts include the root node, internal and leaf nodes, ancestors and descendants, subtrees, depth, height, and degree. Common tree operations include traversing the tree using preorder, inorder, and postorder traversal methods, evaluating expression trees, and using trees for data compression through Huffman coding. Huffman coding assigns variable-length binary codes to characters based on their frequency, allowing more common characters to have shorter codes to reduce the overall file size.
The document discusses tree data structures and binary search trees. It defines key tree terminology like root, internal and leaf nodes. It explains tree traversal algorithms like preorder, inorder and postorder traversal using recursion and iteration. It also describes operations on binary search trees like searching, inserting and deleting nodes. The complexity of these BST operations is provided. Algorithms to find the inorder predecessor and successor of a node in a BST are presented.
This document outlines the syllabus for an MTCSCS302 course on Soft Computing taught by Dr. Sandeep Kumar Poonia. The course covers topics including neural networks, fuzzy logic, probabilistic reasoning, and genetic algorithms. It is divided into five units: (1) neural networks, (2) fuzzy logic, (3) fuzzy arithmetic and logic, (4) neuro-fuzzy systems and applications of fuzzy logic, and (5) genetic algorithms and their applications. The goal of the course is to provide students with knowledge of soft computing fundamentals and approaches for solving complex real-world problems.
Artificial Bee Colony (ABC) is a swarm
optimization technique. This algorithm generally used to solve
nonlinear and complex problems. ABC is one of the simplest
and up to date population based probabilistic strategy for
global optimization. Analogous to other population based
algorithms, ABC also has some drawbacks computationally
pricey due to its sluggish temperament of search procedure.
The solution search equation of ABC is notably motivated by a
haphazard quantity which facilitates in exploration at the cost
of exploitation of the search space. Due to the large step size in
the solution search equation of ABC there are chances of
skipping the factual solution are higher. For that reason, this
paper introduces a new search strategy in order to balance the
diversity and convergence capability of the ABC. Both
employed bee phase and onlooker bee phase are improved
with help of a local search strategy stimulated by memetic
algorithm. This paper also proposes a new strategy for fitness
calculation and probability calculation. The proposed
algorithm is named as Improved Memetic Search in ABC
(IMeABC). It is tested over 13 impartial benchmark functions
of different complexities and two real word problems are also
considered to prove proposed algorithms superiority over
original ABC algorithm and its recent variants
Spider Monkey optimization (SMO) algorithm is newest addition in class of swarm intelligence. SMO is a population based stochastic meta-heuristic. It is motivated by intelligent foraging behaviour of fission fusion structured social creatures. SMO is a very good option for complex optimization problems. This paper proposed a modified strategy in order to enhance performance of original SMO. This paper introduces a position update strategy in SMO and modifies both local leader and global leader phase. The proposed strategy is named as Modified Position Update in Spider Monkey Optimization (MPU-SMO) algorithm. The proposed algorithm tested over benchmark problems and results show that it gives better results for considered unbiased problems.
Artificial Bee Colony (ABC) algorithm is a Nature Inspired Algorithm (NIA) which based in intelligent food foraging behaviour of honey bee swarm. ABC outperformed over other NIAs and other local search heuristics when tested for benchmark functions as well as factual world problems but occasionally it shows premature convergence and stagnation due to lack of balance between exploration and exploitation. This paper establishes a local search mechanism that enhances exploration capability of ABC and avoids the dilemma of stagnation. With help of recently introduces local search strategy it tries to balance intensification and diversification of search space. The anticipated algorithm named as Enhanced local search in ABC (EnABC) and tested over eleven benchmark functions. Results are evidence for its dominance over other competitive algorithms.
The document discusses a proposed Randomized Memetic Artificial Bee Colony (RMABC) algorithm for optimization problems. RMABC incorporates local search techniques into the Artificial Bee Colony algorithm to improve exploitation of promising solutions. It randomizes the step size in the local search to balance diversification and intensification. Experimental results on benchmark problems show RMABC outperforms other ABC algorithm variants in finding optimal solutions. The document provides background on optimization problems, nature-inspired algorithms, Artificial Bee Colony algorithm, and Memetic algorithms.
Differential Evolution (DE) is a renowned optimization stratagem that can easily solve nonlinear and comprehensive problems. DE is a well known and uncomplicated population based probabilistic approach for comprehensive optimization. It has apparently outperformed a number of Evolutionary Algorithms and further search heuristics in the vein of Particle Swarm Optimization at what time of testing over both yardstick and actual world problems. Nevertheless, DE, like other probabilistic optimization algorithms, from time to time exhibits precipitate convergence and stagnates at suboptimal position. In order to stay away from stagnation behavior while maintaining an excellent convergence speed, an innovative search strategy is introduced, named memetic search in DE. In the planned strategy, positions update equation customized as per a memetic search stratagem. In this strategy a better solution participates more times in the position modernize procedure. The position update equation is inspired from the memetic search in artificial bee colony algorithm. The proposed strategy is named as Memetic Search in Differential Evolution (MSDE). To prove efficiency and efficacy of MSDE, it is tested over 8 benchmark optimization problems and three real world optimization problems. A comparative analysis has also been carried out among proposed MSDE and original DE. Results show that the anticipated algorithm go one better than the basic DE and its recent deviations in a good number of the experiments.
Artificial Bee Colony (ABC) is a distinguished optimization strategy that can resolve nonlinear and multifaceted problems. It is comparatively a straightforward and modern population based probabilistic approach for comprehensive optimization. In the vein of the other population based algorithms, ABC is moreover computationally classy due to its slow nature of search procedure. The solution exploration equation of ABC is extensively influenced by a arbitrary quantity which helps in exploration at the cost of exploitation of the better search space. In the solution exploration equation of ABC due to the outsized step size the chance of skipping the factual solution is high. Therefore, here this paper improve onlooker bee phase with help of a local search strategy inspired by memetic algorithm to balance the diversity and convergence capability of the ABC. The proposed algorithm is named as Improved Onlooker Bee Phase in ABC (IoABC). It is tested over 12 well known un-biased test problems of diverse complexities and two engineering optimization problems; results show that the anticipated algorithm go one better than the basic ABC and its recent deviations in a good number of the experiments.
Artificial bee colony (ABC) algorithm is a well known and one of the latest swarm intelligence based techniques. This method is a population based meta-heuristic algorithm used for numerical optimization. It is based on the intelligent behavior of honey bees. Artificial Bee Colony algorithm is one of the most popular techniques that are used in optimization problems. Artificial Bee Colony algorithm has some major advantages over other heuristic methods. To utilize its good feature a number of researchers combined ABC algorithm with other methods, and generate some new hybrid methods. This paper provides comparative analysis of hybrid differential Artificial Bee Colony algorithm with hybrid ABC – SPSO, Genetic algorithm and Independent rough set approach based on some parameters like technique, dimension, methodology etc. KEYWORDS
Artificial bee colony (ABC) algorithm has proved its importance in solving a number of problems including engineering optimization problems. ABC algorithm is one of the most popular and youngest member of the family of population based nature inspired meta-heuristic swarm intelligence method. ABC has been proved its superiority over some other Nature Inspired Algorithms (NIA) when applied for both benchmark functions and real world problems. The performance of search process of ABC depends on a random value which tries to balance exploration and exploitation phase. In order to increase the performance it is required to balance the exploration of search space and exploitation of optimal solution of the ABC. This paper outlines a new hybrid of ABC algorithm with Genetic Algorithm. The proposed method integrates crossover operation from Genetic Algorithm (GA) with original ABC algorithm. The proposed method is named as Crossover based ABC (CbABC). The CbABC strengthens the exploitation phase of ABC as crossover enhances exploration of search space. The CbABC tested over four standard benchmark functions and a popular continuous optimization problem.
Multiplication of two 3 d sparse matrices using 1d arrays and linked listsDr Sandeep Kumar Poonia
A basic algorithm of 3D sparse matrix multiplication (BASMM) is presented using one dimensional (1D) arrays which is used further for multiplying two 3D sparse matrices using Linked Lists. In this algorithm, a general concept is derived in which we enter non- zeros elements in 1st and 2nd sparse matrices (3D) but store that values in 1D arrays and linked lists so that zeros could be removed or ignored to store in memory. The positions of that non-zero value are also stored in memory like row and column position. In this way space complexity is decreased. There are two ways to store the sparse matrix in memory. First is row major order and another is column major order. But, in this algorithm, row major order is used. Now multiplying those two matrices with the help of BASMM algorithm, time complexity also decreased. For the implementation of this, simple c programming and concepts of data structures are used which are very easy to understand for everyone.
This document summarizes a tool called Sunzip that uses the Huffman algorithm for data compression. It discusses how Huffman encoding works by assigning shorter bit codes to more common symbols to reduce file size. The tool analyzes files to determine symbol frequencies and builds a Huffman tree to assign variable-length codes. It allows compressing different data types like text, images, audio and video. Adaptive Huffman coding is also described, which dynamically updates the code tree as more data is processed. Benefits of Huffman compression include being fast, simple to implement and achieving close to optimal compression. Sample screenshots of the Sunzip tool are also provided showing file details before and after compression.
Artificial Bee Colony (ABC) algorithm is a Nature
Inspired Algorithm (NIA) which based on intelligent food
foraging behaviour of honey bee swarm. This paper introduces
a local search strategy that enhances exploration competence
of ABC and avoids the problem of stagnation. The proposed
strategy introduces two new local search phases in original
ABC. One just after onlooker bee phase and one after scout
bee phase. The newly introduced phases are inspired by
modified Golden Section Search (GSS) strategy. The proposed
strategy named as new local search strategy in ABC
(NLSSABC). The proposed NLSSABC algorithm applied over
thirteen standard benchmark functions in order to prove its
efficiency.
This document presents a new approach called mixed S-D slicing that combines static and dynamic program slicing using object-oriented concepts in C++. Static slicing analyzes the entire program code but produces larger slices, while dynamic slicing produces smaller slices based on a specific execution but is more difficult to compute. The mixed S-D slicing aims to generate dynamic slices faster by leveraging object-oriented features like classes. An example C++ program is provided to demonstrate the S-D slicing approach using concepts like classes, inheritance, and polymorphism. The approach is intended to reduce complexity and aid in debugging object-oriented programs by combining static and dynamic slicing techniques.
Performance evaluation of different routing protocols in wsn using different ...Dr Sandeep Kumar Poonia
This document evaluates the performance of different routing protocols in wireless sensor networks using various network parameters. It simulates the Dynamic Source Routing (DSR) and Adhoc On-Demand Distance Vector (AODV) routing protocols in a 1000m x 1000m terrain area with 100 sensor nodes. The packet delivery fraction, average throughput, and normalized routing load are analyzed at different node speeds ranging from 20-100m/s. The results show that AODV performs better than DSR in terms of packet delivery fraction and normalized routing load, while DSR has better average throughput performance. In conclusion, AODV is more optimal for small terrain areas when considering packet delivery and routing overhead, while DSR provides higher data rates.
Articial bee Colony algorithm (ABC) is a population based
heuristic search technique used for optimization problems. ABC
is a very eective optimization technique for continuous opti-
mization problem. Crossover operators have a better exploration
property so crossover operators are added to the ABC. This pa-
per presents ABC with dierent types of real coded crossover op-
erator and its application to Travelling Salesman Problem (TSP).
Each crossover operator is applied to two randomly selected par-
ents from current swarm. Two o-springs generated from crossover
and worst parent is replaced by best ospring, other parent remains
same. ABC with real coded crossover operator applied to travelling
salesman problem. The experimental result shows that our proposed
algorithm performs better than the ABC without crossover in terms
of eciency and accuracy.
This document describes a simulator for database aggregation using metadata. The simulator sits between an end-user application and a database management system (DBMS) to intercept SQL queries and transform them to take advantage of available aggregates using metadata describing the data warehouse schema. The simulator provides performance gains by optimizing queries to use appropriate aggregate tables. It was found to improve performance over previous aggregate navigators by making fewer calls to system tables through the use of metadata mappings. Experimental results showed the simulator solved queries faster than alternative approaches by transforming queries to leverage aggregate tables.
Performance evaluation of diff routing protocols in wsn using difft network p...Dr Sandeep Kumar Poonia
In the recent past, wireless sensor networks have been introduced to use in many applications. To design the networks, the factors needed to be considered are the coverage area, mobility, power consumption, communication capabilities etc. The challenging goal of our project is to create a simulator to support the wireless sensor network simulation. The network simulator (NS-2) which supports both wire and wireless networks is implemented to be used with the wireless sensor network.
The Traveling Salesman Problem (TSP) involves finding the minimum cost tour that visits each customer exactly once and returns to the starting depot. Key heuristics to solve the TSP include nearest neighbor, insertion methods, and 2-opt exchanges. The Vehicle Routing Problem (VRP) extends the TSP by routing multiple vehicles of limited capacity from a central depot to serve customer demands. Common heuristics for the VRP include savings algorithms and sweep methods.
This document provides an overview of linear programming, including:
- It describes the linear programming model which involves maximizing a linear objective function subject to linear constraints.
- It provides examples of linear programming problems like product mix, blending, transportation, and network flow problems.
- It explains how to develop a linear programming model by defining decision variables, the objective function, and constraints.
- It discusses solutions methods like the graphical and simplex methods. The simplex method involves iteratively moving to adjacent extreme points to maximize the objective function.
This document discusses approximation algorithms and introduces several combinatorial optimization problems. It begins by explaining that approximation algorithms are needed to find near-optimal solutions for problems that cannot be solved in polynomial time, such as set cover and bin packing. It then provides examples of problems that are in P, NP, and NP-complete. Several techniques for designing approximation algorithms are outlined, including greedy algorithms, linear programming, and semidefinite programming. Specific NP-complete problems like vertex cover, set cover, and independent set are introduced and approximations algorithms with performance guarantees are provided for set cover and vertex cover.
CapTechTalks Webinar Slides June 2024 Donovan Wright.pptxCapitolTechU
Slides from a Capitol Technology University webinar held June 20, 2024. The webinar featured Dr. Donovan Wright, presenting on the Department of Defense Digital Transformation.
Brand Guideline of Bashundhara A4 Paper - 2024khabri85
It outlines the basic identity elements such as symbol, logotype, colors, and typefaces. It provides examples of applying the identity to materials like letterhead, business cards, reports, folders, and websites.
Decolonizing Universal Design for LearningFrederic Fovet
UDL has gained in popularity over the last decade both in the K-12 and the post-secondary sectors. The usefulness of UDL to create inclusive learning experiences for the full array of diverse learners has been well documented in the literature, and there is now increasing scholarship examining the process of integrating UDL strategically across organisations. One concern, however, remains under-reported and under-researched. Much of the scholarship on UDL ironically remains while and Eurocentric. Even if UDL, as a discourse, considers the decolonization of the curriculum, it is abundantly clear that the research and advocacy related to UDL originates almost exclusively from the Global North and from a Euro-Caucasian authorship. It is argued that it is high time for the way UDL has been monopolized by Global North scholars and practitioners to be challenged. Voices discussing and framing UDL, from the Global South and Indigenous communities, must be amplified and showcased in order to rectify this glaring imbalance and contradiction.
This session represents an opportunity for the author to reflect on a volume he has just finished editing entitled Decolonizing UDL and to highlight and share insights into the key innovations, promising practices, and calls for change, originating from the Global South and Indigenous Communities, that have woven the canvas of this book. The session seeks to create a space for critical dialogue, for the challenging of existing power dynamics within the UDL scholarship, and for the emergence of transformative voices from underrepresented communities. The workshop will use the UDL principles scrupulously to engage participants in diverse ways (challenging single story approaches to the narrative that surrounds UDL implementation) , as well as offer multiple means of action and expression for them to gain ownership over the key themes and concerns of the session (by encouraging a broad range of interventions, contributions, and stances).
How to Create a Stage or a Pipeline in Odoo 17 CRMCeline George
Using CRM module, we can manage and keep track of all new leads and opportunities in one location. It helps to manage your sales pipeline with customizable stages. In this slide let’s discuss how to create a stage or pipeline inside the CRM module in odoo 17.
Cross-Cultural Leadership and CommunicationMattVassar1
Business is done in many different ways across the world. How you connect with colleagues and communicate feedback constructively differs tremendously depending on where a person comes from. Drawing on the culture map from the cultural anthropologist, Erin Meyer, this class discusses how best to manage effectively across the invisible lines of culture.
Post init hook in the odoo 17 ERP ModuleCeline George
In Odoo, hooks are functions that are presented as a string in the __init__ file of a module. They are the functions that can execute before and after the existing code.
How to Download & Install Module From the Odoo App Store in Odoo 17Celine George
Custom modules offer the flexibility to extend Odoo's capabilities, address unique requirements, and optimize workflows to align seamlessly with your organization's processes. By leveraging custom modules, businesses can unlock greater efficiency, productivity, and innovation, empowering them to stay competitive in today's dynamic market landscape. In this tutorial, we'll guide you step by step on how to easily download and install modules from the Odoo App Store.
The Science of Learning: implications for modern teachingDerek Wenmoth
Keynote presentation to the Educational Leaders hui Kōkiritia Marautanga held in Auckland on 26 June 2024. Provides a high level overview of the history and development of the science of learning, and implications for the design of learning in our modern schools and classrooms.
Artificial Intelligence (AI) has revolutionized the creation of images and videos, enabling the generation of highly realistic and imaginative visual content. Utilizing advanced techniques like Generative Adversarial Networks (GANs) and neural style transfer, AI can transform simple sketches into detailed artwork or blend various styles into unique visual masterpieces. GANs, in particular, function by pitting two neural networks against each other, resulting in the production of remarkably lifelike images. AI's ability to analyze and learn from vast datasets allows it to create visuals that not only mimic human creativity but also push the boundaries of artistic expression, making it a powerful tool in digital media and entertainment industries.
3. Binary search trees
● Binary search trees are an important data
structure for dynamic sets.
● Accomplish many dynamic-set operations in
O(h) time, where h=height of tree.
● we represent a binary tree by a linked data
structure in which each node is an object.
● T:root points to the root of tree T .
4. Binary search trees
● Each node contains the attributes
■ key (and possibly other satellite data).
■ left: points to left child.
■ right: points to right child.
■ p: points to parent. T.root.p = NIL.
5. Binary search trees
● Stored keys must satisfy the binary-search-
tree property.
■ If y is in left subtree of x, then y.key <= x.key.
■ If y is in right subtree of x, then y.key >= x.key.
6. Binary search trees
(a) A binary search tree on 6 nodes with height 2.
(b) A less efficient binary search tree with height 4 that
contains the same keys.
8. Binary search trees
● The binary-search-tree property allows us to
print out all the keys in a binary search tree in
sorted order by a simple recursive algorithm,
called an inorder tree walk.
It takes ‚
time to
walk an n-node
binary search tree
9. Binary search trees
● A: prints elements in sorted (increasing) order
● This is called an inorder tree walk
■ Preorder tree walk: print root, then left, then right
■ Postorder tree walk: print left, then right, then root
10. Tree traversal
● Used to print out the data in a tree in a certain
order
● Pre-order traversal
■ Print the data at the root
■ Recursively print out all data in the left subtree
■ Recursively print out all data in the right subtree
11. Preorder, Postorder and Inorder
● Preorder traversal
■ node, left, right
■ prefix expression
○ ++a*bc*+*defg
16. insert
● Proceed down the tree as you would with a find
● If X is found, do nothing (or update something)
● Otherwise, insert X at the last spot on the path traversed
● Time complexity = O(height of the tree)
17. delete
● When we delete a node, we need to consider
how we take care of the children of the deleted
node.
■ This has to be done such that the property of the
search tree is maintained.
18. delete
Three cases:
(1) the node is a leaf
■ Delete it immediately
(2) the node has one child
■ Adjust a pointer from the parent to bypass that node
19. delete
(3) the node has 2 children
■ replace the key of that node with the minimum element at the right
subtree
■ delete the minimum element
○ Has either no child or only right child because if it has a left child, that
left child would be smaller and would have been chosen. So invoke
case 1 or 2.
●
Time complexity = O(height of the tree)
20. Balanced Binary Search Trees
A binary search tree can implement any of the basic dynamic-set
operations in O(h) time. These operations are O(lgn) if tree is
“balanced”.
There has been lots of interest in developing algorithms to keep binary
search trees balanced, including
1st type: insert nodes as is done in the BST insert, then rebalance tree
Red-Black trees
AVL trees
Splay trees
2nd type: allow more than one key per node of the search tree:
2-3 trees
2-3-4 trees
B-trees
21. Red-Black Trees (RBT)
Red-Black tree: BST in which each node is colored red or black.
Constraints on the coloring and connection of nodes ensure that
no root to leaf path is more than twice as long as any other, so
tree is approximately balanced.
Each RBT node contains fields left, right, parent, color, and key.
L
E
F
T
PARENT
KEY
COLOR
R
I
G
H
T
22. Red-Black Trees
● Red-black trees:
■ Binary search trees augmented with node color
■ Operations designed to guarantee that the height
h = O(lg n)
● First: describe the properties of red-black trees
● Then: prove that these guarantee h = O(lg n)
● Finally: describe operations on red-black trees
23. Red-Black Properties
● The red-black properties:
1. Every node is either red or black
2. Every leaf (NULL pointer) is black
○ Note: this means every “real” node has 2 children
3. If a node is red, both children are black
○ Note: can’t have 2 consecutive reds on a path
4. Every path from node to descendent leaf contains
the same number of black nodes
5. The root is always black
24. Red-Black Trees
● Put example on board and verify properties:
1.
2.
3.
4.
Every node is either red or black
Every leaf (NULL pointer) is black
If a node is red, both children are black
Every path from node to descendent leaf contains
the same number of black nodes
5. The root is always black
● black-height: # black nodes on path to leaf
■ Label example with h and bh values
25. Black Height bh(x)
Black-height of a node x: bh(x) is the number of black nodes (including
the NIL leaf) on the path from x to a leaf, not counting x itself.
2 20
2 18
1 17
0
0
1 22
1 19
0
Every node has a
black-height, bh(x).
For all NIL leaves,
bh(x) = 0.
1 21 1 25
0
0
0 0
0
For root x,
bh(x) = bh(T).
26. Height of a Red-black Tree
h=4
26 bh=2
● Example:
● Height of a node:
■ Number of edges in a
longest path to a leaf.
● Black-height of a node
17
h=1
bh=1
h=2 30
bh=1
bh(x) is the number of
black nodes on path
from x to leaf, not
counting x.
nil[T]
h=3
41 bh=2
h=1
bh=1
38
h=2
47 bh=1
h=1 50
bh=1
27. Height of Red-Black Trees
● What is the minimum black-height of a node
●
●
●
●
with height h?
A height-h node has black-height h/2
Proof : By property 4,
h/2 nodes on the path from the node to a leaf
are red.
Hence
are black.
28. Height of Red-Black Trees
● The subtree rooted at any node x contains >= 2bh(x)_1
internal nodes.
● Proof :By induction height of x =0x is a leafbh(x)=0.
The subtree rooted at x has 0 internal nodes. 20 -1 = 0.
● Let the height of x be h and bh(x) = b.
● Any child of x has height h -1 and
● black-height either b (if the child is red) or
● b -1 (if the child is black)
● By induction each child has >= 2bh(x)-1-1 internal nodes
● Thus, the subtree rooted at x contains >= 2(2bh(x)-1-1)+1
●
= 2bh(x)-1(internal Nodes)
29. Height of Red-Black Trees
● Theorem: A red-black tree with n internal
nodes has height h 2 lg(n + 1)
● How do you suppose we’ll prove this?
● Proof: The subtree rooted at any node x contains
● >= 2bh(x)_1 internal nodes.
● A height-h node has black-height h/2
n 2h/2 -1
n + 1 2h/2
lg(n+1) h/2 h 2lg(n+1)
● Thus
●
●
30. RB Trees: Worst-Case Time
● So we’ve proved that a red-black tree has
O(lg n) height
● Corollary: These operations take O(lg n) time:
■ Minimum(), Maximum()
■ Successor(), Predecessor()
■ Search()
● Insert() and Delete():
■ Will also take O(lg n) time
■ But will need special care since they modify tree
31. Red-Black Trees: An Example
● Color this tree:
7
5
9
12
Red-black properties:
1.
Every node is either red or black
2.
Every leaf (NULL pointer) is black
3.
If a node is red, both children are black
4.
Every path from node to descendent leaf
contains the same number of black nodes
5.
The root is always black
32. Red-Black Trees:
The Problem With Insertion
● Insert 8
■ Where does it go?
7
5
9
12
1.
2.
3.
4.
5.
Every node is either red or black
Every leaf (NULL pointer) is black
If a node is red, both children are black
Every path from node to descendent leaf
contains the same number of black nodes
The root is always black
33. Red-Black Trees:
The Problem With Insertion
● Insert 8
■ Where does it go?
■ What color
7
5
should it be?
1.
2.
3.
4.
5.
Every node is either red or black
Every leaf (NULL pointer) is black
If a node is red, both children are black
Every path from node to descendent leaf
contains the same number of black nodes
The root is always black
9
8
12
34. Red-Black Trees:
The Problem With Insertion
● Insert 8
■ Where does it go?
■ What color
7
5
should it be?
1.
2.
3.
4.
5.
Every node is either red or black
Every leaf (NULL pointer) is black
If a node is red, both children are black
Every path from node to descendent leaf
contains the same number of black nodes
The root is always black
9
8
12
35. Red-Black Trees:
The Problem With Insertion
● Insert 11
■ Where does it go?
7
5
9
8
1.
2.
3.
4.
5.
Every node is either red or black
Every leaf (NULL pointer) is black
If a node is red, both children are black
Every path from node to descendent leaf
contains the same number of black nodes
The root is always black
12
36. Red-Black Trees:
The Problem With Insertion
● Insert 11
■ Where does it go?
■ What color?
7
5
9
8
12
11
1.
2.
3.
4.
5.
Every node is either red or black
Every leaf (NULL pointer) is black
If a node is red, both children are black
Every path from node to descendent leaf
contains the same number of black nodes
The root is always black
37. Red-Black Trees:
The Problem With Insertion
● Insert 11
■ Where does it go?
■ What color?
○ Can’t be red! (#3)
7
5
9
8
12
11
1.
2.
3.
4.
5.
Every node is either red or black
Every leaf (NULL pointer) is black
If a node is red, both children are black
Every path from node to descendent leaf
contains the same number of black nodes
The root is always black
38. Red-Black Trees:
The Problem With Insertion
● Insert 11
■ Where does it go?
■ What color?
○ Can’t be red! (#3)
○ Can’t be black! (#4)
1.
2.
3.
4.
5.
7
5
Every node is either red or black
Every leaf (NULL pointer) is black
If a node is red, both children are black
Every path from node to descendent leaf
contains the same number of black nodes
The root is always black
9
8
12
11
39. Red-Black Trees:
The Problem With Insertion
● Insert 11
■ Where does it go?
■ What color?
○ Solution:
recolor the tree
1.
2.
3.
4.
5.
7
5
Every node is either red or black
Every leaf (NULL pointer) is black
If a node is red, both children are black
Every path from node to descendent leaf
contains the same number of black nodes
The root is always black
9
8
12
11
40. Red-Black Trees:
The Problem With Insertion
● Insert 10
■ Where does it go?
7
5
9
8
12
11
1.
2.
3.
4.
5.
Every node is either red or black
Every leaf (NULL pointer) is black
If a node is red, both children are black
Every path from node to descendent leaf
contains the same number of black nodes
The root is always black
41. Red-Black Trees:
The Problem With Insertion
● Insert 10
■ Where does it go?
■ What color?
7
5
9
8
12
11
1.
2.
3.
4.
5.
Every node is either red or black
Every leaf (NULL pointer) is black
If a node is red, both children are black
Every path from node to descendent leaf
contains the same number of black nodes
The root is always black
10
42. Red-Black Trees:
The Problem With Insertion
● Insert 10
■ Where does it go?
7
5
■ What color?
○ A: no color! Tree
is too imbalanced
○ Must change tree structure
to allow recoloring
■ Goal: restructure tree in
O(lg n) time
9
8
12
11
10
43. RB Trees: Rotation
● Our basic operation for changing tree structure
is called rotation:
y
x
x
rightRotate(y)
C
A
y
leftRotate(x)
A
B
B
C
● Does rotation preserve inorder key ordering?
● What would the code for rightRotate()
actually do?
46. RB Trees: Rotation
Left-Rotate (T, x)
1. y right[x] // Set y.
2. right[x] left[y] //Turn y’s left subtree into x’s right subtree.
3. if left[y] nil[T ]
4.
then p[left[y]] x
5. p[y] p[x]
// Link x’s parent to y.
6. if p[x] == nil[T ]
7.
then root[T ] y
8.
else if x == left[p[x]]
9.
then left[p[x]] y
10.
else right[p[x]] y
11. left[y] x
// Put x on y’s left.
12. p[x] y
47. RB Trees: Rotation
right-Rotate (T, y)
1. x right[y]
2. left[y] right[x]
3. If right[x] nil[T ]
4.
then p[right[x]] y
5. p[x] p[y]
6. if p[y] == nil[T ]
7.
then root[T ] x
8.
else if y == left[p[y]]
9.
then left[p[y]] x
10.
else right[p[y]] x
11. right[x] y
12. p[y] x
50. Red-Black Trees: Insertion
● Remember:
1. Insert nodes one at a time, and after every
Insertion balance the tree.
2. Every node inserted starts as a Red node.
3. Consult the cases, Every time two Red nodes
touch must rebalance at that point.
4. The root will always be Black.
51. Red-Black Trees: Insertion
● If we insert, what color to make the new node?
■ Red? Might violate property 3(If a node is red, both
children are black).
■ Black? Might violate property 4(Every path from node
to descendent leaf contains the same number of black nodes).
Insertion: the basic idea
■ Insert x into tree, color x red
■ Only r-b property 3 might be violated (if p[x] red)
○ If so, move violation up tree until a place is found where it
can be fixed
■ Total time will be O(lg n)
52. Reminder: Red-black Properties
1. Every node is either red or black
2. Every leaf (NULL pointer) is black
3. If a node is red, both children are black
4. Every path from node to descendent leaf
contains the same number of black nodes
5. The root is always black
53. Insertion in RB Trees
● Insertion must preserve all red-black properties.
● Should an inserted node be colored Red? Black?
● Basic steps:
■ Use Tree-Insert from BST (slightly modified) to
insert a node x into T.
○ Procedure RB-Insert(x).
■ Color the node x red.
■ Fix the modified tree by re-coloring nodes and
performing rotation to preserve RB tree property.
○ Procedure RB-Insert-Fixup.
54. Algorithm: Insertion
We have detected a need for balance when z is red and his parent too.
• If z has a red uncle: colour the parent and uncle black, and
grandparent red.
z
Balanced trees: Red-black trees
55. Algorithm: Insertion
We have detected a need for balance when z is red and his parent too.
• If z has a red uncle: colour the parent and uncle black, and
grandparent red.
• If z is a left child and has a black uncle: colour the parent black and
the grandparent red, then rotateRight(z.parent.parent)
Balanced trees: Red-black trees
56. Algorithm: Insertion
We have detected a need for balance when z is red and his parent too.
• If z has a red uncle: colour the parent and uncle black, and
grandparent red.
right child and has black uncle: colour the parent black and
• If z is a left child and has a a black uncle, then rotateLeft(z.parent)
andgrandparent red, then rotateRight(z.parent.parent)
the
Balanced trees: Red-black trees
57. Algorithm: Insertion
Let’s insert 4, 7, 12, 15, 3 and 5.
4
7
1
2
Double red violation!
It also shows it’s
unbalanced…
Balanced trees: Red-black trees
58. Algorithm: Insertion
Let’s insert 4, 7, 12, 15, 3 and 5.
7
4
What should we do?
Nothing, no
double red.
3
12
5
15
Double red violation.
We can’t have a better
balance, and there is a
red uncle…
Balanced trees: Red-black trees
59. Insertion
RB-Insert(T, z)
1.
y nil[T]
2.
x root[T]
3.
while x nil[T]
4.
do y x
5.
if key[z] < key[x]
6.
then x left[x]
7.
else x right[x]
8.
p[z] y
9.
if y = nil[T]
10.
then root[T] z
11.
else if key[z] < key[y]
12.
then left[y] z
13.
else right[y] z
RB-Insert(T, z) Contd.
14. left[z] nil[T]
15. right[z] nil[T]
16. color[z] RED
17. RB-Insert-Fixup (T, z)
How does it differ from the TreeInsert procedure of BSTs?
Which of the RB properties might
be violated?
Fix the violations by calling RBInsert-Fixup.
60. Insertion – Fixup
● Which property might be violated?
1. OK(Every node is either red or black)
2. If z is the root, then there’s a violation. Otherwise,
OK(The root is black)
3. OK(Every leaf (NIL) is black)
4. If z.p is red, there’s a violation: both z and z.p are
red(If a node is red, then both its children are black)
● OK(For each node, all simple paths from the node to descendant
leaves contain the same number of black nodes)
● Remove the violation by calling RB-INSERT-FIXUP:
61. Insertion – Fixup
● Problem: we may have one pair of consecutive
reds where we did the insertion.
● Solution: rotate it up the tree and away…
Three cases have to be handled…
62. Insertion – Fixup
RB-Insert-Fixup (T, z)
1.
while color[p[z]] == RED
2.
do if p[z] == left[p[p[z]]]
3.
then y right[p[p[z]]]
4.
if color[y] == RED
5.
then color[p[z]] BLACK // Case 1
6.
color[y] BLACK
// Case 1
7.
color[p[p[z]]] RED // Case 1
8.
z p[p[z]]
// Case 1
63. Insertion – Fixup
RB-Insert-Fixup(T, z) (Contd.)
9.
else if z == right[p[z]] // color[y] RED
10.
then z p[z]
// Case 2
11.
LEFT-ROTATE(T, z)
// Case 2
12.
color[p[z]] BLACK
// Case 3
13.
color[p[p[z]]] RED
// Case 3
14.
RIGHT-ROTATE(T, p[p[z]]) // Case 3
15.
else (if p[z] = right[p[p[z]]])(same as 10-14
16.
with “right” and “left” exchanged)
17. color[root[T ]] BLACK
64. Insertion – Fixup
A node z ’after insertion. Because both z and its parent z.p are red, a
violation of property 4 occurs. Since z’s uncle y is red, case 1 in the
code applies. We recolor nodes and move the pointer z up the tree
65. Insertion – Fixup
Once again, z and its parent are both red, but z’s uncle y
is black. Since z is the right child of z.p, case 2 applies.
We perform a left rotation,
66. Insertion – Fixup
Now, z is the left child of its parent, and case 3 applies.
Recoloring and right rotation yield the tree
68. Correctness
Loop invariant:
● At the start of each iteration of the while loop,
■ z is red.
■ If p[z] is the root, then p[z] is black.
■ There is at most one red-black violation:
○ Property 2: z is a red root, or
○ Property 4: z and p[z] are both red.
69. Correctness – Contd.
● Initialization:
● Termination: The loop terminates only if p[z] is
black. Hence, property 4 is OK.
The last line ensures property 2 always holds.
● Maintenance: We drop out when z is the root (since
then p[z] is sentinel nil[T ], which is black). When we
start the loop body, the only violation is of property 4.
■ There are 6 cases, 3 of which are symmetric to the other 3.
We consider cases in which p[z] is a left child.
■ Let y be z’s uncle (p[z]’s sibling).
70. Case 1 – uncle y is red
p[p[z]]
new z
C
C
p[z]
y
A
D
z
A
B
D
B
z is a right child here.
Similar steps if z is a left child.
● p[p[z]] (z’s grandparent) must be black, since z and p[z] are both red and there
are no other violations of property 4.
● Make p[z] and y black
now z and p[z] are not both red. But property 5
might now be violated.
● Make p[p[z]] red
restores property 5.
● The next iteration has p[p[z]] as the new z (i.e., z moves up 2 levels).
71. Case 1 – uncle y is red
We take the same action whether z is a right child or z is a left child.
Each subtree has a black root, and each has the same black-height.
The code for case 1 changes the colors of some nodes, preserving property
5: all downward simple paths from a node to a leaf have the same number
of blacks. The while loop continues with node z’s grandparent z.p.p as
the new z. Any violation of property 4 can now occur only between the new
z, which is red, and its parent, if it is red as well.
72. Case 2 – y is black, z is a right child
C
C
p[z]
p[z]
y
A
y
B
z
z
B
A
● Left rotate around p[z], p[z] and z switch roles now z is a left
child, and both z and p[z] are red.
● Takes us immediately to case 3.
73. Case 3 – y is black, z is a left child
B
C
p[z]
y
B
z
A
A
C
● Make p[z] black and p[p[z]] red.
● Then right rotate on p[p[z]]. Ensures property 4 is maintained.
● No longer have 2 reds in a row.
● p[z] is now black no more iterations.
74. Cases 2 and 3 of the procedure RB-INSERT-FIXUP
As in case 1, property 4 is violated in either case 2 or case 3 because z
and its parent z.p are both red. Each subtree has a black root , and each
has the same black-height. We transform case 2 into case 3 by a left
rotation, which preserves property 5: all downward simple paths from a
node to a leaf have the same number of blacks. Case 3 causes some
color changes and a right rotation, which also preserve property 5. The
while loop then terminates, because property 4 is satisfied: there are no
longer two red nodes in a row.
75. Algorithm Analysis
● O(lg n) time to get through RB-Insert up to the
call of RB-Insert-Fixup.
● Within RB-Insert-Fixup:
■ Each iteration takes O(1) time.
■ Each iteration but the last moves z up 2 levels.
■ O(lg n) levels O(lg n) time.
■ Thus, insertion in a red-black tree takes O(lg n) time.
■ Note: there are at most 2 rotations overall.
76. Deletion
● Deletion, like insertion, should preserve all the
RB properties.
● The properties that may be violated depends on
the color of the deleted node.
■ Red – OK. Why?
■ Black?
● Steps:
■ Do regular BST deletion.
■ Fix any violations of RB properties that may result.
77. Deletion
RB-Delete(T, z)
1.
if left[z] == nil[T] or right[z] == nil[T]
2.
then y z
3.
else y TREE-SUCCESSOR(z)
4.
if left[y] == nil[T ]
5.
then x left[y]
6.
else x right[y]
7.
p[x] p[y] // Do this, even if x is nil[T]
78. Deletion
RB-Delete (T, z) (Contd.)
8. if p[y] == nil[T ]
9.
then root[T ] x
10. else if y == left[p[y]]
11.
then left[p[y]] x
12.
else right[p[y]] x
13. if y == z
14. then key[z] key[y]
15. copy y’s satellite data into z
16. if color[y] == BLACK
17. then RB-Delete-Fixup(T, x)
18. return y
The node passed to
the fixup routine is the
lone child of the
spliced up node, or
the sentinel.
79. RB Properties Violation
● If the delete node is red?
Not a problem – no RB properties violated
● If y is black, we could have violations of redblack properties:
■ Prop. 1. OK.
■ Prop. 2. If y is the root and x is red, then the root has
become red.
■ Prop. 3. OK.
■ Prop. 4. Violation if p[y] and x are both red.
■ Prop. 5. Any path containing y now has 1 fewer black
80. RB Properties Violation
● Prop. 5. Any path containing y now has 1 fewer black
node.
■ Correct by giving x an “extra black.”
■ Add 1 to count of black nodes on paths containing x.
■ Now property 5 is OK, but property 1 is not.
■ x is either doubly black (if color[x] == BLACK) or red &
black (if color[x] == RED).
■ The attribute color[x] is still either RED or BLACK. No
new values for color attribute.
■ In other words, the extra blackness on a node is by virtue of
x pointing to the node.
● Remove the violations by calling RB-Delete-Fixup.
81. Deletion – Fixup
RB-Delete-Fixup(T, x)
1.
while x root[T ] and color[x] == BLACK
2.
do if x == left[p[x]]
3.
then w right[p[x]]
4.
if color[w] == RED
5.
then color[w] BLACK
// Case 1
6.
color[p[x]] RED
// Case 1
7.
LEFT-ROTATE(T, p[x])
// Case 1
8.
w right[p[x]]
// Case 1
82. RB-Delete-Fixup(T, x) (Contd.)
/* x is still left[p[x]] */
9.
if color[left[w]] == BLACK and color[right[w]] == BLACK
10.
then color[w] RED
// Case 2
11.
x p[x]
// Case 2
12.
else if color[right[w]] == BLACK
13.
then color[left[w]] BLACK
// Case 3
14.
color[w] RED
// Case 3
15.
RIGHT-ROTATE(T,w)
// Case 3
16.
w right[p[x]]
// Case 3
17.
color[w] color[p[x]]
// Case 4
18.
color[p[x]] BLACK
// Case 4
19.
color[right[w]] BLACK
// Case 4
20.
LEFT-ROTATE(T, p[x])
// Case 4
21.
x root[T ]
// Case 4
22.
else (same as then clause with “right” and “left” exchanged)
23. color[x] BLACK
83. Deletion – Fixup
● Idea: Move the extra black up the tree until x points
to a red & black node turn it into a black node,
● x points to the root just remove the extra black, or
● We can do certain rotations and recolorings and
finish.
● Within the while loop:
■ x always points to a nonroot doubly black node.
■ w is x’s sibling.
■ w cannot be nil[T ], since that would violate property 5 at
p[x].
● 8 cases in all, 4 of which are symmetric to the other.
84. Case 1 – w is red
p[x]
B
w
x
A
D
B
D
x
C
E
A
E
new
w
C
● w must have black children.
● Make w black and p[x] red (because w is red p[x] couldn’t have
been red).
● Then left rotate on p[x].
● New sibling of x was a child of w before rotation must be
black.
● Go immediately to case 2, 3, or 4.
85. Case 2 – w is black, both w’s children
p[x]
are black new x
c
B
w
x
A
B
D
A
C
E
c
D
C
E
● Take 1 black off x ( singly black) and off w ( red).
● Move that black to p[x].
● Do the next iteration with p[x] as the new x.
● If entered this case from case 1, then p[x] was red new x is
red & black color attribute of new x is RED loop
terminates. Then new x is made black in the last line.
86. Case 3 – w is black, w’s left child is
c
red, w’s right child is black
c
B
w
x
A
B
x
D
A
C
E
new w
C
D
E
● Make w red and w’s left child black.
● Then right rotate on w.
● New sibling w of x is black with a red right child case 4.
87. Case 4 – w is black, w’s right child is
red
c
B
w
x
A
D
B
D
C
c’
x
E
E
A
C
● Make w be p[x]’s color (c).
● Make p[x] black and w’s right child black.
● Then left rotate on p[x].
● Remove the extra black on x ( x is now singly black) without
violating any red-black properties.
● All done. Setting x to root causes the loop to terminate.