Click here to load reader

עצים ועצי חיפוש

  • View
    31

  • Download
    1

Embed Size (px)

DESCRIPTION

Lecture3 of Geiger & Itai’s slide brochure www.cs.technion.ac.il/~dang/courseDS. עצים ועצי חיפוש. חומר קריאה לשיעור זה. Chapter 5.5– Trees (91 – 97) Chapter 13– Binary Search Trees (244 – 262).  Geiger & Itai, 2001. אברהם. יצחק. ישמאל. עשו. יעקב. 12 בנים. עוד 11בנים ובת. יוסף. מנשה. - PowerPoint PPT Presentation

Text of עצים ועצי חיפוש

introduction

Chapter 5.5 Trees (91 97)Chapter 13 Binary Search Trees (244 262) Lecture3 of Geiger & Itais slide brochurewww.cs.technion.ac.il/~dang/courseDSGeiger & Itai, 2001trees1cs,Technion21. 12 11 trees2cs,Technion32. 2010 / 86-+*53+(5+3) * ((20/10) + (8-6))(5+3) ((20/10) + (8-6))(20/10) (8-6)trees3cs,Technion43. ()" ..." " 856... 934 1 2 1 2

4. "trees45 (Directed Graphs) (V,E) V .E V X VV = a,b,c,d,eE = (a,b),(a,d),(b,c),(b,e),(c,c),(d,c),(d,e),(e,a),(e,c) n = |V| m = |E| . : n = 5 m = 9, .abdectrees5cs,Technion6 - (Undirected Graphs) - (V,E) V E -E V. " (i,j) ( {i,j}).V = a,b,c,d,eE = (a,b), (a,d), (a,d) ,(b,c),(b,e),(c,c),(d,c),(d,e),(e,c) n = |V| m = |E| . : n = 5 m = 9, . m - n2 .abdectrees67 (Directed Graphs)V = a,b,c,d,eE = (a,b),(a,d),(b,c),(b,e),(c,c),(d,c),(d,e),(e,a),(e,c) () () (V,E) (v1,v2,..,vk) , (vi,vi+1) -E. () v1=vk (, (a,d,e,a)). G - -G -G . :abdecabdectrees7cs,Technion8 v u u v. u v v u. f bb e ( ) .bcahidfjkge .trees8cs,Technion9 v u u - v. u v v u.- G v v v, - G. v v. .g ab - h g 3 . a 2.h . .bcahidfjkgetrees9cs,Technion10 v v ( ). v -v v (). . 2 2bcahidfjkge: . .bcahidfjkgetrees10cs,Technion11 ( ).bcahidfjkgebbcahiefjkgdb .treescs,Technion12 : / . : 1. ( ), 2. : , - , - . trees12cs,Technion13 (full) : 2 . (complete) : . : (" ").trees13cs,Technion14 n , L , h: i:ni = 2i : L= nh= 2h :

4. h = log2(n+1) - 1 :5. n - L = 2h - 1 = L - 1 :0123trees14cs,Technion15 "" i - 2i i - 2 i +1 i - i /2

a1b2c3d4e5f6g7h8i9j10k11l12mno014131515810911121314bcahijkdelmnofg4567312 !trees15cs,Technion16 typedef struct node { DATA_Type value; struct node *left, *right; } NODE;valueleftright :bhikdebekdihb1d2e3h4i5f6k70trees16cs,Technion17 preorder a b d e c f h g ipostorder ( ) d e b h f i g c ainorder d b e a f h c i gbcadehifgbcadehifgbcadehifgbcadehifgbcadehifgbcadehifgtrees17cs,Technion18 postorder ( ). 5 3 + 20 10 / 8 6 - + *postfixinorder () (5+3)*((20/10)+(8-6))++*53108/-620infix postfix " ukasievicz postfix : postfix !trees18cs,Technion19 postfix 553882820 postfix . : () - . .5 3 + 20 10 / 8 6 - + * :postfix8286822843282010828trees19cs,Technion20 postordervoid postorder (NODE *T){if (T = = NULL) return;else { postorder( T left); /* 1*/ postorder( T right); /* 2*/ visit; /* 3*/ return;}} I: . II: ( ). : " #2 #3 inorder.valueleftright : ""trees2021 int height (NODE *T){ int L,R; if (T = = NULL) return 1 else { L = height(T left); R = height(T right); return 1 + max(L,R) ; } } ( postorder):trees21cs,Technion22 d :child[0]child[d-1]child[1]value(s)befadgkichjtrees22cs,Technion23 " :first-childnext-brothervalue(s)abcdefhijkg

trees23cs,Technion24 ?abcdefhijkghnew d horiginal, d .

trees24cs,Technion25 (Dictionary):: . create(D) : - D x NULL. find(D,x) : - D x.insert(D,x,info) : - D x.delete(D,x) (, ). (") . -U. : .: x U. x (").trees25cs,Technion26, U ( ):: -D . : D - x. : O(log n) ( ) n . : . next(D,x)min(D) + + = trees26cs,Technion27 xzy

. ( ). : x, -x - x. trees27cs,Technion284562204201451561221202010070358837633060find(70,T)Tfind(80,T)insert(80,T)80 findO(h) insertO(h) treescs,Technion294562204201451561221202010070358837633060T80insert(121,T)121max(T) maxO(h) max O(1)

treescs,Technion30456220420145156122120201007035883763306080121Tdelete(100,T) 1deletetreescs,Technion31456220420145156122120207035883763306080121Tdelete(88,T) 2treescs,Technion324562204201451561221202070353763306080121Tdelete(122,T) 3 deleteO(h) treescs,Technion33 T , -x . y . y=x , x. y > x, T. ( y < x ), T.32081711: . find(T,x) :trees33cs,Technion34 insert(T,4)320817114 insert(T,x) : x T. x -T, . v x y -v.vw4. y>,x w x v.5. ( y < x ), w x v.trees3435 : v . v , . -v , v .21458796324587963delete 1245879632358796delete 4trees35cs,Technion36 . v . v , . -v , v .: w -v inorder. ( - v " " . w ).4. v w.5. -v . 1 2 . 2358796236879delete 5vwwv v ( 5).trees36cs,Technion37 2358797.523789delete 5vww7.5v23789w7.5vdelete vtrees37cs,Technion38 : functionO(1)create(D)O(h)find(D,x)O(h)insert(D,x,info)O(h)delete(D,x)O(h) O(1)min(D)O(h) O(1)next(D,x)treescs,Technion39 // . ? . . h= log n

. . h = n - 1

? trees39cs,Technion40 ( 1,2,3 2,1,3 ). () n n!. - h(i) -i.

:

O(log n) O(log n). ( 254-258 ) . : O(n log n).trees4041 an a1 . bn b1 - T(n) n . T(j-1) j-1 T(n-j) n-j . :

bj-1 b1bn bj+1bja1trees41cs,Technion42

trees42cs,Technion43

trees43cs,Technion44

:

( , - 10242,080 ), , " "trees44