Microsoft PowerPoint - chapter5part2.pptx

Similar documents

Microsoft PowerPoint - lecture15.pptx

Linked Lists

840 提示 Excel - Excel -- Excel (=) Excel ch0.xlsx H5 =D5+E5+F5+G5 (=) = - Excel 00

Microsoft Word - ACL chapter02-5ed.docx

204 */ InitiateStack s ; /* s */ i = n; t = p = new node; /* */ p->data = postorder[i]; while i > q = new node; if parent[i - ] == postorder[i] S,T S

Open topic Bellman-Ford算法与负环

威 福 髮 藝 店 桃 園 市 蘆 竹 區 中 山 里 福 祿 一 街 48 號 地 下 一 樓 50,000 獨 資 李 依 純 105/04/06 府 經 登 字 第 號 宏 品 餐 飲 桃 園 市 桃 園 區 信 光 里 民

恩 典 课 堂 教 学 概 览 课 堂 环 节 持 续 时 间 活 动 所 需 材 料 1 欢 迎 持 续 在 门 口 欢 迎 学 生, 聆 听 他 们 分 享 本 周 开 心 或 烦 恼 的 事 预 备 活 动 <10 分 钟 A 猜 猜 是 谁 B 上 帝 的 礼 物 无 孩 子 们 的 儿 时

团 契 就 体 力 来 说, 参 孙 乃 是 地 上 极 强 壮 的 人 ; 但 在 自 制 忠 贞 和 坚 稳 上, 他 却 是 人 间 最 软 弱 的 了 先 祖 与 先 知 第 页 教 室 布 置 见 第 一 课 课 堂 教 学 概 览 课 堂 环 节 持 续 时 间 活 动

0 0 = 1 0 = 0 1 = = 1 1 = 0 0 = 1

NethersoleJO89(8).indd

关于规范区委、区委办公室发文

Microsoft Word - ??山

Microsoft Word - 助理人員教育訓練-會計室.docx

Microsoft Word - _m30.doc

ebook14-4

Microsoft PowerPoint - ds-1.ppt [兼容模式]

(Microsoft Word - 3\271\375\246\321\257R.doc)

大 台 北 與 桃 竹 苗 地 區 北 得 拉 曼 巨 木 步 道 新 竹 縣 尖 石 鄉 鎮 西 堡 巨 木 群 步 道 新 竹 縣 尖 石 鄉 鳥 嘴 山 登 山 步 道 苗 栗 縣 泰 安 鄉 加 里 山 登 山 步 道 苗 栗 縣 南 庄 鄉

Microsoft Word htm

Microsoft Word - 2AF63內文.doc

untitled

ENGG1410-F Tutorial 6

Fun Time (1) What happens in memory? 1 i n t i ; 2 s h o r t j ; 3 double k ; 4 char c = a ; 5 i = 3; j = 2; 6 k = i j ; H.-T. Lin (NTU CSIE) Referenc

Explain each of the following terms. (12%) (a) O(n 2 ) (b) protected in C++ language (c) sparse matrix 7. Write

什么是函数式编程?

的 精 准 帮 扶 持 续 扩 大 有 效 投 入, 实 施 项 目 建 设 四 督 四 保 制 度, 积 极 对 接 国 家 重 大 工 程 包 和 专 项 建 设 基 金, 商 合 杭 高 铁 合 安 高 铁 京 东 方 10.5 代 线 等 一 批 重 大 项 目 开 工 建 设, 合 福 高

2012年海南党建第2期目录.FIT)

习 近 平 总 书 记 2016 两 会 新 语 一 年 一 度 的 两 会 已 经 落 下 帷 幕 会 议 期 间, 习 近 平 总 书 记 谈 改 革 聊 民 生, 在 供 给 侧 改 革 打 赢 脱 贫 攻 坚 战 保 护 生 态 环 境 和 实 现 强 军 目 标 等 多 个 方 面 发 表

标题

老 床 位 1267 张, 五 年 累 计 建 设 养 老 床 位 3394 张 年 初 确 定 的 24 项 重 大 项 目 总 体 进 展 顺 利,9 方 面 区 政 府 实 事 项 目 全 面 完 成 ( 一 ) 区 域 经 济 转 型 升 级 成 效 明 显 现 代 服 务 业 为 主 导

目 录 一 重 要 提 示... 3 二 公 司 主 要 财 务 数 据 和 股 东 变 化... 3 三 重 要 事 项... 6 四 附 录 / 21

Microsoft Word - 澎湖田調報告-昕瑤組.doc


四川省普通高等学校

第 一 节 认 识 自 我 的 意 义 一 个 人 只 有 认 识 自 我, 才 能 够 正 确 地 认 识 到 自 己 的 优 劣 势, 找 出 自 己 的 职 业 亮 点, 为 自 己 的 顺 利 求 职 推 波 助 澜 ; 一 个 人 只 有 认 识 自 我, 才 能 在 求 职 中 保 持

Microsoft Word - 11.doc

恩 典 课 堂 教 学 概 览 课 堂 环 节 持 续 时 间 活 动 所 需 材 料 欢 迎 在 门 口 欢 迎 孩 子, 聆 听 他 们 分 享 本 周 开 心 或 烦 恼 的 事 无 预 备 活 动 <10 分 钟 A 十 诫 石 板 B 我 是 谁? 粘 土 牙 签 一 些 名 人 的 照

2013 C 1 #include <stdio.h> 2 int main(void) 3 { 4 int cases, i; 5 long long a, b; 6 scanf("%d", &cases); 7 for (i = 0; i < cases; i++) 8 { 9 scanf("%

投影片 1


CC213

向陽花木大綱---

SA-DK2-U3Rユーザーズマニュアル

int *p int a 0x00C7 0x00C7 0x00C int I[2], *pi = &I[0]; pi++; char C[2], *pc = &C[0]; pc++; float F[2], *pf = &F[0]; pf++;

3. 給 定 一 整 數 陣 列 a[0] a[1] a[99] 且 a[k]=3k+1, 以 value=100 呼 叫 以 下 兩 函 式, 假 設 函 式 f1 及 f2 之 while 迴 圈 主 體 分 別 執 行 n1 與 n2 次 (i.e, 計 算 if 敘 述 執 行 次 數, 不

ebook55-13


Front 2 Polar F11 ( ) : Polar F11 Polar F11 Polar F11 Polar (Keeps U Fit - Own Workout Program) Polar Polar F11 Polar F11 Polar F11 Polar (

A Study on JI Xiaolan s ( ) Life, Couplets and Theories of Couplets 紀 曉 嵐 ( ) 生 平 資 料 斠 正 及 對 聯 聯 論 研 究 LI Ha 李 夏 THE UNIVER

3.1 num = 3 ch = 'C' 2

Microsoft PowerPoint 李忠敬-替代役之展望

Microsoft Word htm

演算法導入、ソート、データ構造、ハッシュ

9, : Java 19., [4 ]. 3 Apla2Java Apla PAR,Apla2Java Apla Java.,Apla,,, 1. 1 Apla Apla A[J ] Get elem (set A) A J A B Intersection(set A,set B) A B A B

Python a p p l e b e a r c Fruit Animal a p p l e b e a r c 2-2

LEETCODE leetcode.com 一 个 在 线 编 程 网 站, 收 集 了 IT 公 司 的 面 试 题, 包 括 算 法, 数 据 库 和 shell 算 法 题 支 持 多 种 语 言, 包 括 C, C++, Java, Python 等 2015 年 3 月 份 加 入 了 R

ACI pdf

Microsoft Word - data_mid1611_and_sol.docx

國家圖書館典藏電子全文

Microsoft Word - å�¦ä¹€å¿…å¾Šå’‹éłƒï¼‹å®ı稿;(.doc


Linked Lists

<4D F736F F D20312E5FA473AEFCB867AED5AA605FBB50B04BCFC8AABAAFABB8DCACE3A8732E646F63>


<4D F736F F D20C8EDC9E82DCFC2CEE7CCE22D3039C9CF>

Microsoft PowerPoint - ds-9.ppt [兼容模式]

【摘要】

ebook39-5

Public Projects A Thesis Submitted to Department of Construction Engineering National Kaohsiung First University of Science and Technology In Partial

Microsoft PowerPoint - lec20.ppt

南華大學數位論文

标题

C 1

Microsoft Word - (web)_F.1_Notes_&_Application_Form(Chi)(non-SPCCPS)_16-17.doc

活 動 紀 要 一 活 動 目 的 至 小 琉 球 和 大 鵬 灣 為 例, 了 解 當 地 特 色 文 化 及 生 態 發 展, 並 促 進 同 儕 之 間 的 友 誼 二 活 動 流 程 表 日 期 :100 年 11 月 19 日 ( 六 ) 時 間 活 動 主 題 活 動 內 容 地 點 主

國立臺南大學數位論文典藏.pdf


撰 寫 人 :2B1 王 清 燕 書 名 : 追 風 箏 的 女 孩 條 碼 號 : 月 份 閱 讀 心 得 佳 作 我 覺 得 這 是 一 本 教 我 們 用 殘 酷 的 角 度 認 識 生 命 的 小 說 ; 與 同 儕 甚 是 摯 友 間 也 可 能 出 現 競 奪 下 的

穨report.PDF

Figure 1: Game Tree 为 了 方 便 讨 论, 我 们 这 里 设 这 里 讨 论 的 博 弈 树 是 一 棵 有 限 树, 设 有 两 个 棋 手 甲 与 乙 进 行 这 场 博 弈, 这 样, 博 弈 树 分 为 三 类 结 点 : 1. 奇 数 层 的 非 叶 子 结 点 :

AL-M200 Series

Microsoft Word doc


主程式 : public class Main3Activity extends AppCompatActivity { ListView listview; // 先整理資料來源,listitem.xml 需要傳入三種資料 : 圖片 狗狗名字 狗狗生日 // 狗狗圖片 int[] pic =new

C H A P T E R 7 Windows Vista Windows Vista Windows Vista FAT16 FAT32 NTFS NTFS New Technology File System NTFS

目錄

Chapter 1 選 用 好 的 燜 燒 罐 選 用 好 的 燜 燒 罐 是 做 好 燜 燒 罐 料 理 最 重 要 的 步 驟, 除 了 須 注 意 使 用 的 材 質 是 否 符 合 食 器 使 用 標 準, 也 須 注 意 燜 燒 罐 的 保 溫 效 果, 才 能 安 心 享 用 燜 燒 罐

(Microsoft Word - Motion Program \270\305\264\272\276\363 \307\245\301\366 \271\327 \270\361\302\367.doc)

Stochastic Processes (XI) Hanjun Zhang School of Mathematics and Computational Science, Xiangtan University 508 YiFu Lou talk 06/

穨control.PDF

Microsoft PowerPoint - Eisenstein_ABET_Presentation_Beijing_Oct_2007-Chinese.ppt [兼容模式]

6寸PDF生成工具

FY.DOC

淡 江 大 學

06 01 action JavaScript action jquery jquery AJAX CSS jquery CSS jquery HTML CSS jquery.css() getter setter.css('backgroundcolor') jquery CSS b

概述

6-1 Table Column Data Type Row Record 1. DBMS 2. DBMS MySQL Microsoft Access SQL Server Oracle 3. ODBC SQL 1. Structured Query Language 2. IBM

Transcription:

作業三禮拜四 5pm due 更多的樹 Michael Tsai 2010/10/29 請各位多利用下周二三四的 office hour

今日菜單 樹裡面找東西 選擇樹 ( 勝者樹與敗者樹 ) 樹的相關動作 ( 複製樹 ) 有線頭的樹 森林 解邏輯運算式

Binary search tree 問題 : 找系上某個同學的作業成績 條件 : 知道學號 (key) 找到學號就可以對應到儲存資料的地方 (search) 常常有人進來 (add node) 常常有人出去 (delete node)

Binary search tree Definition: A binary search tree is a binary tree. It may be empty. If it is not empty then it satisfies the following properties: 1. The root has a key. 2. The keys (if any) in the left subtree are smaller than the key in the root 3. The keys (if any) in the right subtree are larger than the key in the root 4. The left and right subtrees are also binary search trees ( 隱藏 ) All keys are distinct.

Binary search tree 30 這些是不是 binary search tree? 找到以後要做什麼? 20 15 25 2 60 5 40 Yes 80 號 HW1 65 HW2 65 HW3 空 Extra -20000 70 12 10 22 65 80 No Yes

如何尋找? 20 假設要找 10 12 22 10 15 element *search(tpointer root, int key) { if (!root) return NULL; if (k==root->data.key) return &(root->data); if (k<root->data.key) return search(root->leftchild, k); return search(root->rightchild, k); } 25 簡單. 那 time complexity = O(??)

如何插入新的 node? 先找有沒有一樣的 ( 隱藏版 rule: binary search tree 裡面不可以有一樣的 key) 找不到的話, 插在最後 找不到 的位置 插入 : 11 11 20 12 22 10 15 25

如何刪掉一個 node? 首先要先找到那個 node 接著, 有各種不同情形 : 20 如果沒有手 (degree=0) 直接拿掉 10 15 12 22 25 11

如何刪掉一個 node? 如果只有一隻手 (degree=1) 則把那個唯一的 child 拿上來接到 parent 上 20 例如 : 拿掉 25 15 25 10 12 22 問題 : 要怎麼接? 怎麼記得 parent 是誰? 11

如何刪掉一個 node? 如果兩手都有東西呢? (degree=2) 20 例如刪掉 12 找左邊 child 底下最大的 ( 或者是右邊 child 底下最小的 ) 10 15 12 22 25 刪掉它並把它移到原本刪掉的位置 11 問題 : 那如果那個最大的底下還有 child 呢?

一些 binary search tree 的動作 threewayjoin(small, mid, big) mid (small 裡面都比 mid 小, big 裡面都比 mid 大 ) mid->left=small; mid->right=big; 結束! small big height=?

一些 binary search tree 的動作 twowayjoin(small, big) (small 裡面的都比 big 裡面的小 ) mid 找到 small 裡面最小的 (O(??)) delete 它 ( 用前面講的刪 node 方法 ) 當成 mid threewayjoin(small, mid, big) small big height=? time complexity=o(??)

一些 binary search tree 的動作 split(tree,k,small,mid,big) 要把一棵 binary search tree 以 k 為分界切成 small, mid, big small 都比 k 小, big 都比 k 大, mid 跟 k 一樣 例子 : k=14 課本 program 5.18 20 13 22 10 15 25 11 14 16

s b small c k=14 20 big 13 22 10 15 25 11 14 16 mid

binary search tree 的高度 高度跟 node 數目有何關係? 20 h=o(log n)?? 13 15 No. In the worst case, h=o(n) 10 之後 chapter 10 & 11 會講到 h=o(log n) 的 balanced search trees

選擇樹 問題 : 有 k 組排好的數列, 要合併成一組 ( 共 n 個數字 ) 先來一個爛方法 每回合都比較各數列的頭頭 ( 共 k(k-1) 次 ), 比較出最大的一個 然後放入最後的數列裡 8 7 6 5 4 3 2 1 0 8 3 7 5 2 1 4 6 0 這樣的話, time complexity=ο

選擇樹 使用樹來記錄一些資訊 : 精神 : 上一回合比較的結果並不是全然無用 ( 只有上一回合勝出的數字和別人的比較結果需要重做 ) 紀錄一些資訊使得下一回合不用全部重來 目標 time complexity = Ο log

勝者樹 Tree 裡面記得每次贏的數字 很像錦標賽 用黑板講解 有沒有什麼缺點? 每次都要和 sibling 比 Linked representation 的時候不好找 從上一回合贏的那個數列一直到 root 每個數字都要改掉 ( 曼一點點 )

敗者樹 Tree 裡面記得每次贏的數字 用黑板講解 這樣就不需要跟 sibling 比了 每回合跟 parent 比, 輸的留下, 贏的晉級 time complexity=o(??) < 動腦時間 > 如果 2, 怎麼處理?

講一些輕鬆一點的 如果要拷貝 binary tree 怎麼做? 使用 recursive, 會很簡單 tpointer copy(tpointer original) { tpointer temp; if (original) { MALLOC(temp, sizeof(*temp)); temp->leftchild=copy(original->leftchild); temp->rightchild=copy(original->rightchild); temp->data=original->data; return temp; } return NULL; }

講一些輕鬆一點的 如果要測試兩個 binary tree 有沒有一模一樣怎麼做? 一樣用 recursive int equal(tpointer first, tpointer second) { if (!first &&!second) return TRUE; return (first->data == second->data) && equal(first->leftchild, second->leftchild) && equal(first->rightchild, second->rightchild); }

線頭樹?? root left A right left B right left C right 浪費浪費 left D right left E right 浪費浪費 left F right 浪費 left G right 浪費浪費浪費

浪費掉的 link field 可以拿來存一些別的資訊 Threaded Binary Tree 1. 如果 leftchild 是 null, 那就改成指到 inorder traversal 的前一個 node ( 又稱為 inorder predecessor) ( 此為 thread) 2. 如果 rightchild 是 null, 那就改成指到 inorder traversal 的後一個 node ( 又稱為 inorder successor) ( 此為 thread) 3. node 的 structure 裡面放兩個額外的 boolean 欄位, 說明是 link 還是 thread 效果 : 之後做 inorder traversal 不需要 stack 了!

Threaded binary tree 長這樣 root f A f f B f t C t t D t f E f f F t t G t t H t

Threaded Binary Tree 怎麼找到 inorder successor? 如果 rightthread == true, 那麼就是 thread 指到的地方 如果 rightthread == false, 那就找 rightchild 的 leftchild 一直 root 走到底 f A f f B f t C t t D t f E f f F t t G t t H t

Threaded Binary Tree 接著, 要做 inorder traversal 就很簡單了 for(;;) { temp=insucc(temp); if (temp==tree) break; // 走回 root 了 printf( %3c, temp->data); } < 動腦時間 > 如果要用 threaded binary tree 做 postorder or preorder traversal 呢?

在 Threaded Binary 裡面加一個 node f A f f B f t C t t D t f E f f F t t G ft t H t t I t

f A f f B f t C t t D t t I f f E f f F t t G t t H t

森林 Definition: A forest is a set of 0 disjoint trees. 要怎麼把森林轉成一棵 binary tree 呢? 提示 : 左小孩右兄弟姊妹法 + root 右手沒有連東西

森林的 traversal forest preorder (1) If F is empty then return (2) Visit the root of the first tree of F. (3) Traverse the subtrees of the first tree in forest preorder ( 把第一個 tree 的 subtrees 當成 forest) (4) Traverse the remaining trees of F in forest preorder 對應到轉換後的 binary tree 的 preorder traversal forest inorder 對應到轉換後的 binary tree 的 inorder traversal forest postorder 沒有對應 forest level-order

計算邏輯運算式 變數可為 true or false 三種 operator:,, 可以使用括號 例如 如何計算當,,,, 時的結果? 進階題 : 如何找出所有組合使得結果為 true?

計算邏輯運算式 用什麼 traversal 方法?

33 周末愉快 作業一已經改好 改好的作業下周會發給同學們. 成績分布會公布 請比較低分的同學多加留意! 多利用 office hour ( 下周二三四 ) 洲際盃中華 12:5 日本 請同學準備好下周要發問的問題 我也會準備一些我覺得重要的課題來複習