当前位置:高等教育资讯网  >  中国高校课件下载中心  >  大学文库  >  浏览文档

《数据结构》课程教学资源(PPT讲稿)二叉树和二叉搜索树 Trees, Binary Trees, and Binary Search Trees

资源类别:文库,文档格式:PPT,文档页数:63,文件大小:2.36MB,团购合买
点击下载完整版文档(PPT)

Trees, Binary Trees and Binary Search Trees

Trees, Binary Trees, and Binary Search Trees

Trees Linear access time of linked lists is prohibitive Does there exist any simple data structure for which the running time of most operations(search insert, delete) is o(log N)? Trees Basic concepts Tree traversal Binary tree Binary search tree and its operations

2 Trees Linear access time of linked lists is prohibitive Does there exist any simple data structure for which the running time of most operations (search, insert, delete) is O(log N)? Trees Basic concepts Tree traversal Binary tree Binary search tree and its operations

Tr ees a tree T is a collection of nodes T can be empty (recursive definition) If not empty, a tree T consists of c a(distinguished)node r( the root B and zero or more nonempty sub-trees T1, T2, .. Tk root T1 T T3 T4 T Figure 4.1 Generic tree

3 Trees A tree T is a collection of nodes T can be empty (recursive definition) If not empty, a tree T consists of  a (distinguished) node r (the root),  and zero or more nonempty sub-trees T1 , T2 , ...., Tk

Tree can be viewed as a nested lists list of lists of lists Tree is also a graph

4 Tree can be viewed as a ‘nested’ lists: list of lists of lists … Tree is also a graph …

Some terminologies Figure 4.2 A tree Child and Parent Every node except the root has one parent a node can have a zero or more children Leaves Leaves are nodes with no children Sibling nodes with same parent

5 Some Terminologies Child and Parent Every node except the root has one parent A node can have a zero or more children Leaves Leaves are nodes with no children Sibling nodes with same parent

6 More Terminologies Path A sequence of edges Length of a path number of edges on the path Depth of a node length of the unique path from the root to that node Height of a node length of the longest path from that node to a leaf all leaves are at height 0 The height of a tree= the height of the root the depth of the deepest leaf Ancestor and descendant If there is a path from n1 to n2 Proper ancestor and proper descendant of n1 n1 is an ancestor of n2. n2 is a descendant

6 More Terminologies Path A sequence of edges Length of a path number of edges on the path Depth of a node length of the unique path from the root to that node Height of a node length of the longest path from that node to a leaf all leaves are at height 0 The height of a tree = the height of the root = the depth of the deepest leaf Ancestor and descendant If there is a path from n1 to n2 n1 is an ancestor of n2, n2 is a descendant of n1 Proper ancestor and proper descendant

Example: UNIX Directory fuss mark* alex* bill* book course junk junk work* course+ chI.r ch2. r ch3. r cop3530* cop3212 fal198* spr99* sum99* fa!98* fa99米 yl.T syl. r grades progl r prog2 r prog2 r progl r grades Figure 4.5 UNIX directory

7 Example: UNIX Directory

Tree Operations Traversal, the most important we will not implement a general tree, so wont discuss Search Insertion Deletion

8 Tree Operations Traversal, the most important we will not implement a general ‘tree’, so wont discuss Search Insertion Deletion

Tree Traversal Used to print out the data in a tree in a certain order Pre-order traversal Print the data at the root Recursively print out all data in the leftmost subtree Recursively print out all data in the rightmost subtree

9 Tree Traversal Used to print out the data in a tree in a certain order Pre-order traversal Print the data at the root Recursively print out all data in the leftmost subtree … Recursively print out all data in the rightmost subtree

A drawing of tree with two pointers Struct TreeNode i double elementi // the data TreeNode* child; / FIRST child go to the next generation TreeNode* next;// next SIBLING: go to the same generation

10 A drawing of tree with two pointers … Struct TreeNode { double element; // the data TreeNode* child; // FIRST child : go to the next generation TreeNode* next; // next SIBLING : go to the same generation }

点击下载完整版文档(PPT)VIP每日下载上限内不扣除下载券和下载次数;
按次数下载不扣除下载券;
24小时内重复下载只扣除一次;
顺序:VIP每日次数-->可用次数-->下载券;
共63页,可试读20页,点击继续阅读 ↓↓
相关文档

关于我们|帮助中心|下载说明|相关软件|意见反馈|联系我们

Copyright © 2008-现在 cucdc.com 高等教育资讯网 版权所有