ძებნის ორობითი ხეები BST (Binary Search Trees)

Slides:



Advertisements
Similar presentations
Jan Binary Search Trees What is a search binary tree? Inorder search of a binary search tree Find Min & Max Predecessor and successor BST insertion.
Advertisements

Analysis of Algorithms CS 477/677 Binary Search Trees Instructor: George Bebis (Appendix B5.2, Chapter 12)
Binary Search Trees Many of the slides are from Prof. Plaisted’s resources at University of North Carolina at Chapel Hill.
Introduction to Algorithms Jiafen Liu Sept
ALGORITHMS THIRD YEAR BANHA UNIVERSITY FACULTY OF COMPUTERS AND INFORMATIC Lecture six Dr. Hamdy M. Mousa.
The complexity and correctness of algorithms (with binary trees as an example)
Chapter 12 Binary search trees Lee, Hsiu-Hui Ack: This presentation is based on the lecture slides from Hsu, Lih-Hsing, as well as various materials from.
David Luebke 1 5/4/2015 Binary Search Trees. David Luebke 2 5/4/2015 Dynamic Sets ● Want a data structure for dynamic sets ■ Elements have a key and satellite.
Binary Search Trees Comp 550.
Analysis of Algorithms CS 477/677 Instructor: Monica Nicolescu Lecture 10.
1 Brief review of the material so far Recursive procedures, recursive data structures –Pseudocode for algorithms Example: algorithm(s) to compute a n Example:
Analysis of Algorithms CS 477/677 Instructor: Monica Nicolescu Lecture 12.
Analysis of Algorithms CS 477/677 Instructor: Monica Nicolescu Lecture 11.
Sorting. How fast can we sort? All the sorting algorithms we have seen so far are comparison sorts: only use comparisons to determine the relative order.
COMP 171 Data Structures and Algorithms Tutorial 6 Binary Search Trees.
Analysis of Algorithms CS 477/677 Red-Black Trees Instructor: George Bebis (Chapter 14)
Comp 122, Spring 2004 Red-Black Trees. redblack - 2 Comp 122, Spring 2004 Red-black trees: Overview  Red-black trees are a variation of binary search.
1.1 Data Structure and Algorithm Lecture 12 Binary Search Trees Topics Reference: Introduction to Algorithm by Cormen Chapter 13: Binary Search Trees.
Data Structures, Spring 2006 © L. Joskowicz 1 Data Structures – LECTURE Binary search trees Motivation Operations on binary search trees: –Search –Minimum,
13. Red-Black Tree Hsu, Lih-Hsing. Computer Theory Lab. Chapter 13P.2 One of many search-tree schemes that are “ balanced ” in order to guarantee that.
DAST 2005 Tirgul 7 Binary Search Trees. DAST 2005 Motivation We would like to have a dynamic ADT that efficiently supports the following common operations:
12.Binary Search Trees Hsu, Lih-Hsing. Computer Theory Lab. Chapter 12P What is a binary search tree? Binary-search property: Let x be a node in.
Design & Analysis of Algorithms Unit 2 ADVANCED DATA STRUCTURE.
Chapter 12. Binary Search Trees. Search Trees Data structures that support many dynamic-set operations. Can be used both as a dictionary and as a priority.
CS 3343: Analysis of Algorithms Lecture 16: Binary search trees & red- black trees.
Red-Black Trees Many of the slides are from Prof. Plaisted’s resources at University of North Carolina at Chapel Hill.
Red-Black Trees Comp 550.
Chapter 13 Red-Black Trees Lee, Hsiu-Hui Ack: This presentation is based on the lecture slides from Hsu, Lih-Hsing, as well as various materials from the.
2IL50 Data Structures Fall 2015 Lecture 7: Binary Search Trees.
Binary Search Tree Qamar Abbas.
October 3, Algorithms and Data Structures Lecture VII Simonas Šaltenis Nykredit Center for Database Research Aalborg University
Lecture 9 Algorithm Analysis Arne Kutzner Hanyang University / Seoul Korea.
Red Black Tree Essentials Notes from “Introduction to Algorithms”, Cormen et al.
1 Algorithms CSCI 235, Fall 2015 Lecture 22 Binary Search Trees.
Lecture 19. Binary Search Tree 1. Recap Tree is a non linear data structure to present data in hierarchical form. It is also called acyclic data structure.
Red Black Trees. History The concept of a balancing tree was first invented by Adel’son-Vel’skii and Landis in They came up with the AVL tree. In.
Fundamentals of Algorithms MCS - 2 Lecture # 17. Binary Search Trees.
Lecture 91 Data Structures, Algorithms & Complexity Insertion and Deletion in BST GRIFFITH COLLEGE DUBLIN.
Mudasser Naseer 1 1/25/2016 CS 332: Algorithms Lecture # 10 Medians and Order Statistics Structures for Dynamic Sets.
Analysis of Algorithms CS 477/677 Red-Black Trees Instructor: George Bebis (Chapter 14)
1 CSC 211 Data Structures Lecture 25 Dr. Iftikhar Azim Niaz 1.
Sept Red-Black Trees What is a red-black tree? -node color: red or black - nil[T] and black height Subtree rotation Node insertion Node deletion.
Analysis of Algorithms CS 477/677 Instructor: Monica Nicolescu Lecture 10.
Binary Search Trees What is a binary search tree?
Balanced Search Trees Modified from authors’ slides.
Red-Black Trees.
Data Structures Review Session 2
Binary Search Tree.
Analysis of Algorithms
CS 583 Analysis of Algorithms
Lecture 7 Algorithm Analysis
Ch. 12: Binary Search Trees Ming-Te Chi
Elementary Data Structures
Red-Black Trees.
ძებნის ორობითი ხეები BST (Binary Search Trees)
CMSC 341 (Data Structures)
Red Black Tree Essentials
Binary Search Trees (13.1/12.1)
Lecture 7 Algorithm Analysis
Algorithms and Data Structures Lecture VII
Chapter 12: Binary Search Trees
Red Black Tree Essentials
Lecture 7 Algorithm Analysis
Topic 6: Binary Search Tree Data structure Operations
EE Data Structures and Algorithms
Design and Analysis of Algorithms
Analysis of Algorithms CS 477/677
Binary Search Trees Comp 122, Spring 2004.
Chapter 12&13: Binary Search Trees (BSTs)
Red-Black Trees CS302 Data Structures
Presentation transcript:

ძებნის ორობითი ხეები BST (Binary Search Trees) ავტორი: ზაზა გამეზარდაშვილი

ძებნის ორობითი ხეები წარმოადგენენ მონაცემთა სტრუქტურას, რომლის საშუალებითაც ხის სიმაღლის პროპორციულ O(h) დროში სრულდება შემდეგი ოპერაციები: ძებნა; მინიმუმის პოვნა; მაქსიმუმის პოვნა; წინა ელემენტის პოვნა; მომდევნო ელემენტის პოვნა; ელემენტის ჩასმა; ელემენტის წაშლა.

ძებნის ორობითი ხეების წარმოდგენა წარმოადგენს მიმთითებლებით დაკავშირებულ ხეს. root(T) კვანძი არის T ხის სათავე. ყოველი კვანძი შედგება ველებისაგან: გასაღები მარცხენა შვილი: მარცხენა ქვეხის სათავე. მარჯვენა შვილი: მარჯვენა ქვეხის სათავე. p - მშობლის მიმთითებელი. p[root[T]] = NIL

ძებნის ორობითი ხის თვისება ძებნის ორობითი ხის გასაღებები უნდა აკმაყოფილებდნენ შემდეგ პირობას:  y-სათვის x-ის მარცხენა ქვეხიდან, key[y]  key[x].  y-სათვის x-ის მარჯვენა ქვეხიდან key[y]  key[x]. 56 26 200 18 28 190 213 12 24 27

ძებნის ხის შემოვლის ალგორითმები Inorder – 4, 10, 12, 15, 18, 22, 24, 25, 31, 35, 44, 50, 66, 70, 90 Preorder – 25, 15, 10, 4, 12, 22, 18, 24, 50, 35, 31, 44, 70, 66, 90 Postorder – 4, 12, 10, 18, 24, 22, 15, 31, 44, 35, 66, 90, 70, 50, 25

ძებნის ხის შემოვლის ალგორითმები void print_Tree_Preorder(struct node* node) { if (node == NULL) return; printf("%d ", node->data); print_Tree_Preorder (node->left); print_Tree_Preorder (node->right); } void print_Tree_Inorder(struct node* node) { if (node == NULL) return; print_Tree_Inorder (node->left); printf("%d ", node->data); print_Tree_Inorder (node->right); } void print_Tree_Postorder(struct node* node) { if (node == NULL) return; print_Tree_Postorder (node->left); print_Tree_Postorder (node->right); printf("%d ", node->data); }

ელემენტის ძებნა მუშაობის დრო: O(h) Recursive-Tree-Search(x, k) 1. if x = NIL or k = key[x] 2. then return x 3. if k < key[x] 4. then return Tree-Search(left[x], k) 5. else return Tree-Search(right[x], k) 56 26 200 18 28 190 213 12 24 27 Iterative-Tree-Search(x, k) 1. while x  NIL and k  key[x] 2. do if k < key[x] 3. then x  left[x] 4. else x  right[x] 5. return x მუშაობის დრო: O(h)

Min & Max-ის პოვნა ძებნის ორობითი ხის თვისება განაპირობებს: მინიმუმი არის სათავიდან ყველაზე მარცხენა კვანძში. მაქსიმუმი არის სათავიდან ყველაზე მარჯვენა კვანძში. Tree-Minimum(x) Tree-Maximum(x) 1. while left[x]  NIL 1. while right[x]  NIL 2. do x  left[x] 2. do x  right[x] 3. return x 3. return x

წინა და მომდევნო ელემენტების პოვნა x ელემენტის მომდევნო ელემენტი (Successor) არის ისეთი y, რომლის მნიშვნელობაც უმცირესია x-ზე მეტ ელემენტებს შორის. უდიდესი ელემენტის მომდევნო არის NIL. ძებნის დროს განიხილება ორი შემთხვევა: თუ x ელემენტს აქვს არაცარიელი მარჯვენა ქვეხე, მაშინ მისი მომდევნო ელემენტია ამ ქვეხის მინიმუმი. თუ x ელემენტის მარჯვენა ქვეხე ცარიელია: თუკი მარჯვენა ქვეხე ცარიელია, მაშინ ჩვენ ვმოძრაობთ x-დან ზემოთ, ვიდრე არ ვიპოვით წვეროს, რომელიც თავისი მშობლის მარცხენა შვილია. სწორედ ეს მშობელი (თუკი ის არსებობს) წარმოადგენს საძებნ ელემენტს.

მომდევნო ელემენტის პოვნის ფსევდოკოდი Tree-Successor(x) if right[x]  NIL 2. then return Tree-Minimum(right[x]) 3. y  p[x] 4. while y  NIL and x = right[y] 5. do x  y 6. y  p[y] 7. return y 56 26 200 18 28 190 213 12 24 27 წინა ელემენტის პოვნა სიმეტრიულია. მუშაობის დრო: O(h). მუშაობის დრო O(h)-ის ტოლია, რადგან მოძრაობა ხდება ან მხოლოდ ზემოთ, ან მხოლოდ ქვემოთ.

ელემენტის ჩასმა ძებნის ორობით ხეში Tree-Insert(T, z) y  NIL x  root[T] while x  NIL do y  x if key[z] < key[x] then x  left[x] else x  right[x] p[z]  y if y = NIL then root[t]  z else if key[z] < key[y] then left[y]  z else right[y]  z 56 26 200 18 28 190 213 12 24 27 ინიციალიზაცია: O(1). While ციკლი 3-7 სტრიქონებში ეძებს z-ის ჩასასმელ ადგილს. სჭირდება O(h) დრო. 8-13 სტრიქონებში ხდება ელემენტის ჩასმა: O(1)  სულ: O(h) დრო ელემენტის ჩასასმელად.

ელემენტის წაშლა ძებნის ორობით ხეში ელემენტის წაშლისას შესაძლებელია სამი შემთხვევა: ა) z-ს არ გააჩნია შვილები, ამ დროს z-ის წასაშლელად საკმარისია მისი მშობლის შესაბამის მინდორში მოვათავსოთ nil; ბ) z-ს გააჩნია ერთი შვილი, ამ შემთხვევაში z “ამოიშლება” მისი მშობლისა და შვილის პირდაპირი შეერთებით; გ) z-ს გააჩნია ორი შვილი – აქ წვეროს წაშლამდე საჭიროა გარკვეული მოსამზადებელი სამუშაოების ჩატარება: უნდა მოვძებნოთ გასაღების სიდიდის მიხედვით მომდევნო y ელემენტი. მას არ ეყოლება მარცხენა შვილი (ძებნის ორობით ხეში მტკიცდება შემდეგი თვისება: თუ ელემენტს გააჩნია ორი შვილი, მაშინ გასაღების სიდიდის მიხედვით მომდევნო ელემენტს არ გააჩნია მარცხენა შვილი, ხოლო გასაღების სიდიდის მიხედვით წინას – მარჯვენა შვილი). ამის შემდეგ y წვეროს გასაღები და დამატებითი მონაცემები z წვეროს შესაბამის მინდვრებში, ხოლო თავად y წვერო წავშალოთ ზემოთ ნახსენები (ბ) ხერხით.

ელემენტის წაშლა ძებნის ორობით ხეში 15 15 5 16 5 16 3 12 20 3 12 20 z 13 18 23 18 23 ა) 15 15 5 16 5 16 3 13 20 3 z 12 20 23 18 23 13 18 ბ) z z 15 15 15 6 5 5 6 16 16 16 3 12 3 12 3 12 20 20 20 13 10 13 23 10 13 10 23 18 23 18 18 7 7 6 გ) 7

ელემენტის წაშლა ძებნის ორობით ხეში Tree-Delete(T, z) /* Determine which node to splice out: either z or z’s successor. */ if left[z] = NIL or right[z] = NIL then y  z else y  Tree-Successor[z] /* Set x to a non-NIL child of x, or to NIL if y has no children. */ if left[y]  NIL then x  left[y] else x  right[y] /* y is removed from the tree by manipulating pointers of p[y] and x */ if x  NIL then p[x]  p[y] if p[y] = NIL then root[T]  x else if y  left[p[i]] then left[p[y]]  x else right[p[y]]  x 13. /* If z’s successor was spliced out, copy its data into z */ if y  z then key[z]  key[y] copy y’s satellite data into z. return y მუშაობის დრო: O(h)