* Your assessment is very important for improving the work of artificial intelligence, which forms the content of this project
Download Variables Storage, Type, Declaration.
Survey
Document related concepts
Transcript
Binary Trees A Robust Data Structure Copyright 2004-2006 Curt Hill Characteristics • Very robust data structure • Good performance, usually • Used for searching – Especially when what we are searching for varies dynamically • Handles additions and deletions better than most other searching structures • Always sorted Copyright 2004-2006 Curt Hill A Tree 12 6 19 2 0 15 4 36 24 30 29 Copyright 2004-2006 Curt Hill Notes • • • • Left side is less than node Right side is greater A leaf has no descendents Interior nodes may have one or two descendents • This particular tree is not balanced • The shape of the tree is based on the arrival order of the items Copyright 2004-2006 Curt Hill Search • Although trees are recursive insertion and searching is simple • Not necessarily recursive • Consider the next two searches Copyright 2004-2006 Curt Hill Search for 24 12 6 19 2 0 15 4 36 Found 24 30 29 Copyright 2004-2006 Curt Hill Search for 8 12 6 2 0 19 Not found 15 4 36 24 30 29 Copyright 2004-2006 Curt Hill Searching • • • • The process is a simple iteration Start with root node as the pointer If search item equal node then done else if search item greater node then set pointer to right sub-tree • Else set pointer to left sub-tree • If pointer is null quit Copyright 2004-2006 Curt Hill Building a Tree Add 12 12 6 6 19 2 4 19 2 4 Copyright 2004-2006 Curt Hill Notes on Above • For each item to be inserted into a tree there is only one place in the tree where it may be inserted • No node is moved • Painless insertion similar to a list • The finding of the correct location is much faster than that of a list • Consider the following tree Copyright 2004-2006 Curt Hill A Tree 12 6 19 2 0 15 4 36 24 30 29 Copyright 2004-2006 Curt Hill Construction • Any given tree can be constructed by a large number of orderings of the input • The 12 must be first • The 6 or 19 must come second • The 6 must precede the 0, 2, 4 • Any interior node must precede anything below it Copyright 2004-2006 Curt Hill The Degenerate Case • If the input to a tree is already sorted a list results • Thus the input 1 3 5 8 will result in: 1 3 5 Copyright 2004-2006 Curt Hill 8 Insertion (non-recursive) • General strategy – – – – Search for position Keep a trailing pointer Insert it Always insert at end • The code: – Two special cases – Root NULL – All other cases Copyright 2004-2006 Curt Hill Insertion (recursive) • General strategy – Search for position – If key found report failure – When root is nil: • Insert it • The parameters: – The pointer by reference – Value to insert • The code: – Only one case Copyright 2004-2006 Curt Hill Observations • Even though a search is eminently loopable • Then recursive version is much shorter • Why is this so much shorter? – Has the built in stack – Procedure call overhead – Simplifies the cases Copyright 2004-2006 Curt Hill Deletion • Three cases • Node has no children – Easy • Node has one child – Similar to linked list deletion • Node has two children – Find the highest of the lower or lowest of higher – Replace current node with that one • Delete the one that replaced Copyright 2004-2006 Curt Hill Deleting a Leaf Delete 4 12 No real problem 6 19 2 0 15 4 36 24 30 29 Copyright 2004-2006 Curt Hill Deleting One Descendent Node Delete 24 12 Promote 30 up 6 19 Also easy 2 0 15 4 36 24 30 29 Copyright 2004-2006 Curt Hill Deleting Two Descendent Node • This is the real tricky one • The two sub-trees cannot both be promoted • The trick is to find one of these: – Largest of the smaller sub-tree – Smallest of the larger sub-tree • Delete that one and substitute its contents for deleted node • Both of these are easy to delete cases Copyright 2004-2006 Curt Hill Deleting Two Descendent Node Delete 12 12 6 19 2 0 15 4 36 24 30 29 Copyright 2004-2006 Curt Hill Deleting Two Descendent Node Delete 12 12 Find smallest of large 6 19 2 0 15 4 36 24 30 29 Copyright 2004-2006 Curt Hill Deleting Two Descendent Node Delete 12 15 Find smallest of large Replace node to delete with found contents 2 0 6 19 15 4 36 24 30 29 Copyright 2004-2006 Curt Hill Deleting Two Descendent Node Delete 12 15 Find smallest of large Replace node to delete with found contents 2 0 6 19 15 4 36 24 30 Start deletion routine with 19 to delete 15 29 Copyright 2004-2006 Curt Hill Deleting Two Descendent Node Delete 12 15 Find smallest of large Replace node to delete with found contents 2 0 6 19 36 4 Start deletion routine with 19 to delete 15 Done Copyright 2004-2006 Curt Hill 24 30 29 Deletion Notes • The smallest of larger or largest of smaller have the following two properties which make this work • They are adjacent to node to delete – Swapping them with node does not affect ordering of tree • They may only have one or zero subtrees – The delete is easy Copyright 2004-2006 Curt Hill Deletion Recursion • This is often a recursive routine, but not in the normal way • Usually search for the item to delete iteratively • If zero or one descendent delete without recursion • If two descendents find the replacement node (also iteratively) and replace the node • Then recursively call delete routine to remove item swapped – Do not start at root, but at descendent Copyright 2004-2006 Curt Hill Traversal • Traversing a tree means to touch each node in some order • Since a tree is ordered the two obvious orders are left to right (ascending) and right to left (descending) • Another presentation with deal with this and other possibilities Copyright 2004-2006 Curt Hill Recursive Traversal • Generally traversal involves a recursive routine • The routine has a pointer and function parameter • The routine calls the function upon arriving at each node Copyright 2004-2006 Curt Hill Iterative Traversal • A tree may be iteratively traversed if the traversal function/method has access to an auxiliary data structure • When entering a node three things need to be done: – – – – Process the node Store the left subtree for future processing Store the right subtree for future processing Get the next node to process • The order of these three actions determine the traversal order – This is true of recursive traversal as well Copyright 2004-2006 Curt Hill Iterators • An interator is just an iterative traversal • The class contains the auxiliary data structure • Has normal iterator methods: – Start – Next (or previous) – Done • Must also do the handshaking with the tree class Copyright 2004-2006 Curt Hill Duplicates • Trees are often used to represent a set – No duplicates are allowed – The insert should return a boolean stating whether insertion was successful • If this is not the case, how should duplicates be handled? – In the tree – Out of the tree Copyright © 2004-2010 Curt Hill In the Tree • Duplicates must be added on the left or right • There are at least two ways to do this • Inserting at leaves • Inserting adjacent to original • This will complicate several routines • The whole question of how to tell the difference of a duplicate is raised? Copyright © 2004-2010 Curt Hill Duplicate Possibilities At leaves Adjacent 2 2 0 4 0 2 2 4 Copyright © 2004-2010 Curt Hill Problems • Adjacent makes deletion, insertion easy • The insertion of a linked list reduces performance – Duplicates never have more than one descendent • Inserting at leaves has easy insertion, but complicates finding the duplicates Copyright © 2004-2010 Curt Hill Out of the Tree • Root a linked list at the tree node • This will contain only duplicates • This is now a three way tree – Lesser – Greater – Equal • Sometimes makes deletion somewhat easier • Often the preferred alternative Copyright © 2004-2010 Curt Hill Trees in Arrays • Older languages, such as FORTRAN, used trees inside arrays – FORTRAN had only the array as a data structure • This can be accomplished in two ways: • Use a subscript to refer to each descendent • Implied location of descendents Copyright © 2004-2010 Curt Hill Subscripted descendents • Each tree node has two integers to represent the two descendents • The integer must be in the array range if the sub-tree existed • An invalid value, such as -1, indicated no descendent • Not particularly valuable in a language that allows handles or pointers Copyright © 2004-2010 Curt Hill Implied locations • There is no room in the object for descendents • The root of the tree is always in the first element – subscript zero • The left descendent is at current index times 2 plus 1 and right is at current times 2 plus 2 Copyright © 2004-2010 Curt Hill Index 0 1 2 3 4 5 6 7 8 9 10 11 12 13 Array containing tree Slot 12 6 19 2 15 36 0 4 12 6 19 2 0 15 4 36 24 30 24 29 Copyright © 2004-2010 Curt Hill Waste Space • Implied locations can be wasteful if the tree does not have all of its leaves at the bottom two levels • A tree is termed complete if all of its leaves are at the bottom two levels • Only the rightmost entries of the bottom row may be absent • This type of tree may be stored efficiently in an array Copyright © 2004-2010 Curt Hill A Complete Tree 19 10 24 8 4 22 14 9 11 15 20 Copyright © 2004-2010 Curt Hill 36 23 28 Finally • The previous tree could be stored in an array with no empty slots except at the end • Trees in arrays are usually only used in primitive languages without handles or pointers • Heaps and heapsort does use an array based tree and these are usually complete Copyright © 2004-2010 Curt Hill