ორობითი ხეები. გროვა. სორტირება გროვით.

Slides:



Advertisements
Similar presentations
Heaps, Heap Sort, and Priority Queues. Sorting III / Slide 2 Background: Binary Trees * Has a root at the topmost level * Each node has zero, one or two.
Advertisements

1 Chapter 10 Trees. Tree Definition 1. A tree is a connected undirected graph with no simple circuits. Theorem 1. An undirected graph is a tree if and.
Heaps, Heap Sort, and Priority Queues
Heapsort.
Priority Queue (Heap) & Heapsort COMP171 Fall 2006 Lecture 11 & 12.
CMPT 225 Priority Queues and Heaps. Priority Queues Items in a priority queue have a priority The priority is usually numerical value Could be lowest.
Chapter 4: Trees General Tree Concepts Binary Trees Lydia Sinapova, Simpson College Mark Allen Weiss: Data Structures and Algorithm Analysis in Java.
Heaps and heapsort COMP171 Fall Sorting III / Slide 2 Motivating Example 3 jobs have been submitted to a printer in the order A, B, C. Sizes: Job.
1 Trees. 2 Outline –Tree Structures –Tree Node Level and Path Length –Binary Tree Definition –Binary Tree Nodes –Binary Search Trees.
A tree is a simple graph satisfying: if v and w are vertices and there is a path from v to w, it is a unique simple path. a b c a b c.
Rooted Trees. More definitions parent of d child of c sibling of d ancestor of d descendants of g leaf internal vertex subtree root.
COMP 171 Data Structures and Algorithms Tutorial 5 Heapsort.
1.1 Data Structure and Algorithm Lecture 12 Binary Search Trees Topics Reference: Introduction to Algorithm by Cormen Chapter 13: Binary Search Trees.
AVL Trees / Slide 1 Balanced Binary Search Tree  Worst case height of binary search tree: N-1  Insertion, deletion can be O(N) in the worst case  We.
Data Structures, Spring 2006 © L. Joskowicz 1 Data Structures – LECTURE Binary search trees Motivation Operations on binary search trees: –Search –Minimum,
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:
2IL50 Data Structures Spring 2015 Lecture 3: Heaps.
1 CSE 1342 Programming Concepts Trees. 2 Basic Terminology Trees are made up of nodes and edges. A tree has a single node known as a root. –The root is.
CISC220 Fall 2009 James Atlas Lecture 13: Trees. Skip Lists.
COSC 1030 Lecture 9 Binary Trees. Topics Basic Concept and Terminology Applications of Binary Tree Complete Tree Representation Traversing Binary Trees.
2IL50 Data Structures Fall 2015 Lecture 3: Heaps.
Red Black Tree Essentials Notes from “Introduction to Algorithms”, Cormen et al.
D. ChristozovCOS 221 Intro to CS II AVL Trees 1 AVL Trees: Balanced BST Binary Search Trees Performance Height Balanced Trees Rotation AVL: insert, delete.
1 Algorithms CSCI 235, Fall 2015 Lecture 22 Binary Search Trees.
CSC 172 DATA STRUCTURES. LISTS We have seen lists: public class Node { Object data; Node next; } 
Min Chen School of Computer Science and Engineering Seoul National University Data Structure: Chapter 6.
AVL trees1 AVL Trees Height of a node : The height of a leaf is 1. The height of a null pointer is zero. The height of an internal node is the maximum.
Lecture 91 Data Structures, Algorithms & Complexity Insertion and Deletion in BST GRIFFITH COLLEGE DUBLIN.
1 Heap Sort. A Heap is a Binary Tree Height of tree = longest path from root to leaf =  (lgn) A heap is a binary tree satisfying the heap condition:
Data Structures Lakshmish Ramaswamy. Tree Hierarchical data structure Several real-world systems have hierarchical concepts –Physical and biological systems.
Heapsort A minimalist's approach Jeff Chastine. Heapsort Like M ERGE S ORT, it runs in O(n lg n) Unlike M ERGE S ORT, it sorts in place Based off of a.
1 Lecture 14: Trees & Insertion Sort CompSci 105 SS 2006 Principles of Computer Science.
1 Algorithms CSCI 235, Fall 2015 Lecture 13 Heap Sort.
Heaps, Heap Sort, and Priority Queues. Background: Binary Trees * Has a root at the topmost level * Each node has zero, one or two children * A node that.
Lec 13 Oct 17, 2011 AVL tree – height-balanced tree Other options:
BCA-II Data Structure Using C
CISC220 Fall 2009 James Atlas Lecture 13: Binary Trees.
AVL Tree Mohammad Asad Abbasi Lecture 12
AVL Tree.
Binary Trees "A tree may grow a thousand feet tall, but its leaves will return to its roots." -Chinese Proverb.
Heap Sort Example Qamar Abbas.
Heaps, Heap Sort, and Priority Queues
Priority Queues Linked-list Insert Æ Æ head head
TREE DATA STRUCTURE Data Structure 21-Sep-18 Tree
Binary Trees, Binary Search Trees
CS223 Advanced Data Structures and Algorithms
Heaps, Heap Sort, and Priority Queues
slides created by Alyssa Harding
תרגול 9 Heaps Lempel-Ziv.
CS200: Algorithm Analysis
Search Sorted Array: Binary Search Linked List: Linear Search
Trees.
Chapter 14: Augmenting Data Structures
Red Black Tree Essentials
Trees Definitions Implementation Traversals K-ary Trees
Data Structures & Algorithms
Binary Search Trees (13.1/12.1)
Design and Analysis of Algorithms
Lecture 36 Section 12.2 Mon, Apr 23, 2007
Red Black Tree Essentials
Binary Trees, Binary Search Trees
Trees Construction Paths Height/Depth Ordered Trees Traversals
HEAPS.
AVL-Trees.
Lecture 10 Oct 1, 2012 Complete BST deletion Height-balanced BST
Tree and its terminologies
AVL-Trees (Part 2).
General Tree Concepts Binary Trees
Algorithms CSCI 235, Spring 2019 Lecture 14 Heap Sort Read: Ch. 6
Binary Trees, Binary Search Trees
Presentation transcript:

ორობითი ხეები. გროვა. სორტირება გროვით. ორობითი ხეები. გროვა. სორტირება გროვით.

რეკურსიული განსაზღვრება ხეები რეკურსიული განსაზღვრება ჩანაწერს, რომელიც შეიცავს კონკრეტული საინფორმაციო სტრუქტურის აღწერას, მნიშვნელობას ან მდგომარეობას, ვუწოდოთ კვანძი (node). T ხე წარმოადგენს კვანძების სიმრავლეს, სადაც T შეიძლება იყოს ცარიელი თუ ცარიელი არაა, მაშინ T შეიცავს: განსაკუთრებულ r კვანძს - ხის სათავეს (root, ფესვი, ძირი), ცარიელ ან არაცარიელ ქვეხეებს T1, T2, ...., Tk

ხეთა თვისებები ხე წარმოადგენს წვეროებისა და წიბოების G=(V,E) სიმრავლეს, სადაც G-ს ნებისმიერი ორი წვეროსათვის არსებობს მათი შემაერთებელი ერთადერთი მარტივი გზა; G ბმულია, მაგრამ კარგავს ბმულობას, თუ გამოვაკლებთ ნებისმიერ წიბოს; E=V-1; G არ შეიცავს ციკლებს ანუ აციკლურია; G აციკლურია, მაგრამ ნებისმიერი წიბოს დამატებით მასში ჩნდება ციკლი.

ტერმინოლოგია მშობელი და შვილი (Parent, Child) ყველა კვანძს ხის სათავის გარდა ჰყავს მხოლოდ 1 მშობელი;  ყველა კვანძს ჰყავს 0, 1 ან რამდენიმე შვილი; ფოთოლი (Leaf ) კვანძი შვილების გარეშე; ძმა (დედმამიშვილი, Sibling) ერთი მშობლის შვილები; გზა და მისი სიგრძე (Path, Length) ორ კვანძს შორის არსებული წიბოების მიმდევრობა წარმოადგენს გზას ამ ორ კვანძს შორის, ხოლო წიბოების რაოდენობა - ამ გზის სიგრძეს; კვანძის სიღრმე (Depth) გზის სიგრძე სათავიდან კვანძამდე; კვანძის სიმაღლე (Height) უშორესი გზის სიგრძე კვანძიდან ფოთლამდე ნებისმიერი ფოთლის სიმაღლე 0-ის ტოლია ხის სიმაღლე = სათავის სიმაღლე = ყველაზე “ღრმა” ფოთლის სიღრმე წინაპარი და შთამომავალი (Ancestor and descendant) თუ არსებობს გზა n1-დან n2-მდე სათავის გავლის გარეშე და n1-ის სიღრმე ნაკლებია n2-ის სიღრმეზე, მაშინ n1 არის n2-ის წინაპარი, ხოლო n2 – n1-ის შთამომავალი.

ორობითი ხეები გააჩნია სათავე, სიმაღლის ყველაზე მეტი დონით; ყოველ კვანძს აქვს 0, 1 ან 2 შვილი; კვანძები შვილების გარეშე წარმოადგენენ ფოთლებს; X კვანძის სტრუქტურაში განისაზღვრება მისი მარცხენა შვილი left(x), მარჯვენა შვილი right(x) და მშობელი parent(x). root Parent(x) x leaf leaf left(x) right(x) leaf

ორობითი ხის სიმაღლე ორობითი ხის სიმაღლედ ითვლება წიბოების რაოდენობა სათავიდან წვერომდე არსებულ უგრძელეს გზაში. Height = 4

სრული ორობითი ხე height no. of nodes 1 1 2 2 4 3 8 d 2d სრული ორობითი ხე ეწოდება ხეს, რომელშიც ყველა კვანძს აქვს 0 (ფოთოლი) ან 2 შვილი; ყველა ფოთოლი ერთნაირი სიღრმე აქვს დამოკიდებულება კვანძებსა და ხის სიმაღლეს შორის N კვანძისაგან შედგენილი სრული ორობითი ხის სიმაღლეა O(logN); d სიმაღლის მქონე სრული ორობითი ხე შეიცავს 2d+1-1 კვანძს. height no. of nodes 1 1 2 2 4 3 8 d 2d

გროვა გროვა წარმოადგენს ბალანსირებულ ორობით ხეს, ანუ არასრულად შევსებული შეიძლება იყოს მხოლოდ ყველაზე ქვედა დონე, ამასთან ამ შეუვსებელ დონეზე კვანძები მიჯრით მარცხნივ არიან განლაგებული; ნებისმიერი კვანძისათვის მისი მნიშვნელობა ნაკლებია მშობლის მნიშვნელობაზე (ასეთ გროვებს მაქს-გროვებს უწოდებენ, არსებობს მინ-გროვაც, სადაც ნებისმიერ კვანძში მისი მნიშვნელობა მშობლის მნიშვნელობაზე ნაკლებია). n-2 n-1 n ბალანსირებული ბალანსირებული არაბალანსირებული 1 4 2 5 2 5 4 3 6 1 3 6 მარცხნივ დალაგებული გროვა არ არის გროვა

გროვის თვისებები გროვა ეფექტურად ახორციელებს შემდეგ ოპერაციებს ელემენტის ჩამატება ხდება O(logN) დროში; განსაზღვრავს მაქსიმუმს (ან მინიმუმს) O(1) დროში; ახორციელებს მაქსიმუმის (ან მინიმუმის) წაშლას O(log N) დროში.

გროვის წარმოდგენა მასივით მასივის ნებისმიერი i-ური ელემენტისათვის მარცხენა შვილის ინდექსია 2i მარჯვენა შვილის ინდექსია 2i+1 მშობლის ინდექსია floor(i/2) - (i/2)-ის მთელი ნაწილი. ასეთი წარმოდგენის უარყოფითი მხარე ის არის, რომ წინასწარ უნდა აღვწეროთ მაქსიმალური ზომის მასივი, ხოლო დამუშავებას მასივში უამრავი ცარიელი ადგილი შეიძლება დარჩეს. 53 44 25 15 21 13 18 3 12 5 7 53 44 25 15 21 13 18 3 12 5 7 0 1 2 3 4 5 6 7 8 9 10 11

გროვის მთავარი თვისების აღდგენა 12 12 12 8 3 8 12 8 14 წითელი კვანძი აკმაყოფილებს გროვის მთავარ თვისებას წითელი კვანძი არ აკმაყოფილებს გროვის მთავარ თვისებას 12 14 8 14 8 12 თუ კვანძი არღვევს გროვის მთავარ თვისებას, მაშინ ის უცვლის ადგილს თავისი შვილებიდან უდიდესს.

გროვის აგება თუ ხე შეიცავს მხოლოდ ერთ კვანძს, ის გროვად ითვლება; გროვის ასაგებად ვამატებთ თითო კვანძს შემდეგი წესით: ვუმატებთ კვანძს ყველაზე ქვედა დონეზე უკიდურეს მარცხენა თავისუფალ ადგილას; თუ ქვედა დონე შევსებულია, შევქმნათ ახალი დონე; თუ ახლად დამატებული კვანძი არღვევს გროვის მთავარ თვისებას, მას რეკურსიულად ვუცვლით ადგილს მშობელთან; მაგალითი: ვუმატებთ ახალ კვანძს აქ ვუმატებთ ახალ კვანძს აქ

გროვის აგება 8 10 5 12 1 2 3 4 12 10 5 8 14 12 14 5 8 10 14 12 5 8 10

სათავის წაშლა 19 14 18 22 3 21 11 9 15 25 17 19 14 18 22 3 21 9 15 17 11 გროვის შესანარჩუნებლად წაშლილი წვეროს ნაცვლად იქ მოვათავსოთ გროვის ბოლო ელემენტი და აღვადგინოთ გროვის მთავარი თვისება.

გროვის აღდგენა სათავის წაშლის შემდეგ 19 14 18 21 3 11 9 15 17 22 ფოთლის პოზიციაში მოხვედრამდე, 11 გაუცვლის ადგილს 22-ს, 22-ს და 21-ს.

მასივის გარდაქმნა გროვად გავყოთ მასივი ორ ნაწილად, პირველი ნახევრის თითოეული წევრი განვიხილოთ მარჯვნიდან მარცხნივ და ის რეკურსიულად “ჩავძიროთ” ხის ქვედა დონეებზე შესაბამის ადგილამდე. 21 3 17 11 22 14 9 18 25 15 19 1 2 3 4 5 6 7 8 9 10 11 12 13 21 3 17 11 22 14 9 18 25 15 19 1 2 3 4 5 6 7 8 9 10 11 12 13 21 3 17 11 22 19 9 18 14 25 15 1 2 3 4 5 6 7 8 9 10 11 12 13 21 3 17 11 25 19 9 18 14 22 15 1 2 3 4 5 6 7 8 9 10 11 12 13 21 3 17 18 25 19 9 11 14 22 15 1 2 3 4 5 6 7 8 9 10 11 12 13

1 2 3 4 5 6 7 8 9 10 11 12 13 21 3 19 18 25 17 9 11 14 22 15 21 25 19 18 22 17 9 11 14 3 15 1 2 3 4 5 6 7 8 9 10 11 12 13 1 2 3 4 5 6 7 8 9 10 11 12 13 25 22 19 18 22 17 9 11 14 3 21 15 14 25 22 19 18 22 17 9 11 14 3 21 15 14

ასიმპტოტიკა

გროვით სორტირება ალგორითმი: გროვით სორტირება ყოველთვის O(n log n)-ში მუშაობს ჩქარი სორტირება საშუალოდ O(n log n)-ში მუშაობს, მაგრამ უარეს შემთხვევაში O(n2) დროს ხარჯავს; საზოგადოდ ჩქარი სორტირება სწრაფია გროვით სორტირებაზე, მაგრამ ეს უკანასკნელი უარეს შემთხვევაშიც O(n log n)-ში მუშაობს და უფრო სანდოა კრიტიკული დროის მქონე აპლიკაციებში. ალგორითმი: ავაგოთ გროვა; გროვის სათავეში მყოფ ელემენტს (მაქსიმუმს) გავუცვალოთ ადგილი გროვის ბოლო ელემენტთან და გროვა ერთით შევამციროთ; აღვადგინოთ გროვის ძირიტადი თვისება და გროვის სათავეში მოხვედრილი ელემენტი “ჩავძიროთ” შესაბამის ადგილამდე; გავიმეოროთ წინა ორი მოქმედება ყველა ელემენტისათვის.

25 22 19 18 22 17 9 1 2 3 4 5 6 7 25 22 19 18 22 17 9 22 19 18 9 17 25 1 2 3 4 5 6 7

22 18 19 17 9 25 1 2 3 4 5 6 7 19 18 9 22 22 25 17 1 2 3 4 5 6 7 19 18 9 17 22 22 25

18 17 9 19 22 25 1 2 3 4 5 6 7 17 9 18 19 22 22 25 1 2 3 4 5 6 7 17 9 18 19 22 22 25 1 2 3 4 5 6 7 9 17 18 19 22 22 25

ანალიზი გროვის აგებისათვის საჭიროა (n/2)*O(log n); წინა ორი ოპერაცია n-ჯერ უნდა გავიმეოროთ, ამიტომ ალგორითმის მუშაობის საერთო დრო იქნება n*O(log n), ანუ O(n log n)

void Heapify(int A[],int i,int HeapSize){ int left=2*i, right=2*i+1; int largest; if ((left <= HeapSize) && (A[left] > A[i])) largest = left; else largest = i; if ((right <= HeapSize) && (A[right] > A[largest])) largest = right; if (largest != i) { Swap(&A[i],&A[largest]); Heapify(A,largest,HeapSize); } void HeapSort(int A[], int n){ int i, HeapSize = n; for (i= HeapSize/2; i >= 1; i--) Heapify(A,i,HeapSize); for (i=n; i>=2; i--) { Swap(&A[i],&A[1]); HeapSize--; Heapify(A,1,HeapSize);

დასასრული