Presentation is loading. Please wait.

Presentation is loading. Please wait.

A Robust Data Structure

Similar presentations


Presentation on theme: "A Robust Data Structure"— Presentation transcript:

1 A Robust Data Structure
Binary Trees A Robust Data Structure Copyright Curt Hill

2 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 Curt Hill

3 A Tree 12 6 19 2 15 36 4 24 30 29 Copyright Curt Hill

4 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 Curt Hill

5 Search Although trees are recursive insertion and searching is simple
Not necessarily recursive Consider the next two searches Copyright Curt Hill

6 Search for 24 12 6 19 2 15 36 4 24 Found 30 29 Copyright Curt Hill

7 Search for 8 12 6 19 Not found 2 15 36 4 24 30 29 Copyright Curt Hill

8 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 Curt Hill

9 Building a Tree Add 12 6 2 4 19 12 6 19 2 4 Copyright Curt Hill

10 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 Curt Hill

11 A Tree 12 6 19 2 15 36 4 24 30 29 Copyright Curt Hill

12 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 Curt Hill

13 The Degenerate Case If the input to a tree is already sorted a list results Thus the input will result in: 1 3 5 8 Copyright Curt Hill

14 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 Curt Hill

15 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 Curt Hill

16 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 Curt Hill

17 Deletion Three cases Node has no children Node has one child
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 Curt Hill

18 Deleting a Leaf Delete 4 12 No real problem 6 19 2 15 36 4 24 30 29
4 24 30 29 Copyright Curt Hill

19 Deleting One Descendent Node
Delete 24 12 Promote 30 up 6 19 Also easy 2 15 36 4 24 30 29 Copyright Curt Hill

20 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 Curt Hill

21 Deleting Two Descendent Node
Delete 12 12 6 19 2 15 36 4 24 30 29 Copyright Curt Hill

22 Deleting Two Descendent Node
Delete 12 12 Find smallest of large 6 19 2 15 36 4 24 30 29 Copyright Curt Hill

23 Deleting Two Descendent Node
Delete 12 15 Find smallest of large Replace node to delete with found contents 6 19 2 15 36 4 24 30 29 Copyright Curt Hill

24 Deleting Two Descendent Node
Delete 12 15 Find smallest of large Replace node to delete with found contents 6 19 2 15 36 4 24 30 Start deletion routine with 19 to delete 15 29 Copyright Curt Hill

25 Deleting Two Descendent Node
Delete 12 15 Find smallest of large Replace node to delete with found contents 6 19 2 36 4 24 30 Start deletion routine with 19 to delete 15 Done 29 Copyright Curt Hill

26 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 Curt Hill

27 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 Curt Hill

28 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 Curt Hill

29 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 Curt Hill

30 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 Curt Hill

31 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 Curt Hill

32 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 © Curt Hill

33 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 © Curt Hill

34 Duplicate Possibilities
At leaves Adjacent 2 2 4 2 2 4 Copyright © Curt Hill

35 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 © Curt Hill

36 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 © Curt Hill

37 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 © Curt Hill

38 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 © Curt Hill

39 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 © Curt Hill

40 Array containing tree Index Slot 12 12 1 6 2 19 3 2 6 19 4 5 15 2 15
12 12 1 6 2 19 3 2 6 19 4 5 15 2 15 36 6 36 7 8 4 4 24 9 10 30 11 12 29 13 24 Copyright © Curt Hill

41 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 © Curt Hill

42 A Complete Tree 19 10 24 8 22 36 14 4 9 20 23 28 15 11 Copyright © Curt Hill

43 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 © Curt Hill


Download ppt "A Robust Data Structure"

Similar presentations


Ads by Google