Binary Trees.ppt
《Binary Trees.ppt》由会员分享,可在线阅读,更多相关《Binary Trees.ppt(40页珍藏版)》请在麦多课文档分享上搜索。
1、Binary Trees,CSC 220,Your Observations (so far data structures),Array Unordered Add, delete, search OrderedLinked List ?,Introduction to Tree,Fundamental data storage structures used in programming. Combines advantages of an ordered array and a linked list. Searching as fast as in ordered array. Ins
2、ertion and deletion as fast as in linked list.,Tree (example),node,edge,Draw a parse tree,Tree characteristics,Consists of nodes connected by edges. Nodes often represent entities (complex objects) such as people, car parts etc. Edges between the nodes represent the way the nodes are related. Its ea
3、sy for a program to get from one node to another if there is a line connecting them. The only way to get from node to node is to follow a path along the edges.,Tree Terminology,Path: Traversal from node to node along the edges results in a sequence called path. Root: Node at the top of the tree. Par
4、ent: Any node, except root has exactly one edge running upward to another node. The node above it is called parent. Child: Any node may have one or more lines running downward to other nodes. Nodes below are children. Leaf: A node that has no children. Subtree: Any node can be considered to be the r
5、oot of a subtree, which consists of its children and its childrens children and so on.,Tree Terminology,Visiting: A node is visited when program control arrives at the node, usually for processing. Traversing: To traverse a tree means to visit all the nodes in some specified order. Levels: The level
6、 of a particular node refers to how many generations the node is from the root. Root is assumed to be level 0. Keys: Key value is used to search for the item or perform other operations on it.,B-Tree,Binary Trees,Every node in a binary tree can have at most two children. The two children of each nod
7、e are called the left child and right child corresponding to their positions. A node can have only a left child or only a right child or it can have no children at all. Left child is always less that its parent, while right child is greater than its parent.,Applet,FinalAppletsChap08TreeTree.html,Unb
8、alanced Trees,Some trees can be unbalanced. They have most of their nodes on one side of the root or the other. Individual subtrees may also be unbalanced. Trees become unbalanced because of the order in which the data items are inserted. If the key values are inserted in ascending or descending ord
9、er the tree will be unbalanced. For search-centric application (Binary tree), an unbalanced tree must be re-balanced.,Is a binary tree ADT?,What are tree behaviors? Do they look familiar to other DS? Implantation details? Draw UML diagram for a B-Tree?,Representing Tree in Java,Similar to Linked Lis
10、t but with 2 Links Store the nodes at unrelated locations in memory and connect them using references in each node that point to its children. Can also be represented as an array, with nodes in specific positions stored in corresponding positions in the array.,Object with links,Array implementation,
11、Traversing the Tree,Visiting each node in a specified order. Three simple ways to traverse a tree: Inorder Preorder Postorder,Inorder Traversing,Inorder traversal will cause all the nodes to be visited in ascending order. Steps involved in Inorder traversal (recursion) are: - Call itself to traverse
12、 the nodes left subtree - Visit the node (e.g. display a key) - Call itself to traverse the nodes right subtree.,inOrder( node lroot) If (lroot != null) inOrder(lroot.leftChild();System.out.print(lroot.iData + “ “);inOrder(lroot.rightChild(); ,Tree Traversal (continued),Sequence of preorder traversa
13、l: e.g. use for infix parse tree to generate prefix - Visit the node - Call itself to traverse the nodes left subtree - Call itself to traverse the nodes right subtree Sequence of postorder traversal: e.g. use for infix parse tree to generate postfix - Call itself to traverse the nodes left subtree
14、- Call itself to traverse the nodes right subtree - Visit the node.,Finding a Node,To find a node given its key value, start from the root. If the key value is same as the node, then node is found. If key is greater than node, search the right subtree, else search the left subtree. Continue till the
15、 node is found or the entire tree is traversed. Time required to find a node depends on how many levels down it is situated, i.e. O(log N).,Inserting a Node,To insert a node we must first find the place to insert it. Follow the path from the root to the appropriate node, which will be the parent of
16、the new node. When this parent is found, the new node is connected as its left or right child, depending on whether the new nodes key is less or greater than that of the parent. What is the complexity?,Finding Maximum and Minimum Values,For the minimum, go to the left child of the root and keep goin
17、g to the left child until you come to a leaf node. This node is the minimum. For the maximum, go to the right child of the root and keep going to the right child until you come to a leaf node. This node is the maximum.,Deleting a Node,Start by finding the node you want to delete. Then there are thre
- 1.请仔细阅读文档,确保文档完整性,对于不预览、不比对内容而直接下载带来的问题本站不予受理。
- 2.下载的文档,不会出现我们的网址水印。
- 3、该文档所得收入(下载+内容+预览)归上传者、原创作者;如果您是本文档原作者,请点此认领!既往收益都归您。
下载文档到电脑,查找使用更方便
2000 积分 0人已下载
下载 | 加入VIP,交流精品资源 |
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- BINARYTREESPPT
