西安电子科技大学:《数据结构与算法分析 Data Structure and Algorithm Analysis》课程教学资源(PPT课件讲稿)02 Trees(主讲:刘静)

Preliminaries A tree can be defined in several ways.One natural way to define a tree is recursively. A tree is a collection of nodes.The collection can be empty;otherwise,a tree consists of a distinguished node r,called the root and zero or more nonempty (sub)trees 71,72....,7k each of whose roots are connected by a directed edge from / The root of each subtree is said to be a child of, and ris the parent of each subtree root. root 方 万五 万 Ta Tio
Preliminaries ◼ A tree can be defined in several ways. One natural way to define a tree is recursively. ◼ A tree is a collection of nodes. The collection can be empty; otherwise, a tree consists of a distinguished node r, called the root, and zero or more nonempty (sub)trees T1 , T2 , …, Tk , each of whose roots are connected by a directed edge from r. ◼ The root of each subtree is said to be a child of r, and r is the parent of each subtree root. root T1 T2 T3 T4 … T10

Preliminaries From the recursive definition,we find that a tree is a collection of nodes,one of which is the root,and /1 edges.That there are /1 edges follows from the fact that each edge connects some node to its parent,and every node except the root has one parent. A The root is A. Node Fhas A as a parent and I,Jas children. Each node may have an arbitrary B E number of children,possibly zero. Nodes with no children are known as leaves. H I J Nodes with the same parent are siblings. Grandparent and grandchild K relations can be defined in a similar manner
Preliminaries ◼ From the recursive definition, we find that a tree is a collection of N nodes, one of which is the root, and N-1 edges. That there are N-1 edges follows from the fact that each edge connects some node to its parent, and every node except the root has one parent. A B C D E F G H I J K L ◼ The root is A. ◼ Node E has A as a parent and I, J as children. ◼ Each node may have an arbitrary number of children, possibly zero. ◼ Nodes with no children are known as leaves. ◼ Nodes with the same parent are siblings. ◼ Grandparent and grandchild relations can be defined in a similar manner

Preliminaries A path from node n to n is defined as a sequence of nodes m,n,n...,nk such that n is the parent of n for 1≤Kk The length of this path is the number of edges on the path,namely k1.There is a path of length zero from every node to itself.Notice that in a tree there is exactly one path from the root to each node
Preliminaries ◼ A path from node n1 to nk is defined as a sequence of nodes n1 , n2 , n3…, nk such that ni is the parent of ni+1 for 1i<k. ◼ The length of this path is the number of edges on the path, namely k-1. There is a path of length zero from every node to itself. Notice that in a tree there is exactly one path from the root to each node

Preliminaries For any node n the depth of n;is the length of the unique path from the root to /,Thus,the root is at depth 0.The height of n,is the length of the longest path from n,to a leaf.Thus all leaves are at height 0.The height of a tree is equal to the height of the root.The depth of a tree is equal to the depth of the deepest leaf;this is always equal to the height of the tree. If there is a path from m to m,then m is an ancestor of m and m is a descendant of m.If mtm,then m is a proper ancestor of m and m is a proper descendant of m
Preliminaries ◼ For any node ni , the depth of ni is the length of the unique path from the root to ni . Thus, the root is at depth 0. The height of ni is the length of the longest path from ni to a leaf. Thus all leaves are at height 0. The height of a tree is equal to the height of the root. The depth of a tree is equal to the depth of the deepest leaf; this is always equal to the height of the tree. ◼ If there is a path from n1 to n2 , then n1 is an ancestor of n2 and n2 is a descendant of n1 . If n1n2 , then n1 is a proper ancestor of n2 and n2 is a proper descendant of n1

Preliminaries For example,Eis at depth 1 and height 2;Dis at depth 1 and height 1;the height of the tree is 3. A B D E G H I J K
Preliminaries ◼ For example, E is at depth 1 and height 2; D is at depth 1 and height 1; the height of the tree is 3. A B C D E F G H I J K L

Implementation of Trees One way to implement a tree would be to have in each node,besides its data,a pointer to each child of the node. However,since the number of children per node can vary so greatly and is not known in advance,it might be infeasible to make the children direct links in the data structure,because there would be too much wasted space. The solution is simple:Keep the children of each node in a linked list of tree nodes
Implementation of Trees ◼ One way to implement a tree would be to have in each node, besides its data, a pointer to each child of the node. ◼ However, since the number of children per node can vary so greatly and is not known in advance, it might be infeasible to make the children direct links in the data structure, because there would be too much wasted space. ◼ The solution is simple: Keep the children of each node in a linked list of tree nodes

Implementation of Trees struct TreeNode { char Element; TreeNode*FirstChild; TreeNode*NextSibling; )
Implementation of Trees struct TreeNode { char Element; TreeNode* FirstChild; TreeNode* NextSibling; }

Implementation of Trees A I A B C D E F H I H I J K K I Arrows that point downward are FirstChild pointers. Arrows that go left to right are NextSib/ing pointers. Node Ehas both a pointer to a sibling (A)and a pointer to a child (while some nodes have neither
Implementation of Trees A B C D E F G H I J K L A B C D E F G H I J K L ◼Arrows that point downward are FirstChild pointers. ◼Arrows that go left to right are NextSibling pointers. ◼Node E has both a pointer to a sibling (F) and a pointer to a child (I), while some nodes have neither

Implementation of Trees Example:Please give the first child/next sibling representation of the following tree. A B C D E F G H I J K
Implementation of Trees A B C D E H F G I J ◼ Example: Please give the first child/next sibling representation of the following tree. K L

Application of Trees There are many applications for trees.One of the popular uses is the directory structure in many common operating systems. /usr* mark* alex* bi训* course* junk.c prog1.r prog2.r The root of this directory is /usr. ■ The asterisk next to the name indicates that /usr is itself a directory
mark* alex* bill* course* junk.c prog1.r prog2.r /usr* Application of Trees ◼ There are many applications for trees. One of the popular uses is the directory structure in many common operating systems. ◼ The root of this directory is /usr. ◼ The asterisk next to the name indicates that /usr is itself a directory
按次数下载不扣除下载券;
注册用户24小时内重复下载只扣除一次;
顺序:VIP每日次数-->可用次数-->下载券;
- 西安电子科技大学:《数据结构与算法分析 Data Structure and Algorithm Analysis》课程教学资源(PPT课件讲稿)05 Disjoint Set ADT.ppt
- 西安电子科技大学:《数据结构与算法分析 Data Structure and Algorithm Analysis》课程教学资源(PPT课件讲稿)04 Priority Queues(Heaps).ppt
- 西安电子科技大学:《数据结构与算法分析 Data Structure and Algorithm Analysis》课程教学资源(PPT课件讲稿)03 Hashing.ppt
- 西安电子科技大学:《数据结构与算法分析 Data Structure and Algorithm Analysis》课程教学资源(PPT课件讲稿)08 Algorithm Design Techniques.ppt
- 西安电子科技大学:《数据结构与算法分析 Data Structure and Algorithm Analysis》课程教学资源(PPT课件讲稿)07 Graph Algorithms.ppt
- 西安电子科技大学:《数据结构与算法分析 Data Structure and Algorithm Analysis》课程教学资源(PPT课件讲稿)06 Algorithm Analysis and Sorting.ppt
- 西安电子科技大学:《算法设计技术 Algorithms Design Techniques》课程教学资源(PPT课件讲稿)Iterative Improvement for Domain-Specific Problems(Chapter 16 Network Flow Chapter 17 Matching).ppt
- 西安电子科技大学:《算法设计技术 Algorithms Design Techniques》课程教学资源(PPT课件讲稿)Coping with Hardness(Chapter 13 Backtracking Chapter 14 Randomized Algorithms Chapter 15 Approximation Algorithms).ppt
- 西安电子科技大学:《算法设计技术 Algorithms Design Techniques》课程教学资源(PPT课件讲稿)First-Cut Techniques(Chapter 8 The Greedy Approach Chapter 9 Graph Traversal).ppt
- 西安电子科技大学:《算法设计技术 Algorithms Design Techniques》课程教学资源(PPT课件讲稿)Techniques Based on Recursion(Chapter 5 Induction Chapter 6 Divide and Conquer Chapter 7 Dynamic Programming).ppt
- 西安电子科技大学:《算法设计技术 Algorithms Design Techniques》课程教学资源(PPT课件讲稿)Chapter 01 Basic Concepts in Algorithmic Analysis(主讲:刘静).ppt
- 《计算机基础》课程教学资源(参考论文)Motif Difficulty(MD):A Predictive Measure of Problem Difficulty for Evolutionary Algorithms using Network Motifs.pdf
- 《计算机基础》课程教学资源(参考论文)An Organizational Evolutionary Algorithm for Numerical Optimization.pdf
- 《计算机基础》课程教学资源(参考论文)A Multiagent Evolutionary Algorithm for Constraint Satisfaction Problems.pdf
- 《计算机基础》课程教学资源(参考论文)Comments on “the 1993 DIMACS Graph Coloring Challenge” and “Energy Function-Based Approaches to Graph Coloring”.pdf
- 《计算机基础》课程教学资源(参考论文)Moving Block Sequence and Organizational Evolutionary Algorithm for General Floorplanning with Arbitrarily Shaped Rectilinear Blocks.pdf
- 《计算机基础》课程教学资源(参考论文)A Multi-Agent Genetic Algorithm for Global Numerical Optimization.pdf
- 《计算机基础》课程教学资源(参考论文)An Organizational Coevolutionary Algorithm for Classification.pdf
- 西安电子科技大学:《大学计算机基础 Fundamentals of Computers》课程教学资源(PPT课件讲稿)Chapter 12,14,15 Lecture_Computer Software(2/2).ppt
- 西安电子科技大学:《大学计算机基础 Fundamentals of Computers》课程教学资源(PPT课件讲稿)Chapter 10-11 Lecture_Computer Software(1/2).ppt
- 西安电子科技大学:《数据结构与算法分析 Data Structure and Algorithm Analysis》课程教学资源(PPT课件讲稿)01 Lists, Stacks, and Queues.ppt
- 中国科学技术大学:Robust Speech Recognition with Speech Enhanced Deep Neural Networks.pdf
- 南京大学:《编译原理 Principles and Techniques of Compilers》课程教学电子教案(课件讲义)第九章 机器无关的优化.pdf
- 南京大学:《编译原理 Principles and Techniques of Compilers》课程教学电子教案(课件讲义)第八章 代码生成.pdf
- 南京大学:《编译原理 Principles and Techniques of Compilers》课程教学电子教案(课件讲义)第七章 运行时刻环境.pdf
- 南京大学:《编译原理 Principles and Techniques of Compilers》课程教学电子教案(课件讲义)第六章 中间代码生成.pdf
- 南京大学:《编译原理 Principles and Techniques of Compilers》课程教学电子教案(课件讲义)第五章 语法制导的翻译.pdf
- 南京大学:《编译原理 Principles and Techniques of Compilers》课程教学电子教案(课件讲义)第四章 语法分析.pdf
- 南京大学:《编译原理 Principles and Techniques of Compilers》课程教学电子教案(课件讲义)第三章 词法分析.pdf
- 南京大学:《编译原理 Principles and Techniques of Compilers》课程教学电子教案(课件讲义)第一章 引论(许畅).pdf
- 南京大学:《编译原理 Principles and Techniques of Compilers》课程教学电子教案(课件讲义)编译课程复习(许畅).pdf
- 南京大学:《编译原理 Principles and Techniques of Compilers》课程教学电子教案(实验讲义)实验一 词法分析与语法分析.pdf
- 南京大学:《编译原理 Principles and Techniques of Compilers》课程教学电子教案(实验讲义)实验二 语义分析.pdf
- 南京大学:《编译原理 Principles and Techniques of Compilers》课程教学电子教案(实验讲义)实验三 中间代码生成.pdf
- 南京大学:《编译原理 Principles and Techniques of Compilers》课程教学电子教案(实验讲义)实验四 目标代码生成.pdf
- 《编译原理 Principles and Techniques of Compilers》课程教学资源(学习资料)Assemblers,Linkers,and the SPIM Simulator(MIPS32 and SPIM).pdf
- 南京大学:《软件工程导论 Introduction to Software Engineering Research》课程教学电子教案(课件讲义)04 Conduct Rigorous and Scientific Research(Experiment Design in Software Engineering Research).pdf
- 华中科技大学:《面向对象程序设计》课程教学资源(课件讲稿)课程简介(李瑞轩).pdf
- 华中科技大学:《面向对象程序设计》课程教学资源(课件讲稿)第1章 引论(李瑞轩).pdf
- 华中科技大学:《面向对象程序设计》课程教学资源(课件讲稿)第2章 C++的变量、类型及函数.pdf