算法和数据结构09课件.ppt
- 【下载声明】
1. 本站全部试题类文档,若标题没写含答案,则无答案;标题注明含答案的文档,主观题也可能无答案。请谨慎下单,一旦售出,不予退换。
2. 本站全部PPT文档均不含视频和音频,PPT中出现的音频或视频标识(或文字)仅表示流程,实际无音频或视频文件。请谨慎下单,一旦售出,不予退换。
3. 本页资料《算法和数据结构09课件.ppt》由用户(晟晟文业)主动上传,其收益全归该用户。163文库仅提供信息存储空间,仅对该用户上传内容的表现方式做保护处理,对上传内容本身不做任何修改或编辑。 若此文所含内容侵犯了您的版权或隐私,请立即通知163文库(点击联系客服),我们立即给予删除!
4. 请根据预览情况,自愿下载本文。本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
5. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007及以上版本和PDF阅读器,压缩文件请下载最新的WinRAR软件解压。
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 算法 数据结构 09 课件
- 资源描述:
-
1、2004-3L.Chen12004-3L.Chen2qTrees.qTerminology.qTraversal of Binary Trees.qExpression Trees.qBinary Search Trees.2004-3L.Chen3Definition A trees:t is a finite nonempty set of elements.One of these elements is called the root,and the remaining elements(if any)are partitioned into trees which are calle
2、d the subtrees of t.2004-3L.Chen62004-3L.Chen7nodes2004-3L.Chen8parent node2004-3L.Chen9child nodesparent node2004-3L.Chen10child nodesparent node2004-3L.Chen11root node2004-3L.Chen12leaf nodes2004-3L.Chen13sub-tree2004-3L.Chen14sub-tree2004-3L.Chen15sub-tree2004-3L.Chen16sub-treeLevel 4Level 3Level
3、 2ObjectNumberThrowableOutputStreamIntegerDoubleExceptionFileOutputStreamRuntimeExceptionLevel 1ObjectNumberThrowableOutputStreamIntegerDoubleExceptionFileOutputStreamRuntimeException321100100ObjectNumberThrowableOutputStreamIntegerDoubleExceptionFileOutputStreamRuntimeException321100100Degree of tr
4、ee=3.2004-3L.Chen20nFinite(possibly empty)collection of elements.nA nonempty binary tree has a root element.nThe remaining elements(if any)are partitioned into two binary trees.nThese are called the left and right subtrees of the binary tree.2004-3L.Chen21nNo node in a binary tree may have a degree
5、more than 2,whereas there is no limit on the degree of a node in a tree.nA binary tree may be empty;a tree cannot be empty.2004-3L.Chen22nThe subtrees of a binary tree are ordered;those of a tree are not ordered.abab Are different when viewed as binary trees.Are the same when viewed as trees.2004-3L
6、.Chen23ABABemptyemptyABCDEFGHI#nodes=9height of root=42004-3L.Chen24nUse binary trees to represent arithmetic expressionsne.g.,/a*e-cd)(*dcea2004-3L.Chen25n(a+b)*(c+d)+e f/g*h+3.25nExpressions comprise three kinds of entities.Operators(+,-,/,*).Operands(a,b,c,d,e,f,g,h,3.25,(a+b),(c+d),etc.).Delimit
7、ers(,).2004-3L.Chen26nNumber of operands that the operator requires.nBinary operator requires two operands.a+bc/de-fnUnary operator requires one operand.+g-h2004-3L.Chen27nNormal way to write an expression.nBinary operators come in between their left and right operands.a*ba+b*ca*b/c(a+b)*(c+d)+e f/g
8、*h+3.252004-3L.Chen28nHow do you figure out the operands of an operator?a+b*ca*b+c/dnThis is done by assigning operator priorities.priority(*)=priority(/)priority(+)=priority(-)nWhen an operand lies between two operators,the operand associates with the operator that has higher priority.2004-3L.Chen2
9、9nWhen an operand lies between two operators that have the same priority,the operand associates with the operator on the left.a+b-ca*b/c/d2004-3L.Chen30nSubexpression within delimiters is treated as a single operand,independent from the remainder of the expression.(a+b)*(c d)/(e f)2004-3L.Chen31nNee
10、d operator priorities,tie breaker,and delimiters.nThis makes computer evaluation more difficult than is necessary.nPostfix and prefix expression forms do not rely on operator priorities,a tie breaker,or delimiters.nSo it is easier for a computer to evaluate expressions that are in these forms.2004-3
11、L.Chen32nThe postfix form of a variable or constant is the same as its infix form.a,b,3.25nThe relative order of operands is the same in infix and postfix forms.nOperators come immediately after the postfix form of their operands.Infix=a+bPostfix=ab+2004-3L.Chen33nInfix=a+b*cPostfix=a b c*+Infix=a*b
12、+c Postfix=a b*c+Infix=(a+b)*(c d)/(e+f)Postfix=a b+c d-*e f+/2004-3L.Chen34nReplace with new symbols.+a=a+a+b=a b+-a=a?-a-b=a?b-2004-3L.Chen35nScan postfix expression from left to right pushing operands on to a stack.nWhen an operator is encountered,pop as many operands as this operator needs;evalu
13、ate the operator;push the result on to the stack.nThis works because,in postfix,operators come immediately after their operands.2004-3L.Chen36n(a+b)*(c d)/(e+f)na b+c d-*e f+/na b+c d-*e f+/stacka a b+c d-*e f+/b a b+c d-*e f+/2004-3L.Chen37n(a+b)*(c d)/(e+f)na b+c d-*e f+/na b+c d-*e f+/stack(a+b)a
14、 b+c d-*e f+/a b+c d-*e f+/a b+c d-*e f+/c a b+c d-*e f+/d a b+c d-*e f+/2004-3L.Chen38n(a+b)*(c d)/(e+f)na b+c d-*e f+/stack(a+b)a b+c d-*e f+/(c d)2004-3L.Chen39n(a+b)*(c d)/(e+f)na b+c d-*e f+/stack(a+b)*(c d)a b+c d-*e f+/e a b+c d-*e f+/a b+c d-*e f+/f a b+c d-*e f+/2004-3L.Chen40n(a+b)*(c d)/(
15、e+f)na b+c d-*e f+/stack(a+b)*(c d)a b+c d-*e f+/(e+f)a b+c d-*e f+/a b+c d-*e f+/a b+c d-*e f+/a b+c d-*e f+/2004-3L.Chen41nThe prefix form of a variable or constant is the same as its infix form.a,b,3.25nThe relative order of operands is the same in infix and prefix forms.nOperators come immediate
16、ly before the prefix form of their operands.Infix=a+bPostfix=ab+Prefix=+ab2004-3L.Chen42na+b+ab-a-a2004-3L.Chen43n(a+b)*(c d)/(e+f)/+ab-cd+ef*/2004-3L.Chen44nLeft and right operands are easy to visualize.nCode optimization algorithms work with the binary tree form of an expression.nSimple recursive
17、evaluation of expression.+ab-cd+ef*/2004-3L.Chen451.A binary tree with n nodes has n-1 edges2.A binary tree of height h,h0,has at least h and at most 2h-1 elements in it3.The height of a binary tree that contains n elements is at most n and at least log2(n+1)2004-3L.Chen48nMinimum number of nodes in
18、 a binary tree whose height is h.nAt least one node at each of first h levels.minimum number of nodes is hnAll possible nodes at first h levels are present.Maximum number of nodes=1+2+4+8+2h-1=2h-12004-3L.Chen50nLet n be the number of nodes in a binary tree whose height is h.nh=n=2h 1nlog2(n+1)=h n,
19、where n is the number of nodes.nIf 2i n,node i has no left child.123456789101112131415 nRight child of node i is node 2i+1,unless 2i+1 n,where n is the number of nodes.nIf 2i+1 n,node i has no right child.123456789101112131415123467121415513810119Definition:Full binary tree of height h has exactly 2
20、h-1 elementsExample:height h=4 24-1 =15 elements2004-3L.Chen57123465 Definition:binary tree of height h all levels except possibly level h-1 are full full binary tree is a special case of a complete binary tree2004-3L.Chen58nStart with a full binary tree that has at least n nodes.nNumber the nodes a
21、s described earlier.nThe binary tree defined by the nodes numbered 1 through n is the unique n node complete binary tree.2004-3L.Chen59nComplete binary tree with 10 nodes.123456789101112131415nLet i,1 i n,be the number assigned to an element of a complete binary tree of height h as follows:nBegin at
22、 level 1 and go down to level h,numbering left to right(see next slide).If i=1,then element is root of binary tree.If i1,then its parent has number i/2nIf 2in,then element has no left child.O.w.,its left child has been assigned the number 2inIf 2i+1n,then this element has no right child.O.w.,its rig
23、ht child has been assigned the number 2i+12004-3L.Chen61123467121415513810119Rigorous proof by induction on i2004-3L.Chen62nWhich nodes are leaves?nWhich node is the root?nWhat is the parent of C?nWhich nodes are ancestors of E?ABCDFGIJEKHLnWhat is the depth of node C?nWhat is the height of node C/t
展开阅读全文