Bushy Binary Search Tree from Ordered List. Behavior of the Algorithm Binary Search Tree Recall that tree_search is based closely on binary search. If.

Slides:



Advertisements
Similar presentations
Heuristic Search techniques
Advertisements

Binary Trees CSC 220. Your Observations (so far data structures) Array –Unordered Add, delete, search –Ordered Linked List –??
Chapter 4: Trees Part II - AVL Tree
AVL Trees COL 106 Amit Kumar Shweta Agrawal Slide Courtesy : Douglas Wilhelm Harder, MMath, UWaterloo
Binary Searching.
B+-Trees (PART 1) What is a B+ tree? Why B+ trees? Searching a B+ tree
Discrete Structure Li Tak Sing( 李德成 ) Lectures
Balanced Search Trees. 2-3 Trees Trees Red-Black Trees AVL Trees.
Chapter 19: Searching and Sorting Algorithms
A balanced life is a prefect life.
CS Data Structures Chapter 10 Search Structures (Selected Topics)
Tirgul 10 Rehearsal about Universal Hashing Solving two problems from theoretical exercises: –T2 q. 1 –T3 q. 2.
Tirgul 5 AVL trees.
BST Data Structure A BST node contains: A BST contains
FALL 2006CENG 351 Data Management and File Structures1 External Sorting.
Tirgul 6 B-Trees – Another kind of balanced trees Problem set 1 - some solutions.
Chapter 12 Trees. Copyright © 2005 Pearson Addison-Wesley. All rights reserved Chapter Objectives Define trees as data structures Define the terms.
Chapter 10 Search Structures Instructors: C. Y. Tang and J. S. Roger Jang All the material are integrated from the textbook "Fundamentals of Data Structures.
Data Structures Using C++ 2E Chapter 11 Binary Trees and B-Trees.
A Binary Search Tree Implementation Chapter 25 Copyright ©2012 by Pearson Education, Inc. All rights reserved.
C++ Programming: Program Design Including Data Structures, Third Edition Chapter 20: Binary Trees.
Database Management Systems, R. Ramakrishnan and J. Gehrke1 Tree-Structured Indexes Chapter 9.
Tree-Structured Indexes. Range Searches ``Find all students with gpa > 3.0’’ –If data is in sorted file, do binary search to find first such student,
Binary Trees Chapter 6.
CPSC 335 BTrees Dr. Marina Gavrilova Computer Science University of Calgary Canada.
1 HEAPS & PRIORITY QUEUES Array and Tree implementations.
IntroductionIntroduction  Definition of B-trees  Properties  Specialization  Examples  2-3 trees  Insertion of B-tree  Remove items from B-tree.
Chapter Tow Search Trees BY HUSSEIN SALIM QASIM WESAM HRBI FADHEEL CS 6310 ADVANCE DATA STRUCTURE AND ALGORITHM DR. ELISE DE DONCKER 1.
Tree.
Advanced Algorithms Analysis and Design Lecture 8 (Continue Lecture 7…..) Elementry Data Structures By Engr Huma Ayub Vine.
1 COP 3538 Data Structures with OOP Chapter 8 - Part 2 Binary Trees.
Chapter 19: Binary Trees. Objectives In this chapter, you will: – Learn about binary trees – Explore various binary tree traversal algorithms – Organize.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 19: Searching and Sorting Algorithms.
Multiway Trees. Trees with possibly more than two branches at each node are know as Multiway trees. 1. Orchards, Trees, and Binary Trees 2. Lexicographic.
AVL Trees Amanuel Lemma CS252 Algoithms Dec. 14, 2000.
A Review of Binary Search Trees Dr. Gang Qian Department of Computer Science University of Central Oklahoma.
CS Data Structures Chapter 10 Search Structures.
Chapter 6 Binary Trees. 6.1 Trees, Binary Trees, and Binary Search Trees Linked lists usually are more flexible than arrays, but it is difficult to use.
Data Structures and Algorithms Lecture (BinaryTrees) Instructor: Quratulain.
Analysis of Red-Black Tree Because of the rules of the Red-Black tree, its height is at most 2log(N + 1). Meaning that it is a balanced tree Time Analysis:
Data Structures Balanced Trees 1CSCI Outline  Balanced Search Trees 2-3 Trees Trees Red-Black Trees 2CSCI 3110.
Discrete Structures Lecture 12: Trees Ji Yanyan United International College Thanks to Professor Michael Hvidsten.
1 Chapter 10 Trees. 2 Definition of Tree A tree is a set of linked nodes, such that there is one and only one path from a unique node (called the root.
CMSC 341 B- Trees D. Frey with apologies to Tom Anastasio.
Adapted from Mike Franklin
2-3 Tree. Slide 2 Outline  Balanced Search Trees 2-3 Trees Trees.
Binary Tree. Contiguous versus Linked List Insertion in Contiguous list needs a lot of move. For big chunks of records it is time consuming. Linked List.
Binary Search Tree vs. Balanced Search Tree. Why care about advanced implementations? Same entries, different insertion sequence: 10,20,30,40,50,60,70,
Chapter 13 A Advanced Implementations of Tables. © 2004 Pearson Addison-Wesley. All rights reserved 13 A-2 Balanced Search Trees The efficiency of the.
File Organization and Processing Week Tree Tree.
Priority Queues and Heaps. October 2004John Edgar2  A queue should implement at least the first two of these operations:  insert – insert item at the.
Chapter 7 Trees_Part3 1 SEARCH TREE. Search Trees 2  Two standard search trees:  Binary Search Trees (non-balanced) All items in left sub-tree are less.
UNIT 5.  The related activities of sorting, searching and merging are central to many computer applications.  Sorting and merging provide us with a.
Week 10 - Friday.  What did we talk about last time?  Graph representations  Adjacency matrix  Adjacency lists  Depth first search.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 B+-Tree Index Chapter 10 Modified by Donghui Zhang Nov 9, 2005.
AVL Tree.
BINARY TREES Objectives Define trees as data structures Define the terms associated with trees Discuss tree traversal algorithms Discuss a binary.
1 Binary Search Trees  Average case and worst case Big O for –insertion –deletion –access  Balance is important. Unbalanced trees give worse than log.
8/3/2007CMSC 341 BTrees1 CMSC 341 B- Trees D. Frey with apologies to Tom Anastasio.
Chapter 11. Chapter Summary  Introduction to trees (11.1)  Application of trees (11.2)  Tree traversal (11.3)  Spanning trees (11.4)
CSC317 1 x y γ β α x y γ β x β What did we leave untouched? α y x β.
File Organization and Processing Week 3
AVL Tree.
Binary Search Tree Chapter 10.
Data Structures Balanced Trees CSCI
Data Structures and Programming Techniques
Applied Combinatorics, 4th Ed. Alan Tucker
Binary Search Trees.
Chapter 20: Binary Trees.
1 Lecture 13 CS2013.
Presentation transcript:

Bushy Binary Search Tree from Ordered List

Behavior of the Algorithm Binary Search Tree Recall that tree_search is based closely on binary search. If we apply binary search to an ordered list and draw its comparison tree, then we see that binary search does exactly the same comparisons as tree_search will do if it is applied to this same tree. We already know from Section 7.4 that binary search performs O(log n) comparisons for a list of length n. This performance is excellent in comparison to other methods, since log n grows very slowly as n increases.

an example Suppose, as an example, that we apply binary search to the list of seven letters a, b, c, d, e, f, and g. The resulting tree is shown in part (a) of Figure If tree_search is applied to this tree, it will do the same number of comparisons as binary search.

The Binary Search Tree

It is quite possible, however, that the same letters may be built into a binary search tree of a quite different shape, such as any of those shown in the remaining parts of Figure 10.8.

The Binary Search Tree Class The tree shown as part (a) of Figure 10.8 is the best possible for searching. It is as “bushy” as possible: It has the smallest possible height for a given number of vertices.

The Binary Search Tree Class In part (c) of Figure 10.8, however, the tree has degenerated quite badly, so that a search for target c requires six comparisons. In parts (d) and (e), the tree reduces to a single chain. tree_search, when applied to such a chain, degenerates to sequential search.

Goal: Start with an ordered list and build its entries into a binary search tree that is nearly balanced (“as bushy as possible”).

Building A Binary Search Tree When the number of entries, n, is 31, for example, we wish to build the tree of Figure

Building A Binary Search Tree In Figure the entries are numbered in their natural order, that is, in inorder sequence, which is the order in which they will be received and built into the tree, since they are received in sorted order. We will also use this numbering to label the nodes of the tree.

An important property of the labels If you examine the diagram for a moment, you may notice an important property of the labels. The labels of the leaves are all odd numbers; that is, they are not divisible by 2. The labels of the nodes one level above the leaves are 2, 6, 10, 14, 18, 22, 26, and 30. These numbers are all double an odd number; that is, they are all even (divisible by 2 = 2 1 ), but are not divisible by 4. On the next level up, the labels are 4, 12, 20, and 28, numbers that are divisible by 4 = 2 2, but not by 8. Finally, the nodes just below the root are labeled 8 and 24 (divisible by 8 = 2 3 ), and the root itself is 16 (divisible by 16 = 2 4 ). The crucial observation is: If the nodes of a complete binary tree are labeled in inorder sequence, starting with 1, then each node is exactly as many levels above the leaves as the highest power of 2 that divides its label.

one more constraint Let us now put one more constraint on our problem: Let us suppose that we do not know in advance how many entries will be built into the tree. If the entries are coming from a file or a linked list, then this assumption is quite reasonable, since we may not have any convenient way to count the entries before receiving them.

one more constraint This assumption also has the advantage that it will stop us from worrying about the fact that, when the number of entries is not exactly one less than a power of 2, the resulting tree will not be complete and cannot be as symmetrical as the one in Figure Instead, we shall design our algorithm as though it were completely symmetrical, and after receiving all entries we shall determine how to tidy up the tree.

Getting Started There is no doubt what to do with entry number 1 when it arrives. It will be placed in a leaf node whose left and right pointers should both be set to NULL. Node number 2 goes above node 1, as shown in Figure Since node 2 links to node 1, we obviously must keep some way to remember where node 1 is until entry 2 arrives. Node 3 is again a leaf, but it is in the right subtree of node 2, so we must remember a pointer to node 2.

keep a list of pointers Does this mean that we must keep a list of pointers to all nodes previously processed, to determine how to link in the next one? The answer is no, since when node 2 is added, all connections for node 1 are complete. Node 2 must be remembered until node 4 is added, to establish the left link from node 4, but then a pointer to node 2 is no longer needed. Similarly, node 4 must be remembered until node 8 has been processed. In Figure 10.13, colored arrows point to each node that must be remembered as the tree grows.

keep a list of pointers It should now be clear that to establish future links, we need only remember pointers to one node on each level, the last node processed on that level. We keep these pointers in a List called last_node that will be quite small. For example, a tree with 20 levels (hence 20 entries in last_node) can accommodate > 1,00 0,000 nodes.

Finishing the Task Finally, we must determine how to tie in any subtrees that may not yet be connected properly after all the nodes have been received.

Finishing the Task For example, if n … 21, we must connect the three components shown in Figure into a single tree.

Finishing the Task Some nodes in the upper part of the tree may still have their right links set to NUL L, even though further nodes have been inserted that now belong in their right subtrees. Any one of these nodes (a node, not a leaf, for which the right child is still NULL) will be one of the nodes in the list last_node. For n … 21, these will be nodes 16 and 20 (in positions 5 and 3 of last_node, respectively), as shown in Figure

determine the highest node in last_node that is not already in the left subtree The pointer lower_node can be determined as the highest node in last_node that is not already in the left subtree of high_nod e. To determine whether a node is in the left subtree, we need only compare its key with that of high_nod e.

Evaluation The algorithm of this section produces a binary search tree that is not always completely balanced. If the tree has 31 nodes, then it will be completely balanced, but if 32 nodes come in, then node 32 will become the root of the tree, and all 31 remaining nodes will be in its left subtree. In this case, the leaves are five steps away from the root. If the root were chosen optimally, then most of the leaves would be four steps from it, and only one would be five steps. Hence one comparison more than necessary will usually be done in the tree with 32 nodes.

Evaluation One extra comparison in a binary search is not really a very high price, and it is easy to see that a tree produced by our method is never more than one level away from optimality. There are sophisticated methods for building a binary search tree that is as balanced as possible, but much remains to recommend a simpler method, one that does not need to know in advance how many nodes are in the tree.

Random Search Trees and Optimality To conclude this section, let us ask whether it is worthwhile, on average, to keep a binary search tree balanced or to rebalance it. If we assume that the keys have arrived in random order, then, on average, how many more comparisons are needed in a search of the resulting tree than would be needed in a completely balanced tree?

The average number of nodes visited

Evaluation In other words, the average cost of not balancing a binary search tree is approximately 39 percent more comparisons. In applications where optimality is important, this cost must be weighed against the extra cost of balancing the tree, or of maintaining it in balance. Note especially that these latter tasks involve not only the cost of computer time, but the cost of the extra programming effort that will be required.