Tree Traversal Techniques; Heaps.ppt
《Tree Traversal Techniques; Heaps.ppt》由会员分享,可在线阅读,更多相关《Tree Traversal Techniques; Heaps.ppt(34页珍藏版)》请在麦多课文档分享上搜索。
1、CS 103,1,Tree Traversal Techniques; Heaps,Tree Traversal Concept Tree Traversal Techniques: Preorder, Inorder, Postorder Full Trees Almost Complete Trees Heaps,CS 103,2,Binary-Tree Related Definitions,The children of any node in a binary tree are ordered into a left child and a right child A node ca
2、n have a left anda right child, a left childonly, a right child only,or no children The tree made up of a leftchild (of a node x) and all itsdescendents is called the left subtree of x Right subtrees are defined similarly,10,CS 103,3,A Binary-tree Node Class,class TreeNode public: typedef int dataty
3、pe; TreeNode(datatype x=0, TreeNode *left=NULL, TreeNode *right=NULL)data=x; this-left=left; this-right=right; ;datatype getData( ) return data;TreeNode *getLeft( ) return left;TreeNode *getRight( ) return right;void setData(datatype x) data=x;void setLeft(TreeNode *ptr) left=ptr;void setRight(TreeN
4、ode *ptr) right=ptr; private:datatype data; / different data type for other appsTreeNode *left; / the pointer to left childTreeNode *right; / the pointer to right child ;,CS 103,4,Binary Tree Class,class Tree public: typedef int datatype; Tree(TreeNode *rootPtr=NULL)this-rootPtr=rootPtr;TreeNode *se
5、arch(datatype x);bool insert(datatype x); TreeNode * remove(datatype x);TreeNode *getRoot()return rootPtr;Tree *getLeftSubtree(); Tree *getRightSubtree();bool isEmpty()return rootPtr = NULL;private: TreeNode *rootPtr; ;,CS 103,5,Binary Tree Traversal,Traversal is the process of visiting every node o
6、nce Visiting a node entails doing some processing at that node, but when describing a traversal strategy, we need not concern ourselves with what that processing is,CS 103,6,Binary Tree Traversal Techniques,Three recursive techniques for binary tree traversal In each technique, the left subtree is t
7、raversed recursively, the right subtree is traversed recursively, and the root is visited What distinguishes the techniques from one another is the order of those 3 tasks,CS 103,7,Preoder, Inorder, Postorder,In Preorder, the rootis visited before (pre)the subtrees traversals In Inorder, the root isv
8、isited in-between left and right subtree traversal In Preorder, the rootis visited after (pre)the subtrees traversals,Preorder Traversal: Visit the root Traverse left subtree Traverse right subtree,Inorder Traversal: Traverse left subtree Visit the root Traverse right subtree,Postorder Traversal: Tr
9、averse left subtree Traverse right subtree Visit the root,CS 103,8,Illustrations for Traversals,Assume: visiting a nodeis printing its label Preorder: 1 3 5 4 6 7 8 9 10 11 12 Inorder:4 5 6 3 1 8 7 9 11 10 12 Postorder:4 6 5 3 8 11 12 10 9 7 1,CS 103,9,Illustrations for Traversals (Contd.),Assume: v
10、isiting a nodeis printing its data Preorder: 15 8 2 6 3 711 10 12 14 20 27 22 30 Inorder: 2 3 6 7 8 10 1112 14 15 20 22 27 30 Postorder: 3 7 6 2 10 1412 11 8 22 30 27 20 15,CS 103,10,Code for the Traversal Techniques,The code for visitis up to you toprovide, dependingon the application A typical exa
11、mplefor visit() is toprint out the data part of its inputnode,void inOrder(Tree *tree)if (tree-isEmpty( ) return;inOrder(tree-getLeftSubtree( );visit(tree-getRoot( );inOrder(tree-getRightSubtree( ); ,void preOrder(Tree *tree)if (tree-isEmpty( ) return;visit(tree-getRoot( );preOrder(tree-getLeftSubtr
12、ee();preOrder(tree-getRightSubtree(); ,void postOrder(Tree *tree)if (tree-isEmpty( ) return;postOrder(tree-getLeftSubtree( );postOrder(tree-getRightSubtree( );visit(tree-getRoot( ); ,CS 103,11,Application of Traversal Sorting a BST,Observe the output of the inorder traversal of the BST example two s
13、lides earlier It is sorted This is no coincidence As a general rule, if you output the keys (data) of the nodes of a BST using inorder traversal, the data comes out sorted in increasing order,CS 103,12,Other Kinds of Binary Trees (Full Binary Trees),Full Binary Tree: A full binary tree is a binary t
14、ree where all the leaves are on the same level and every non-leaf has two children The first four full binary trees are:,CS 103,13,Examples of Non-Full Binary Trees,These trees are NOT full binary trees: (do you know why?),CS 103,14,Canonical Labeling of Full Binary Trees,Label the nodes from 1 to n
15、 from the top to the bottom, left to right,Relationships between labels of children and parent:,2i,2i+1,i,CS 103,15,Other Kinds of Binary Trees (Almost Complete Binary trees),Almost Complete Binary Tree: An almost complete binary tree of n nodes, for any arbitrary nonnegative integer n, is the binar
16、y tree made up of the first n nodes of a canonically labeled full binary,1,1,2,1,2,3,4,5,6,7,1,2,1,2,3,4,5,6,1,2,3,4,1,2,3,4,5,CS 103,16,Depth/Height of Full Trees and Almost Complete Trees,The height (or depth ) h of such trees is O(log n) Proof: In the case of full trees, The number of nodes n is:
17、 n=1+2+22+23+2h=2h+1-1 Therefore, 2h+1 = n+1, and thus, h=log(n+1)-1 Hence, h=O(log n) For almost complete trees, the proof is left as an exercise.,CS 103,17,Canonical Labeling of Almost Complete Binary Trees,Same labeling inherited from full binary trees Same relationship holding between the labels
- 1.请仔细阅读文档,确保文档完整性,对于不预览、不比对内容而直接下载带来的问题本站不予受理。
- 2.下载的文档,不会出现我们的网址水印。
- 3、该文档所得收入(下载+内容+预览)归上传者、原创作者;如果您是本文档原作者,请点此认领!既往收益都归您。
下载文档到电脑,查找使用更方便
2000 积分 0人已下载
下载 | 加入VIP,交流精品资源 |
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- TREETRAVERSALTECHNIQUESHEAPSPPT
