1 Trees CLRS: chapter 12. 2 A hierarchical combinatorial structure הגדרה רקורסיבית: 1. צומת בודד. זהו גם שורש העץ. 2. אם n הוא צומת ו T 1 ….T K הינם עצים,

Slides:



Advertisements
Similar presentations
1 Data Structures, CS, TAU, RB-Tree1 עץ אדום-שחור  עץ חיפוש בינארי  בכל צומת ביט אינפורמציה נוסף - צבע  עץ “כמעט מאוזן”  (O(log n במקרה גרוע ביותר.
Advertisements

S. Sudarshan Based partly on material from Fawzi Emad & Chau-Wen Tseng
Search Trees: BSTs and B-Trees David Kauchak cs302 Spring 2013.
Binary Search Trees. A binary search tree is a binary tree that keeps the following property: Every element is larger than all elements in its left sub-tree.
Binary Search Trees CSE 331 Section 2 James Daly.
Binary Trees, Binary Search Trees CMPS 2133 Spring 2008.
Data Structures Haim Kaplan and Uri Zwick November 2012 Lecture 5 B-Trees.
InOrder Traversal Algorithm // InOrder traversal algorithm inOrder(TreeNode n) { if (n != null) { inOrder(n.getLeft()); visit(n) inOrder(n.getRight());
Data Structures: Trees i206 Fall 2010 John Chuang Some slides adapted from Marti Hearst, Brian Hayes, or Glenn Brookshear.
Department of Computer Science University of Maryland, College Park
1 Trees. 2 Outline –Tree Structures –Tree Node Level and Path Length –Binary Tree Definition –Binary Tree Nodes –Binary Search Trees.
1 Trees CLRS: chapter A hierarchical combinatorial structure הגדרה רקורסיבית: 1. צומת בודד. זהו גם שורש העץ. 2. אם n הוא צומת ו T 1 ….T K הינם עצים,
CSE 326: Data Structures Lecture #7 Binary Search Trees Alon Halevy Spring Quarter 2001.
1 Binary Search Trees (BSTs). 2 Consider the search operation FindKey (): find an element of a particular key value in a binary tree. This operation takes.
1 ייצוג באמצעות עצים שונים מתוחכם רק לקבוצות גדולות (תקורה בפעולות ובתכנות!!!) עצי חיפוש בינאריים BINARY SEARCH TREES תחום סדור (> < =) תחום איברים גדול.
CS 307 Fundamentals of Computer Science 1 Data Structures Review Session 2 Ramakrishna, PhD student. Grading Assistant for this course.
Data Structures, CS, TAU, RB-Tree 1 עץ אדום-שחור - עץ חיפוש בינארי - בכל צומת ביט אינפורמציה נוסף - צבע « עץ “ כמעט מאוזן ” « (O(log n במקרה גרוע ביותר.
עצים בינאריים - תזכורת דרגת צומת שורש עלה צומת פנימי מרחק בין 2 צמתים
1 - גמישות בבינאריות גמישות בעומק - עץ חיפוש בינארי: ממוצע O(log n) גרוע ביותר (O(n - היינו רוצים לשמור את העץ מאוזן תמיד Data Structures, CS, TAU
1 BST Trees A binary search tree is a binary tree in which every node satisfies the following: the key of every node in the left subtree is.
Binary Trees Chapter 6.
Version TCSS 342, Winter 2006 Lecture Notes Trees Binary Trees Binary Search Trees.
CSE373: Data Structures & Algorithms Lecture 6: Binary Search Trees Lauren Milne Summer
Design and Analysis of Algorithms Binary search trees Haidong Xue Summer 2012, at GSU.
1 Search Trees - Motivation Assume you would like to store several (key, value) pairs in a data structure that would support the following operations efficiently.
CSCE 3110 Data Structures & Algorithm Analysis Binary Search Trees Reading: Chap. 4 (4.3) Weiss.
AVL Trees Amanuel Lemma CS252 Algoithms Dec. 14, 2000.
Tree (new ADT) Terminology:  A tree is a collection of elements (nodes)  Each node may have 0 or more successors (called children)  How many does a.
Binary Trees, Binary Search Trees RIZWAN REHMAN CENTRE FOR COMPUTER STUDIES DIBRUGARH UNIVERSITY.
D ESIGN & A NALYSIS OF A LGORITHM 05 – N - ARY T REE & B INARY T REE Informatics Department Parahyangan Catholic University.
Binary SearchTrees [CLRS] – Chap 12. What is a binary tree ? A binary tree is a linked data structure in which each node is an object that contains following.
Search Trees. Binary Search Tree (§10.1) A binary search tree is a binary tree storing keys (or key-element pairs) at its internal nodes and satisfying.
Binary Search Trees (10.1) CSE 2011 Winter November 2015.
CS 361 – Chapter 3 Sorted dictionary ADT Implementation –Sorted array –Binary search tree.
 Trees Data Structures Trees Data Structures  Trees Trees  Binary Search Trees Binary Search Trees  Binary Tree Implementation Binary Tree Implementation.
Data Structures Haim Kaplan and Uri Zwick November 2012 Lecture 3 Dynamic Sets / Dictionaries Binary Search Trees.
Lec 15 Oct 18 Binary Search Trees (Chapter 5 of text)
Preview  Graph  Tree Binary Tree Binary Search Tree Binary Search Tree Property Binary Search Tree functions  In-order walk  Pre-order walk  Post-order.
October 9, Algorithms and Data Structures Lecture VIII Simonas Šaltenis Aalborg University
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.
1 Binary Trees and Binary Search Trees Based on Dale & Co: Object-Oriented Data Structures using C++ (graphics)
1 Trees CLRS: chapter A hierarchical combinatorial structure הגדרה רקורסיבית: 1. צומת בודד. זהו גם שורש העץ. 2. אם n הוא צומת ו T 1 ….T K הינם עצים,
Binary Search Trees (BST)
Data Structures Hanoch Levi and Uri Zwick March 2011 Lecture 3 Dynamic Sets / Dictionaries Binary Search Trees.
CSE 2331/5331 Topic 8: Binary Search Tree Data structure Operations.
1 CSC 211 Data Structures Lecture 25 Dr. Iftikhar Azim Niaz 1.
CSE373: Data Structures & Algorithms Lecture 6: Binary Search Trees Linda Shapiro Winter 2015.
Lecture 9 Binary Trees Trees General Definition Terminology
Trees CSIT 402 Data Structures II 1. 2 Why Do We Need Trees? Lists, Stacks, and Queues are linear relationships Information often contains hierarchical.
Question 4 Tutorial 8. Part A Insert 20, 10, 15, 5,7, 30, 25, 18, 37, 12 and 40 in sequence into an empty binary tree
CSC317 1 Binary Search Trees (binary because branches either to left or to right) Operations: search min max predecessor successor. Costs? Time O(h) with.
CS6045: Advanced Algorithms Data Structures. Dynamic Sets Next few lectures will focus on data structures rather than straight algorithms In particular,
Nov 2, 2001CSE 373, Autumn Hash Table example marking deleted items + choice of table size.
Balancing Binary Search Trees. Balanced Binary Search Trees A BST is perfectly balanced if, for every node, the difference between the number of nodes.
Binary search tree. Removing a node
CSIT 402 Data Structures II
Design & Analysis of Algorithm n-ary Tree & Binary Tree
Lecture 22 Binary Search Trees Chapter 10 of textbook
Ch. 12: Binary Search Trees Ming-Te Chi
תירגול 14: מבני נתונים דינאמיים
מבני נתונים עצים קרן כליף.
Lec 12 March 9, 11 Mid-term # 1 (March 21?)
Ch. 12: Binary Search Trees Ming-Te Chi
Chapter 12: Binary Search Trees
CS6045: Advanced Algorithms
Topic 6: Binary Search Tree Data structure Operations
BST Insert To insert an element, we essentially do a find( ). When we reach a NULL pointer, we create a new node there. void BST::insert(const Comp &
Binary SearchTrees [CLRS] – Chap 12.
Algorithms, CSCI 235, Spring 2019 Lecture 22—Red Black Trees
Algorithms CSCI 235, Spring 2019 Lecture 21 Binary Search Trees
Presentation transcript:

1 Trees CLRS: chapter 12

2 A hierarchical combinatorial structure הגדרה רקורסיבית: 1. צומת בודד. זהו גם שורש העץ. 2. אם n הוא צומת ו T 1 ….T K הינם עצים, ניתן לבנות עץ חדש שבו n השורש ו T 1 ….T K הינם “תתי עצים”. T1T1 TKTK n T1T1 TKTK n... מושגים:

3 r c1c1 c2c2 c3c3 s 1.1 s 1.2 s 1.3 s 2.1 s 2.2 s 3.1 מושגים: r - הורה/Parent (אבא) של c1, c2, c3 c 1, c 2 - ילדים/children של r s צאצא/Descendant (לא ישיר) של r r,c 1,s מסלול/Path (אם כ”א הורה של הקודם) אורך המסלול = מס’ הקשתות = מס’ הצמתים (פחות אחד) צומת ללא ילדים = עלה/Leaf r - אב קדמון/Ancestor של s 3.1 Terminology

גובה העץ - אורך המסלול הארוך ביותר מהשורש לעלה (height) עומק צומת - אורך המסלול מהצומת לשורש (depth) Ordered tree יש משמעות לסדר הילדים. מסדרים משמאל לימין. a bC a Cb אם הסדר לא חשוב - עץ לא מסודר (unordered tree)

- עץ ריק או לכל צומת יש תת קבוצה של {ילד ימני, ילד שמאלי} דוגמא: עצים בינאריים Full : each internal node always has both children

6 The dictionary problem Maintain (distinct) items with keys from a totally ordered universe subject to the following operations

7 The ADT Insert(x,D) Delete(x,D) Find(x,D): Returns a pointer to x if x ∊ D, and a pointer to the successor or predecessor of x if x is not in D

8 The ADT successor(x,D) predecessor(x,D) Min(D) Max(D)

9 The ADT catenate(D 1,D 2 ) : Assume all items in D 1 are smaller than all items in D 2 split(x,D) : Separate to D 1, D 2, D 1 with all items smaller than x and D 2 with all items greater than x

10 Reminder from “mavo” We have seen solutions using unordered lists and ordered lists. Worst case running time O(n) We also defined Binary Search Trees (BST)

11 Binary search trees A representation of a set with keys from a totally ordered universe We put each element in a node of a binary tree subject to:

12 BST If y is in the left subtree of x then y.key < x.key If y is in the right subtree of x then y.key > x.key

13 BST x.right x.key x.left x x.parent

14 Y ← null z ← T.root While z ≠ null do y ← z if x = z.key return z if x < z.key then z ← z.left else z ← z.right return y Find(x,T)

15 Y ← null z ← T.root While z ≠ null do y ← z if x = z.key return z if x < z.key then z ← z.left else z ← z.right return y Find(5,T) z

16 Y ← null z ← T.root While z ≠ null do y ← z if x = z.key return z if x < z.key then z ← z.left else z ← z.right return y Find(5,T) z y

17 Y ← null z ← T.root While z ≠ null do y ← z if x = z.key return z if x < z.key then z ← z.left else z ← z.right return y Find(5,T) z y

18 Y ← null z ← T.root While z ≠ null do y ← z if x = z.key return z if x < z.key then z ← z.left else z ← z.right return y Find(5,T) z y

19 Y ← null z ← T.root While z ≠ null do y ← z if x = z.key return z if x < z.key then z ← z.left else z ← z.right return y Find(5,T) z y

20 Y ← null z ← T.root While z ≠ null do y ← z if x = z.key return z if x < z.key then z ← z.left else z ← z.right return y Find(5,T) z y

21 Y ← null z ← T.root While z ≠ null do y ← z if x = z.key return z if x < z.key then z ← z.left else z ← z.right return y Find(6,T) z y

22 Y ← null z ← T.root While z ≠ null do y ← z if x = z.key return z if x < z.key then z ← z.left else z ← z.right return y Find(6,T) y z=null

Min(T) 6 Min(T.root) min(z): While (z.left ≠ null) do z ← z.left return (z) 1.5

24 n ← new node n.key ← x n.left ← n.right ← null y ← find(x,T) n.parent ← y if x < y.key then y.left ← n else y.right ← n Insert(x,T)

Insert(6,T) n ← new node n.key ← x n.left ← n.right ← null y ← find(x,T) n.parent ← y if x < y.key then y.left ← n else y.right ← n

Insert(6,T) 6 y n ← new node n.key ← x n.left ← n.right ← null y ← find(x,T) n.parent ← y if x < y.key then y.left ← n else y.right ← n

Delete(6,T) 6

Delete(6,T)

Delete(8,T)

Delete(8,T)

Delete(2,T) 6 Switch 5 and 2 and delete the node containing 5

Delete(2,T) 6 Switch 5 and 2 and delete the node containing 5

33 delete(x,T) q ← find(x,T) If q.left = null or q.right = null then z ← q else z ← min(q.right) q.key ← z.key q

34 delete(x,T) q ← find(x,T) If q.left = null or q.right = null then z ← q else z ← min(q.right) q.key ← z.key q z

35 delete(x,T) q ← find(x,T) If q.left = null or q.right = null then z ← q else z ← min(q.right) q.key ← z.key q

36 delete(x,T) q ← find(x,T) If q.left = null or q.right = null then z ← q else z ← min(q.right) q.key ← z.key q z Find z that does not have 2 children! Z is deletion point

37 delete(x,T) q ← find(x,T) If q.left = null or q.right = null then z ← q else z ← min(q.right) q.key ← z.key If z.left ≠ null then y ← z.left else y ← z.right z Z is deletion point

38 delete(x,T) q ← find(x,T) If q.left = null or q.right = null then z ← q else z ← min(q.right) q.key ← z.key If z.left ≠ null then y ← z.left else y ← z.right z y

39 delete(x,T) q ← find(x,T) If q.left = null or q.right = null then z ← q else z ← min(q.right) q.key ← z.key If z.left ≠ null then y ← z.left else y ← z.right If y ≠ null then y.parent ← z.parent z y

40 delete(x,T) q ← find(x,T) If q.left = null or q.right = null then z ← q else z ← min(q.right) q.key ← z.key If z.left ≠ null then y ← z.left else y ← z.right If y ≠ null then y.parent ← z.parent p = z.parent If z = p.left then p.left = y else p.right = y z y p

41 delete(x,T) q ← find(x,T) If q.left = null or q.right = null then z ← q else z ← min(q.right) q.key ← z.key If z.left ≠ null then y ← z.left else y ← z.right If y ≠ null then y.parent ← z.parent p = z.parent If z = p.left then p.left = y else p.right = y z y p

42 delete(x,T) q ← find(x,T) If q.left = null or q.right = null then z ← q else z ← min(q.right) q.key ← z.key If z.left ≠ null then y ← z.left else y ← z.right If y ≠ null then y.parent ← z.parent p = z.parent If z = p.left then p.left = y else p.right = y

43 Variation: Items only at the leaves Keep elements only at the leaves Each internal node contains a number to direct the search Implementation is simpler (e.g. delete) Costs space

44 Analysis Each operation takes O(h) time, where h is the height of the tree In general h may be as large as n Want to keep the tree with small h

45 Balance  h = O(log n) How do we keep the tree balanced through insertions and deletions ?

46 1) O(n) : Worst Case 2) כמה זמן לוקח בממוצע להכניס n איברים אקראיים? עץ מושלם: עץ קווי: על איבר לוקח לכל היותר log n O(n) לצומת ממוצע: צריך להניח הנחות - רק INSERT - כל הסדרים שווי הסתברות (סדרים בין n אלמנטים) Data Structures, CS, TAU אנליזת זמן של עץ חיפוש בינארי

47 - רוצים לחשב אורך מסלול ממוצע: קח את האיבר הראשון (a) ההסתברות שi איברים קטנים ממנו היא a iN-i-1 יהי L(n) אורך ממוצע של מסלול בעץ בגודל n (אורך מסלול כאן = מס’ צמתים במסלול) אזי, אם i איברים קטנים מ- a, האורך הממוצע הוא: שמאל ימין מרכז (j=n-i-1) Data Structures, CS, TAU - 5.7

48 נוריד את ההתניה על i: הצב: נראה באינדוקציה: הוכחה: Data Structures, CS, TAU - 5.8

49 Data Structures, CS, TAU - 5.9

50 Balance  h = O(log n) How do we keep the tree balanced through insertions and deletions ? - Next chapter!

successor(x,T) 6 1.5

52 successor(x,T) If x has a right child it’s the minimum in the subtree of x.right x

successor(6,T) 6 1.5

54 successor(x,T) If x.right is null, go up until the lowest ancestor such that x is at its left subtree x

55 successor(x,T) If x.right ≠ null then min(x.right) y ← x.parent While y≠null and x=y.right do x ← y y ← y.parent return(y) x

56 successor(x,T) If x.right ≠ null then min(x.right) y ← x.parent While y≠null and x=y.right do x ← y y ← y.parent return(y) y x

57 successor(x,T) If x.right ≠ null then min(x.right) y ← x.parent While y≠null and x=y.right do x ← y y ← y.parent return(y) y x

58 successor(x,T) If x.right ≠ null then min(x.right) y ← x.parent While y≠null and x=y.right do x ← y y ← y.parent return(y) y x