Algoritmi i Strukture Podataka 1 5 Stabla 4
Algoritmi i Strukture Podataka 1 5. Stabla (4)
Zadatak 1 Za dato binarno stablo napisati funkciju koja vraća celobrojnu vrednost koja predstavlja širinu stabla. Funkciji je prosleđen pokazivač na koren stabla. Širina nivoa je 1 Širina nivoa je 2 Širina nivoa je 3 Maksimalna širina je 3 što je i širina celog stabla. Elektrotehnički fakultet, Beograd Algoritmi i Strukture Podataka 1 2
Zadatak 1 - rešenje • Iterativno rešenje TREE_WIDTH (root) if root = nil then return 0 end_if INIT_QUEUE(Q) node = root max_width = 0 curr_width = 0 INSERT(Q, node) INSERT(Q, nil) //graničnik Elektrotehnički fakultet, Beograd while (FRONT(Q) nil ) do node = DELETE(Q) curr_width++ if left(node) nil then INSERT(Q, left(node)) end_if if right(node) nil then INSERT(Q, right(node)) end_if if FRONT(Q) = nil then INSERT(Q, nil) DELETE(Q) // brisanje nil if curr_width > max_width then max_width = curr_width end_if curr_width = 0 end_if end_while DELETE(Q) // brisanje nil return max_width Algoritmi i Strukture Podataka 1 3
Zadatak 1 - rešenje • Bez graničnika TREE_WIDTH (root) if root = nil then return 0 end_if max_width = 1 INIT_QUEUE(Q) INSERT(Q, root) curr_width = 1 Elektrotehnički fakultet, Beograd while (not QUEUE-EMPTY(Q)) do next_width = 0 while curr_width > 0 do begin curr_width = curr_width - 1 next = DELETE(Q) if left(next) nil then INSERT(Q, left(next)) next_width = next_width + 1 end_if if right(next) nil then INSERT(Q, right(next)) next_width = next_width + 1 end_if end_while if next_width > max_width then max_width = next_width end_if curr_width = next_width end_while return max_width Algoritmi i Strukture Podataka 1 4
Zadatak 2 Za binarno stablo dato pokazivačem na koren root napisati iterativnu funkciju koja efikasno vraća k-ti čvor po preorder obilasku. Dozvoljeno je proširiti čvor stabla jednim poljem. Ideja: u čvoru stabla čuvati i informaciju o tome koliko ima čvorova u levom podstablu - lc. U zavisnosti od toga da li u levom podstablu ima više od k čvorova ili ne prelazimo u levo ili desno podstablo, respektivno, i dalje tražimo k-ti čvor. Elektrotehnički fakultet, Beograd Algoritmi i Strukture Podataka 1 5
Zadatak 2 - rešenje PREORDER-K(root, k) temp = root while temp nil then if k = 1 then return temp end_if if lc(temp) + 1 >= k then temp = left(temp) k=k-1 else k = k - lc(temp) - 1 temp = right(temp) end_if end_while return nil Za vežbu: Naći k-ti element po inorder obilasku. Elektrotehnički fakultet, Beograd Algoritmi i Strukture Podataka 1 6
Zadatak 3 Dato je binarno stablo koje sadrži cele brojeve. Napisati u pseudokodu iterativnu implementaciju funkcije koja na osnovu zadatog stabla na čiji koren pokazuje pokazivač root formira simetrično stablo. Elektrotehnički fakultet, Beograd Algoritmi i Strukture Podataka 1 7
Zadatak 3 - rešenje • Rekurzivno rešenje CREATE_MIRROR_TREE(root) if root nil then node = GETNODE info(node) = info(root) left(node) = CREATE_MIRROR_TREE(right(root)) right(node) = CREATE_MIRROR_TREE(left(root)) return node else return nil end_if Elektrotehnički fakultet, Beograd Algoritmi i Strukture Podataka 1 8
Zadatak 3 - rešenje • Iterativno rešenje COPYNODE(node) if node = nil then return nil end_if res = GETNODE info(res) = info(node) left(res) = left(node) right(res) = right(node) return res Elektrotehnički fakultet, Beograd CREATE_MIRROR_TREE(root) if root = nil then return nil end_if INIT_STACK(S) res = COPYNODE(root) PUSH(S, res) while (not STACK-EMPTY(S)) do next = POP(S) left = left(next) right = right(next) = COPYNODE(left) left(next) = COPYNODE(right) if left(next) nil then PUSH(S, left(next)) end_if if right(next) nil then PUSH(S, right(next)) end_if end_while return res Algoritmi i Strukture Podataka 1 9
Zadatak 4 • Napisati u pseudokodu iterativnu funkciju koja pronalazi i ispisuje vrednosti svih c vorova u binarnom stablu koji se nalaze na udaljenosti k od nekog lista u tom stablu. Funkciji je prosleđen pokazivac na koren stabla kao i celobrojna vrednost k. Elektrotehnički fakultet, Beograd, 2010. Algoritmi i Strukture Podataka 1 SI 1 AS 1 10
Zadatak 4 – rešenje • A - B Elektrotehnički fakultet, Beograd, 2010. 0 E 0 C 1 D 1 Algoritmi i Strukture Podataka 1 SI 1 AS 1 F 2 11
Zadatak 4 – rešenje FIND K ANCESTORS(root, k) IS_LEAF(node) INIT_ARRAY(A) return node. left = nil and node. right = nil curr = 0 A[curr] = {root, -1} PRINT_ANCESTOR(idx, k) cnt = 1 node = A[idx] while (curr < N) do while k > 0 do node = A[curr]. first parent = node. second if node. left nil then if parent = -1 then A[cnt++] = {node. left, curr} return end_if if node. right nil then node = A[node. second] A[cnt++] = {node. right, curr} k— end_if end_while if IS_LEAF(node) then print(node. info) PRINT_ANCESTOR(curr, k) end_if curr++ Elektrotehnički fakultet, Algoritmi i Strukture Podataka 1 end_while Beograd, 2010. SI 1 AS 1 12
Zadatak 4 – rešenje (2) • Elektrotehnički fakultet, Beograd, 2010. Algoritmi i Strukture Podataka 1 SI 1 AS 1 13
Zadatak 4 – rešenje (2) Primer k = 2 A null B null 0 C null 1 D A 0 0 E A 0 1 F A 1 1 G B 0 0 H B 0 1 I C 1 1 Elektrotehnički fakultet, Beograd, 2010. 1 H Algoritmi i Strukture Podataka 1 SI 1 AS 1 B 0 1 14
Zadatak 4 – rešenje (2) FIND K ANCESTORS(root, k) INIT_QUEUE(Q) path = ALLOC(k) INSERT(Q, {root, null, path}) while (not QUEUE-EMPTY(Q)) do curr = DELETE(Q) node = curr. first ancestor = curr. second path = curr. third if node. left nil then INSERT(node. left, RESOLVE(node), path << 1) end_if if node. right nil then INSERT(node. right, RESOLVE(node), (path << 1) | 1) end_if if IS_LEAF(node) then print(ancestor. info) end_if end_while Elektrotehnički fakultet, Beograd, 2010. IS_LEAF(node) return node. left = nil and node. right = nil RESOLVE(node) ancestor = node. ancestor path = node. third If ancestor nil then return path[0] == 0 ? ancestor. left : ancestor. right end_if If GET_LEVEL(node) == k – 1 then return root end_if return nil Algoritmi i Strukture Podataka 1 SI 1 AS 1 15
Zadatak 4 – rešenje (3) • Elektrotehnički fakultet, Beograd, 2010. Algoritmi i Strukture Podataka 1 SI 1 AS 1 16
Zadatak 4 – rešenje (3) Primer k = 2 A -A A B A B A -B -D A -B E -A -E F C -A -E -F -C -A -E -A D A Elektrotehnički fakultet, Beograd, 2010. Algoritmi i Strukture Podataka 1 SI 1 AS 1 17
Zadatak 4 – rešenje (3) FIND K ANCESTORS(root, k) INIT_ARRAY(A) cnt = 0 next = root while (next nil) do A[cnt++] = next = left(next) end_while (cnt > 0) do next = A[--cnt] if (next > 0) then A[cnt++] = -next = right(next) while (next nil) do A[cnt++] = next = left(next) end_while else CHECK(A, cnt, k, -next) end_if end_while IS_LEAF(node) return node. left = nil and node. right = nil CHECK(A, cnt, k, node) if IS_LEAF(node) and cnt >= k then print(A[cnt – k] end_if return Elektrotehnički fakultet, Beograd, 2010. Algoritmi i Strukture Podataka 1 SI 1 AS 1 18
- Slides: 18