CS 2412 Data Structures. Chapter 8 Multiway Trees

Size: px
Start display at page:

Download "CS 2412 Data Structures. Chapter 8 Multiway Trees"

Transcription

1 CS 2412 Data Structures Chapter 8 Multiway Trees

2 This chapter studies multiway trees. These trees can be used for external search. When searched data is big, it is not suitable to load all the data to memory. Search in high-speed memory is much faster than search in external devices (hard discs, CD etc.) The idea of external search: each time read in a block of information to the memory and decide what is the next block we should search on. Data Structure 2015 R. Wei 2

3 Definition An m-way tree has the following properties. Each node has 0 to m subtrees. A node with k < m subtrees contains k subtrees and k 1 data entries. The keys of the data entries are ordered: key 1 key 2 key k 1. The key values in the first subtree (0th subtree) are all less than key 1 ; the key values in the ith subtrees are all greater than or equal to key i but less than key i+1. All subtrees are themselves multiway trees. A 2-way tree is a BST. Data Structure 2015 R. Wei 3

4 Data Structure 2015 R. Wei 4

5 Data Structure 2015 R. Wei 5

6 We also want the multisay tree to be balance. Definition A B-tree is an m-way tree with the following additional properties: The root is either a leaf or it has 2 to m subtrees. All internal nodes have at least m/2 nonnull subtrees. All leaf nodes are at the same level. A leaf node has at least m/2 1 entries. Data Structure 2015 R. Wei 6

7 Data Structure 2015 R. Wei 7

8 Data structure of B-tree. The structure of m-way tree: an entry of a node contains data and a pointer to its right subtree. A node contains the first pointer to the subtree with entries less than the key of the first entry, a count of the number of entries currently in the node, and an array of entries. The array can be of size m. Main operations for B-trees are: insert, delete, traverse and search. Data Structure 2015 R. Wei 8

9 Data Structure 2015 R. Wei 9

10 B-tree insertion: B-tree insertion takes place at a leaf node. Locate the leaf node where the data can be inserted. If the node is not full (has less than m 1 entries), insert the data to this node. If the node is full (called overflow condition), split the node into two node. A B-tree grows from the bottom up. Data Structure 2015 R. Wei 10

11 Example Insert 11, 21, 14, 78, 97 to a B-tree of order 5: Data Structure 2015 R. Wei 11

12 The algorithm of B-tree insert: If the B-tree is empty, then create the root and insert the first entry. If the B-tree is not empty, call the insert node algorithm which finds the location, insert it and do necessary update (if overflow, then split and install the median entry to the parent etc.). If the root needs to split, then create a new root. Data Structure 2015 R. Wei 12

13 Algorithm BTreeInsert( tree, data) if (tree empty) create new node set left subtree of node to null move data to first entry in new node set subtree of first entry to null set tree root to address of new node set number of entries to 1 else insertnode(tree, data, upentry) end if if (tree higher) create new node move upentry to first entry in new node set left subtree of the new node to tree set tree root to new node set number of entries to 1 end if Data Structure 2015 R. Wei 13

14 Data Structure 2015 R. Wei 14

15 Data Structure 2015 R. Wei 15

16 Algorithm searchnode(nodeptr, target) if (target < key in first entry) return 0 end if set walker to number of entries -1 loop (targer < entry key[walker]) decrement walker end loop return walker This function returns the index to entry with key target, or 0 if the key < first entry in node. Data Structure 2015 R. Wei 16

17 Algorithm splitnode (node, entryndx, newentrylow, upentry) create new node move high entries to new node if (entryndx < minimum entries) inset upentry in new node end if move median data to upentry make new node first Ptr the right subtree of median data make new node the right subtree of upentry Data Structure 2015 R. Wei 17

18 Data Structure 2015 R. Wei 18

19 Data Structure 2015 R. Wei 19

20 Data Structure 2015 R. Wei 20

21 Data Structure 2015 R. Wei 21

22 B-tree deletion: B-tree deletion is a little more complicated than insertion. Search for the data to be deleted. If can t find, then print an error message and quit. If the data is found, then delete the data. Two cases need to consider: the data at leaf node or non-leaf node. If an underflow (a leaf node has less than m/2 1 entries or an internal node has less than m/2 nonull subtrees) occurred after the data deletion, then adjustment must be done. Data Structure 2015 R. Wei 22

23 The following algorithm delete an entry. Some situations are considered: empty tree, the root is empty after deletion. Leave the details about how to treat underflow to algorithm delete. Algorithm BTreeDelete (tree, dltkey) if (tree empty) return false end if delete (tree, dltkey, success) if (success) if(tree number of entries zero) set tree to left subtree end if end if return success Data Structure 2015 R. Wei 23

24 Data Structure 2015 R. Wei 24

25 6 end if 6 end if 7 return underflow end delete Data Structure 2015 R. Wei 25

26 The following algorithm deletes the entry from a leaf node and returns the value of underflow. Algorithm deleteentry (node, entryndx) delete entry at entryndx from node shift entries after delete to left if (number of entries less minimum, entries) return true else retrun false end if Data Structure 2015 R. Wei 26

27 When deleting an entry in an internal node, we must find substitute data. We use the immediate predecessor, which is the largest node on the left subtree of the entry to be deleted. In the subtree, the largest node is the rightmost subtree. Algorithm deletemid (node, entryndx, subtree) if (no rightmost subtree) //predecessor in a leaf node move predecessor s data to deleted entry set underflow if node entries less minimum else set underflow to deletemid(node, entryndx, right subtree) if (underflow) set underflow to reflow(root, entryndx) end if end if return underflow Data Structure 2015 R. Wei 27

28 When a node is underflow, we need to do some adjustment which we call reflow. Suppose one of the subtree contains unerflow node, two situations need to consider: If the other subtree has more entries than the minimum number, than we just move some entry to the underflow node, which we call it balance. If the other subtree only has minimum number of entries, then we need to combine two node to one node together with the root entry. This is called combine. Data Structure 2015 R. Wei 28

29 Algorithm reflow (root, entryndx) if (righttree entries greater minimum entries) borrowright (root, entryndx, lefttree, righttree) set underflow to false else if (lefttree entries greater minimum entries) borrowleft (root, entryndx, lefttree, righttree) set underflow to false else combine (root, entryndx, lefttree, righttree) if (root numentries less minimum entries) set underflow to true else set underflow to false end if end if return underflow Data Structure 2015 R. Wei 29

30 Data Structure 2015 R. Wei 30

31 Algorithm borrowleft(root, entryndx, left, right) shift all elements one to the right move root data to first entry in right move right first pointer to right subtree of first entry move left last right pointer to right first pointer move left last entry data to root at entryndx In above algorithm, when an entry is moved the according pointers are also adjusted. (To see that, consider the underflow node is not a leaf node). The algorithm of borrowright is similar. Data Structure 2015 R. Wei 31

32 Data Structure 2015 R. Wei 32

33 Algorithm combine (root, entryndx, left, right) move parent entry to first open entry in left subtree move right subtree first subtree to moved parent left subtree move entries from right subtree to end of left subtree shift root data to left Data Structure 2015 R. Wei 33

34 Example Data Structure 2015 R. Wei 34

35 Data Structure 2015 R. Wei 35

36 Similar to BST, the traversal of a B-tree uses inorder. The difference is that except of leaf nodes, the data in a node is not processed at the same time. Data Structure 2015 R. Wei 36

37 Algorithm BTreeTraversal (root) set scancount to 0 set nextsubtree to root left subtree loop (scancount <= number of entries) if (nextsubtree not null) BTreeTraversal (nextsubtree) end if if (ScanCount < number of entries) process (entry[scancount]) set nextsubtree to current entry right subtree end if increment scanount end loop Data Structure 2015 R. Wei 37

38 The B-tree search algorithm follow the similar idea of search a binary tree. But we need find the node and then find the entry in that node. In this case, we need to return both the node and the location of the entry in that node. Recursive method are used for finding the node. At the node found, compare from the last entry to the first entry. Data Structure 2015 R. Wei 38

39 Data Structure 2015 R. Wei 39

40 Data Structure 2015 R. Wei 40

41 typedef struct void* dataptr; struct node* rightptr; } ENTRY; typedef struct node struct node* firstptr; int numentries; ENTRY entries[order - 1]; } NODE; typedef struct int count; NODE* root; int (*compare) (void* argu1, void* argu2); } BTREE; Data Structure 2015 R. Wei 41

42 void* BTree_Search (BTREE* tree, void* targetptr) if (tree->root) return _search (tree, targetptr, tree->root); else return NULL; } // BTree_Search Data Structure 2015 R. Wei 42

43 void* _search (BTREE* tree, void* targetptr, NODE* root) int entryno; if (!root) return NULL; if (tree->compare(targetptr, root->entries[0].dataptr) < 0) return _search (tree, targetptr, root->firstptr); entryno = root->numentries - 1; while (tree->compare(targetptr, root->entries[entryno].dataptr) < 0) entryno--; if (tree->compare(targetptr, root->entries[entryno].dataptr) == 0) return (root->entries[entryno].dataptr); return (_search (tree, targetptr, root->entries[entryno].rightptr)); } // _search Data Structure 2015 R. Wei 43

44 void BTree_Traverse (BTREE* tree, void (*process) (void* dataptr)) // Statements if (tree->root) _traverse (tree->root, process); return; } // end BTree_Traverse Data Structure 2015 R. Wei 44

45 void _traverse (NODE* root, void (*process) (void* dataptr)) int scancount; NODE* ptr; scancount = 0; ptr = root->firstptr; while (scancount <= root->numentries) if (ptr) _traverse (ptr, process); // Subtree processed -- get next entry if (scancount < root->numentries) process (root->entries[scancount].dataptr); ptr = root->entries[scancount].rightptr; } // if scancount scancount++; } // if return; } // _traverse Data Structure 2015 R. Wei 45

46 void BTree_Insert (BTREE* tree, void* datainptr) bool taller; NODE* newptr; ENTRY upentry; if (tree->root == NULL) // Empty Tree. Insert first node if (newptr = (NODE*)malloc(sizeof (NODE))) newptr->firstptr = NULL; newptr->numentries = 1; newptr->entries[0].dataptr = datainptr; newptr->entries[0].rightptr = NULL; tree->root = newptr; (tree->count)++; for (int i = 1; i < ORDER - 1; i++) Data Structure 2015 R. Wei 46

47 newptr->entries[i].dataptr = NULL; newptr->entries[i].rightptr = NULL; } // for * return; } // if malloc else printf("overflow error 100 in BTree_Insert\a\n"), exit (100); taller = _insert (tree, tree->root, datainptr, &upentry); if (taller) // Tree has grown. Create new root newptr = (NODE*)malloc(sizeof(NODE)); if (newptr) Data Structure 2015 R. Wei 47

48 newptr->entries[0] = upentry; newptr->firstptr = tree->root; newptr->numentries = 1; tree->root = newptr; } // if newptr else printf("overflow error 101\a\n"), exit (100); } // if taller (tree->count)++; return; } // BTree_Insert Data Structure 2015 R. Wei 48

49 bool _insert (BTREE* tree, NODE* root, void* datainptr, ENTRY* upentry) int compresult; int entryndx; bool taller; NODE* subtreeptr; if (!root) (*upentry).dataptr = datainptr; (*upentry).rightptr = NULL; return true; // tree taller } // if NULL tree entryndx = _searchnode (tree, root, datainptr); compresult = tree->compare(datainptr, root->entries[entryndx].dataptr); Data Structure 2015 R. Wei 49

50 if (entryndx <= 0 && compresult < 0) // in node s first subtree subtreeptr = root->firstptr; else // in entry s right subtree subtreeptr = root->entries[entryndx].rightptr; taller = _insert (tree, subtreeptr, datainptr, upentry); if (taller) if (root->numentries >= ORDER - 1) // Need to create new node _splitnode (root, entryndx, compresult, upentry); taller = true; Data Structure 2015 R. Wei 50

51 } // node full else if (compresult >= 0) // New data >= current entry -- insert after _insertentry(root, entryndx + 1, *upentry); else // Insert before current entry _insertentry(root, entryndx, *upentry); (root->numentries)++; taller = false; } // else } // if taller return taller; } // _insert Data Structure 2015 R. Wei 51

52 void _splitnode (NODE* node, int entryndx, int compresult, ENTRY* upentry) int fromndx; int tondx; NODE* rightptr; rightptr = (NODE*)malloc(sizeof (NODE)); if (!rightptr) printf("overflow Error 101 in _splitnode\a\n"), exit (100); if (entryndx < MIN_ENTRIES) fromndx = MIN_ENTRIES; else fromndx = MIN_ENTRIES + 1; tondx = 0; rightptr->numentries = node->numentries - fromndx; Data Structure 2015 R. Wei 52

53 while (fromndx < node->numentries) rightptr->entries[tondx++] = node->entries[fromndx++]; node->numentries = node->numentries - rightptr->numentries; if (entryndx < MIN_ENTRIES) if (compresult < 0) _insertentry (node, entryndx, *upentry); else _insertentry (node, entryndx + 1, *upentry); } // if else _insertentry (rightptr, entryndx - MIN_ENTRIES, Data Structure 2015 R. Wei 53

54 *upentry); (rightptr->numentries)++; (node->numentries)--; } // else upentry->dataptr = node->entries[min_entries].dataptr; upentry->rightptr = rightptr; rightptr->firstptr = node->entries[min_entries].rightptr; return; } // _splitnode Data Structure 2015 R. Wei 54

55 bool BTree_Delete (BTREE* tree, void* dltkey) bool success; NODE* dltptr; if (!tree->root) return false; _delete (tree, tree->root, dltkey, &success); if (success) (tree->count)--; if (tree->root->numentries == 0) dltptr = tree->root; Data Structure 2015 R. Wei 55

56 tree->root = tree->root->firstptr; free (dltptr); } // root empty } // success return success; } // BTree_Delete Data Structure 2015 R. Wei 56

57 bool _delete (BTREE* tree, NODE* root, void* dltkeyptr, bool* success) NODE* leftptr; NODE* subtreeptr; int entryndx; int underflow; if (!root) *success = false; return false; } // null tree entryndx = _searchnode (tree, root, dltkeyptr); if (tree->compare(dltkeyptr, root->entries[entryndx].dataptr) == 0) Data Structure 2015 R. Wei 57

58 *success = true; if (root->entries[entryndx].rightptr == NULL) underflow = _deleteentry (root, entryndx); else if (entryndx > 0) leftptr = root->entries[entryndx - 1].rightPtr; else leftptr = root->firstptr; underflow = _deletemid (root, entryndx, leftptr); if (underflow) underflow = _reflow (root, entryndx); } // else internal node } // else found entry Data Structure 2015 R. Wei 58

59 else if (tree->compare (dltkeyptr, root->entries[0].dataptr) < 0) subtreeptr = root->firstptr; else subtreeptr = root->entries[entryndx].rightptr; underflow = _delete (tree, subtreeptr, dltkeyptr, success); if (underflow) underflow = _reflow (root, entryndx); } // else not found * return underflow; } // _delete Data Structure 2015 R. Wei 59

60 bool _deletemid (NODE* root, int entryndx, NODE* subtreeptr) int dltndx; int rightndx; bool underflow; if (subtreeptr->firstptr == NULL) // leaf located. Exchange data & delete leaf dltndx = subtreeptr->numentries - 1; root->entries[entryndx].dataptr = subtreeptr->entries[dltndx].dataptr; --subtreeptr->numentries; underflow = subtreeptr->numentries < MIN_ENTRIES; } // if leaf Data Structure 2015 R. Wei 60

61 else // Not located. Traverse right for predecessor rightndx = subtreeptr->numentries - 1; underflow = _deletemid (root, entryndx, subtreeptr->entries[rightndx].rightptr); if (underflow) underflow = _reflow (subtreeptr, rightndx); } // else traverse right return underflow; } // _deletemid Data Structure 2015 R. Wei 61

62 bool _reflow (NODE* root, int entryndx) NODE* lefttreeptr; NODE* righttreeptr; bool underflow; if (entryndx == 0) lefttreeptr = root->firstptr; else lefttreeptr = root->entries[entryndx - 1].rightPtr; righttreeptr = root->entries[entryndx].rightptr; if (righttreeptr->numentries > MIN_ENTRIES) _borrowright (root, entryndx, lefttreeptr, righttreeptr); underflow = false; } // if borrow right else Data Structure 2015 R. Wei 62

63 // Can t borrow from right--try left if (lefttreeptr->numentries > MIN_ENTRIES) _borrowleft (root, entryndx, lefttreeptr, righttreeptr); underflow = false; } // if borrow left * else // Can t borrow. Must combine nodes. _combine (root, entryndx, lefttreeptr, righttreeptr); underflow = (root->numentries < MIN_ENTRIES); } // else combine } // else borrow right return underflow; } // _reflow Data Structure 2015 R. Wei 63

64 void _borrowright (NODE* root, int entryndx, NODE* lefttreeptr, NODE* righttreeptr) int tondx; int shifter; tondx = lefttreeptr->numentries; lefttreeptr->entries[tondx].dataptr = root->entries[entryndx].dataptr; lefttreeptr->entries[tondx].rightptr = righttreeptr->firstptr; ++lefttreeptr->numentries; root->entries[entryndx].dataptr = righttreeptr->entries[0].dataptr; Data Structure 2015 R. Wei 64

65 righttreeptr->firstptr = righttreeptr->entries[0].rightptr; shifter = 0; while (shifter < righttreeptr->numentries - 1) righttreeptr->entries[shifter] = righttreeptr->entries[shifter + 1]; ++shifter; } // while --righttreeptr->numentries; return; } // _borrowright Data Structure 2015 R. Wei 65

66 void _combine (NODE* root, int entryndx, NODE* lefttreeptr, NODE* righttreeptr) int tondx; int fromndx; int shifter; tondx = lefttreeptr->numentries; lefttreeptr->entries[tondx].dataptr = root->entries[entryndx].dataptr; lefttreeptr->entries[tondx].rightptr = righttreeptr->firstptr; ++lefttreeptr->numentries; --root->numentries; fromndx = 0; tondx++; while (fromndx < righttreeptr->numentries) Data Structure 2015 R. Wei 66

67 lefttreeptr->entries[tondx++] = righttreeptr->entries[fromndx++]; lefttreeptr->numentries += righttreeptr->numentries; free (righttreeptr); shifter = entryndx; while (shifter < root->numentries) root->entries[shifter] = root->entries[shifter + 1]; shifter++; } // while return; } // _combine Data Structure 2015 R. Wei 67

68 BTREE* BTree_Create (int BTREE* tree; (*compare) (void* argu1, void* argu2)) tree = (BTREE*) malloc (sizeof (BTREE)); if (tree) tree->root = NULL; tree->count = 0; tree->compare = compare; } // if return tree; } // BTree_Create Data Structure 2015 R. Wei 68

69 void BTree_Print (BTREE* tree) _print (tree->root, 0); return; } // BTree_PRINT void _print (NODE* root, int level) int scancount; NODE* ptr; void* voidptr; // Statements if (root) Data Structure 2015 R. Wei 69

70 scancount = root->numentries - 1; while (scancount >= 0) ptr = root->entries[scancount].rightptr; // Test for subtree if (ptr) _print (ptr, level + 1); // Subtree processed -- print current entry printf("(%02d)", level); for (int i = 1; i <= level; i++ ) printf ("." ); voidptr = root->entries[scancount].dataptr; printf("%4d", *((int*)voidptr)); Data Structure 2015 R. Wei 70

71 printf("\t--node: %p\n", root); scancount--; } // while // Process first pointer if (root->firstptr) _print (root->firstptr, level + 1); } // if root return; } // BTree_Print Data Structure 2015 R. Wei 71

72 Some special B-tree and variations: 2-3 Tree: a B-tree of order 3. (suitable for internal search) Tree: a B-tree of order 4. (suitable for internal search) B*tree: when a node overflows, instead of being split immediately, the data are tried to redistribute among the node s siblings. B+tree: Some data need to be processed both randomly and sequentially. In a B+tree, data are all stored in leaf nodes. The key in the internal node are just for searching. Each leaf node has one additional pointer pointed to the next leaf node. Data Structure 2015 R. Wei 72

73 Data Structure 2015 R. Wei 73

74 Data Structure 2015 R. Wei 74

75 Data Structure 2015 R. Wei 75

76 Tries A trie is a multiway tree which is used to search keys as a sequence of characters (letters or digits, for example). For example, if we want to search a key begin, then we first find b, then find be, then beg, and so on. In this way, the root has 26 children. And each node may have at most 26 children. So it is based on a 26-way tree. In English, there are no words beginning with bb, bc or, bf, bg,. So the according nodes can be pruned. Data Structure 2015 R. Wei 76

77 Data Structure 2015 R. Wei 77

78 To prune the tree, we cut all of the branches that are not needed. For example, if no key starts with letter X, then at level 0 the X pointer is null. Similarly, after the letter Q, the only valid letter is U. So all the pointers in the Q branch except U are set to null. As an example, we display a tries which only contains 5 letters A, B, C, E, and T. A node contains an array of 5. The node itself pointer to the letter if the letter exists. In this example, most pointers are null. Data Structure 2015 R. Wei 78

79 Data Structure 2015 R. Wei 79

80 Algorithm searchtrie (dictionary, word) set root to dictionary set ltrndx to 0 loop (root not null) if (root entry equals word) return true end if if (ltrndx > = word length) return false end if set chndx to word[ltrndx] set root to chndx subtree increment ltrndx end loop return false Data Structure 2015 R. Wei 80

Multiway searching. In the worst case of searching a complete binary search tree, we can make log(n) page faults Everyone knows what a page fault is?

Multiway searching. In the worst case of searching a complete binary search tree, we can make log(n) page faults Everyone knows what a page fault is? Multiway searching What do we do if the volume of data to be searched is too large to fit into main memory Search tree is stored on disk pages, and the pages required as comparisons proceed may not be

More information

Chapter 20: Binary Trees

Chapter 20: Binary Trees Chapter 20: Binary Trees 20.1 Definition and Application of Binary Trees Definition and Application of Binary Trees Binary tree: a nonlinear linked list in which each node may point to 0, 1, or two other

More information

CS 2412 Data Structures. Chapter 9 Graphs

CS 2412 Data Structures. Chapter 9 Graphs CS 2412 Data Structures Chapter 9 Graphs Some concepts A graph consists of a set of vertices and a set of lines (edges, arcs). A directed graph, or digraph, is a graph in which each line has a direction.

More information

Recursion, Trees and Tree Traversal. Module CE System Programming & Computer Control Systems

Recursion, Trees and Tree Traversal. Module CE System Programming & Computer Control Systems Recursion, Trees and Tree Traversal Module CE00352-2 System Programming & Computer Control Systems Lists Data Pointer Data Pointer Can be used as queue or stack Data Pointer Data Pointer Tree Structure

More information

Material You Need to Know

Material You Need to Know Review Quiz 2 Material You Need to Know Normalization Storage and Disk File Layout Indexing B-trees and B+ Trees Extensible Hashing Linear Hashing Decomposition Goals: Lossless Joins, Dependency preservation

More information

(2,4) Trees. 2/22/2006 (2,4) Trees 1

(2,4) Trees. 2/22/2006 (2,4) Trees 1 (2,4) Trees 9 2 5 7 10 14 2/22/2006 (2,4) Trees 1 Outline and Reading Multi-way search tree ( 10.4.1) Definition Search (2,4) tree ( 10.4.2) Definition Search Insertion Deletion Comparison of dictionary

More information

2-3 Tree. Outline B-TREE. catch(...){ printf( "Assignment::SolveProblem() AAAA!"); } ADD SLIDES ON DISJOINT SETS

2-3 Tree. Outline B-TREE. catch(...){ printf( Assignment::SolveProblem() AAAA!); } ADD SLIDES ON DISJOINT SETS Outline catch(...){ printf( "Assignment::SolveProblem() AAAA!"); } Balanced Search Trees 2-3 Trees 2-3-4 Trees Slide 4 Why care about advanced implementations? Same entries, different insertion sequence:

More information

Data Structure. Chapter 5 Trees (Part II) Angela Chih-Wei Tang. National Central University Jhongli, Taiwan

Data Structure. Chapter 5 Trees (Part II) Angela Chih-Wei Tang. National Central University Jhongli, Taiwan Data Structure Chapter 5 Trees (Part II) Angela Chih-Wei Tang Department of Communication Engineering National Central University Jhongli, Taiwan 2010 Spring Threaded Binary Tree Problem: There are more

More information

Binary Trees. Examples:

Binary Trees. Examples: Binary Trees A tree is a data structure that is made of nodes and pointers, much like a linked list. The difference between them lies in how they are organized: In a linked list each node is connected

More information

[ DATA STRUCTURES ] Fig. (1) : A Tree

[ DATA STRUCTURES ] Fig. (1) : A Tree [ DATA STRUCTURES ] Chapter - 07 : Trees A Tree is a non-linear data structure in which items are arranged in a sorted sequence. It is used to represent hierarchical relationship existing amongst several

More information

void insert( Type const & ) void push_front( Type const & )

void insert( Type const & ) void push_front( Type const & ) 6.1 Binary Search Trees A binary search tree is a data structure that can be used for storing sorted data. We will begin by discussing an Abstract Sorted List or Sorted List ADT and then proceed to describe

More information

Fall, 2015 Prof. Jungkeun Park

Fall, 2015 Prof. Jungkeun Park Data Structures and Algorithms Binary Search Trees Fall, 2015 Prof. Jungkeun Park Copyright Notice: This material is modified version of the lecture slides by Prof. Rada Mihalcea in Univ. of North Texas.

More information

Tree Structures. A hierarchical data structure whose point of entry is the root node

Tree Structures. A hierarchical data structure whose point of entry is the root node Binary Trees 1 Tree Structures A tree is A hierarchical data structure whose point of entry is the root node This structure can be partitioned into disjoint subsets These subsets are themselves trees and

More information

CSCI Trees. Mark Redekopp David Kempe

CSCI Trees. Mark Redekopp David Kempe CSCI 104 2-3 Trees Mark Redekopp David Kempe Trees & Maps/Sets C++ STL "maps" and "sets" use binary search trees internally to store their keys (and values) that can grow or contract as needed This allows

More information

Binary Trees. Height 1

Binary Trees. Height 1 Binary Trees Definitions A tree is a finite set of one or more nodes that shows parent-child relationship such that There is a special node called root Remaining nodes are portioned into subsets T1,T2,T3.

More information

We have the pointers reference the next node in an inorder traversal; called threads

We have the pointers reference the next node in an inorder traversal; called threads Leaning Objective: In this Module you will be learning the following: Threaded Binary Tree Introduction: Threaded Binary Tree is also a binary tree in which all left child pointers that are NULL (in Linked

More information

Search Trees. Data and File Structures Laboratory. DFS Lab (ISI) Search Trees 1 / 17

Search Trees. Data and File Structures Laboratory.  DFS Lab (ISI) Search Trees 1 / 17 Search Trees Data and File Structures Laboratory http://www.isical.ac.in/~dfslab/2017/index.html DFS Lab (ISI) Search Trees 1 / 17 Binary search trees. Definition. Binary tree in which following property

More information

IX. Binary Trees (Chapter 10)

IX. Binary Trees (Chapter 10) IX. Binary Trees (Chapter 10) -1- A. Introduction: Searching a linked list. 1. Linear Search /* To linear search a list for a particular Item */ 1. Set Loc = 0; 2. Repeat the following: a. If Loc >= length

More information

A set of nodes (or vertices) with a single starting point

A set of nodes (or vertices) with a single starting point Binary Search Trees Understand tree terminology Understand and implement tree traversals Define the binary search tree property Implement binary search trees Implement the TreeSort algorithm 2 A set of

More information

Physical Level of Databases: B+-Trees

Physical Level of Databases: B+-Trees Physical Level of Databases: B+-Trees Adnan YAZICI Computer Engineering Department METU (Fall 2005) 1 B + -Tree Index Files l Disadvantage of indexed-sequential files: performance degrades as file grows,

More information

Lecture 11: Multiway and (2,4) Trees. Courtesy to Goodrich, Tamassia and Olga Veksler

Lecture 11: Multiway and (2,4) Trees. Courtesy to Goodrich, Tamassia and Olga Veksler Lecture 11: Multiway and (2,4) Trees 9 2 5 7 10 14 Courtesy to Goodrich, Tamassia and Olga Veksler Instructor: Yuzhen Xie Outline Multiway Seach Tree: a new type of search trees: for ordered d dictionary

More information

! Tree: set of nodes and directed edges. ! Parent: source node of directed edge. ! Child: terminal node of directed edge

! Tree: set of nodes and directed edges. ! Parent: source node of directed edge. ! Child: terminal node of directed edge Trees & Heaps Week 12 Gaddis: 20 Weiss: 21.1-3 CS 5301 Fall 2018 Jill Seaman!1 Tree: non-recursive definition! Tree: set of nodes and directed edges - root: one node is distinguished as the root - Every

More information

Chapter 5. Binary Trees

Chapter 5. Binary Trees Chapter 5 Binary Trees Definitions and Properties A binary tree is made up of a finite set of elements called nodes It consists of a root and two subtrees There is an edge from the root to its children

More information

Trees. Courtesy to Goodrich, Tamassia and Olga Veksler

Trees. Courtesy to Goodrich, Tamassia and Olga Veksler Lecture 12: BT Trees Courtesy to Goodrich, Tamassia and Olga Veksler Instructor: Yuzhen Xie Outline B-tree Special case of multiway search trees used when data must be stored on the disk, i.e. too large

More information

BST Implementation. Data Structures. Lecture 4 Binary search trees (BST) Dr. Mahmoud Attia Sakr University of Ain Shams

BST Implementation. Data Structures. Lecture 4 Binary search trees (BST) Dr. Mahmoud Attia Sakr University of Ain Shams Lecture 4 Binary search trees (BST) Dr. Mahmoud Attia Sakr mahmoud.sakr@cis.asu.edu.eg Cairo, Egypt, October 2012 Binary Search Trees (BST) 1. Hierarchical data structure with a single reference to root

More information

SCJ2013 Data Structure & Algorithms. Binary Search Tree. Nor Bahiah Hj Ahmad

SCJ2013 Data Structure & Algorithms. Binary Search Tree. Nor Bahiah Hj Ahmad SCJ2013 Data Structure & Algorithms Binary Search Tree Nor Bahiah Hj Ahmad Binary Search Tree A binary search tree has the following properties: For every node n in the tree Value of n is greater than

More information

C Data Structures Stacks. Stack. push Adds a new node to the top of the stack

C Data Structures Stacks. Stack. push Adds a new node to the top of the stack 1 12 C Data Structures 12.5 Stacks 2 Stack New nodes can be added and removed only at the top Similar to a pile of dishes Last-in, first-out (LIFO) Bottom of stack indicated by a link member to NULL Constrained

More information

(2,4) Trees Goodrich, Tamassia (2,4) Trees 1

(2,4) Trees Goodrich, Tamassia (2,4) Trees 1 (2,4) Trees 9 2 5 7 10 14 2004 Goodrich, Tamassia (2,4) Trees 1 Multi-Way Search Tree A multi-way search tree is an ordered tree such that Each internal node has at least two children and stores d -1 key-element

More information

Module 4: Index Structures Lecture 13: Index structure. The Lecture Contains: Index structure. Binary search tree (BST) B-tree. B+-tree.

Module 4: Index Structures Lecture 13: Index structure. The Lecture Contains: Index structure. Binary search tree (BST) B-tree. B+-tree. The Lecture Contains: Index structure Binary search tree (BST) B-tree B+-tree Order file:///c /Documents%20and%20Settings/iitkrana1/My%20Documents/Google%20Talk%20Received%20Files/ist_data/lecture13/13_1.htm[6/14/2012

More information

a graph is a data structure made up of nodes in graph theory the links are normally called edges

a graph is a data structure made up of nodes in graph theory the links are normally called edges 1 Trees Graphs a graph is a data structure made up of nodes each node stores data each node has links to zero or more nodes in graph theory the links are normally called edges graphs occur frequently in

More information

Binary Search Tree 1.0. Generated by Doxygen Mon Jun :12:39

Binary Search Tree 1.0. Generated by Doxygen Mon Jun :12:39 Binary Search Tree 1.0 Generated by Doxygen 1.7.1 Mon Jun 6 2011 16:12:39 Contents 1 Binary Search Tree Program 1 1.1 Introduction.......................................... 1 2 Data Structure Index 3

More information

(2,4) Trees Goodrich, Tamassia. (2,4) Trees 1

(2,4) Trees Goodrich, Tamassia. (2,4) Trees 1 (2,4) Trees 9 2 5 7 10 14 (2,4) Trees 1 Multi-Way Search Tree ( 9.4.1) A multi-way search tree is an ordered tree such that Each internal node has at least two children and stores d 1 key-element items

More information

ECE264 Spring 2013 Final Exam, April 30, 2013

ECE264 Spring 2013 Final Exam, April 30, 2013 ECE264 Spring 2013 Final Exam, April 30, 2013 In signing this statement, I hereby certify that the work on this exam is my own and that I have not copied the work of any other student while completing

More information

Trees. (Trees) Data Structures and Programming Spring / 28

Trees. (Trees) Data Structures and Programming Spring / 28 Trees (Trees) Data Structures and Programming Spring 2018 1 / 28 Trees A tree is a collection of nodes, which can be empty (recursive definition) If not empty, a tree consists of a distinguished node r

More information

Algorithms. Deleting from Red-Black Trees B-Trees

Algorithms. Deleting from Red-Black Trees B-Trees Algorithms Deleting from Red-Black Trees B-Trees Recall the rules for BST deletion 1. If vertex to be deleted is a leaf, just delete it. 2. If vertex to be deleted has just one child, replace it with that

More information

Cpt S 122 Data Structures. Data Structures Trees

Cpt S 122 Data Structures. Data Structures Trees Cpt S 122 Data Structures Data Structures Trees Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Motivation Trees are one of the most important and extensively

More information

! Tree: set of nodes and directed edges. ! Parent: source node of directed edge. ! Child: terminal node of directed edge

! Tree: set of nodes and directed edges. ! Parent: source node of directed edge. ! Child: terminal node of directed edge Trees (& Heaps) Week 12 Gaddis: 20 Weiss: 21.1-3 CS 5301 Spring 2015 Jill Seaman 1 Tree: non-recursive definition! Tree: set of nodes and directed edges - root: one node is distinguished as the root -

More information

Unit III - Tree TREES

Unit III - Tree TREES TREES Unit III - Tree Consider a scenario where you are required to represent the directory structure of your operating system. The directory structure contains various folders and files. A folder may

More information

Trees can be used to store entire records from a database, serving as an in-memory representation of the collection of records in a file.

Trees can be used to store entire records from a database, serving as an in-memory representation of the collection of records in a file. Large Trees 1 Trees can be used to store entire records from a database, serving as an in-memory representation of the collection of records in a file. Trees can also be used to store indices of the collection

More information

CS 2412 Data Structures. Chapter 7 Heaps

CS 2412 Data Structures. Chapter 7 Heaps CS 2412 Data Structures Chapter 7 Heaps A binary tree is a complete tree if it has maximum number of entries for its height. a nearly complete if it has the minimum height for its nodes and all nodes in

More information

amiri advanced databases '05

amiri advanced databases '05 More on indexing: B+ trees 1 Outline Motivation: Search example Cost of searching with and without indices B+ trees Definition and structure B+ tree operations Inserting Deleting 2 Dense ordered index

More information

An AVL tree with N nodes is an excellent data. The Big-Oh analysis shows that most operations finish within O(log N) time

An AVL tree with N nodes is an excellent data. The Big-Oh analysis shows that most operations finish within O(log N) time B + -TREES MOTIVATION An AVL tree with N nodes is an excellent data structure for searching, indexing, etc. The Big-Oh analysis shows that most operations finish within O(log N) time The theoretical conclusion

More information

2-3 and Trees. COL 106 Shweta Agrawal, Amit Kumar, Dr. Ilyas Cicekli

2-3 and Trees. COL 106 Shweta Agrawal, Amit Kumar, Dr. Ilyas Cicekli 2-3 and 2-3-4 Trees COL 106 Shweta Agrawal, Amit Kumar, Dr. Ilyas Cicekli Multi-Way Trees A binary search tree: One value in each node At most 2 children An M-way search tree: Between 1 to (M-1) values

More information

Trees. A tree is a directed graph with the property

Trees. A tree is a directed graph with the property 2: Trees Trees A tree is a directed graph with the property There is one node (the root) from which all other nodes can be reached by exactly one path. Seen lots of examples. Parse Trees Decision Trees

More information

8. Binary Search Tree

8. Binary Search Tree 8 Binary Search Tree Searching Basic Search Sequential Search : Unordered Lists Binary Search : Ordered Lists Tree Search Binary Search Tree Balanced Search Trees (Skipped) Sequential Search int Seq-Search

More information

Programming II (CS300)

Programming II (CS300) 1 Programming II (CS300) Chapter 11: Binary Search Trees MOUNA KACEM mouna@cs.wisc.edu Fall 2018 General Overview of Data Structures 2 Introduction to trees 3 Tree: Important non-linear data structure

More information

B-Trees. nodes with many children a type node a class for B-trees. an elaborate example the insertion algorithm removing elements

B-Trees. nodes with many children a type node a class for B-trees. an elaborate example the insertion algorithm removing elements B-Trees 1 B-Trees nodes with many children a type node a class for B-trees 2 manipulating a B-tree an elaborate example the insertion algorithm removing elements MCS 360 Lecture 35 Introduction to Data

More information

FORTH SEMESTER DIPLOMA EXAMINATION IN ENGINEERING/ TECHNOLIGY- OCTOBER, 2012 DATA STRUCTURE

FORTH SEMESTER DIPLOMA EXAMINATION IN ENGINEERING/ TECHNOLIGY- OCTOBER, 2012 DATA STRUCTURE TED (10)-3071 Reg. No.. (REVISION-2010) Signature. FORTH SEMESTER DIPLOMA EXAMINATION IN ENGINEERING/ TECHNOLIGY- OCTOBER, 2012 DATA STRUCTURE (Common to CT and IF) [Time: 3 hours (Maximum marks: 100)

More information

Binary Trees and Binary Search Trees

Binary Trees and Binary Search Trees Binary Trees and Binary Search Trees Learning Goals After this unit, you should be able to... Determine if a given tree is an instance of a particular type (e.g. binary, and later heap, etc.) Describe

More information

CS102 Binary Search Trees

CS102 Binary Search Trees CS102 Binary Search Trees Prof Tejada 1 To speed up insertion, removal and search, modify the idea of a Binary Tree to create a Binary Search Tree (BST) Binary Search Trees Binary Search Trees have one

More information

CSE 250 Final Exam. Fall 2013 Time: 3 hours. Dec 11, No electronic devices of any kind. You can open your textbook and notes

CSE 250 Final Exam. Fall 2013 Time: 3 hours. Dec 11, No electronic devices of any kind. You can open your textbook and notes CSE 250 Final Exam Fall 2013 Time: 3 hours. Dec 11, 2013 Total points: 100 14 pages Please use the space provided for each question, and the back of the page if you need to. Please do not use any extra

More information

CS 241 Data Organization Binary Trees

CS 241 Data Organization Binary Trees CS 241 Data Organization Binary Trees Brooke Chenoweth University of New Mexico Fall 2017 Binary Tree: Kernighan and Ritchie 6.5 Read a file and count the occurrences of each word. now is the time for

More information

Data Structures and Algorithms

Data Structures and Algorithms Data Structures and Algorithms CS245-2008S-19 B-Trees David Galles Department of Computer Science University of San Francisco 19-0: Indexing Operations: Add an element Remove an element Find an element,

More information

CS24 Week 8 Lecture 1

CS24 Week 8 Lecture 1 CS24 Week 8 Lecture 1 Kyle Dewey Overview Tree terminology Tree traversals Implementation (if time) Terminology Node The most basic component of a tree - the squares Edge The connections between nodes

More information

B-trees. It also makes sense to have data structures that use the minimum addressable unit as their base node size.

B-trees. It also makes sense to have data structures that use the minimum addressable unit as their base node size. B-trees Balanced BSTs such as RBTs are great for data structures that can fit into the main memory of the computer. But what happens when we need to use external storage? Here are some approximate speeds

More information

Lecture Notes on Tries

Lecture Notes on Tries Lecture Notes on Tries 15-122: Principles of Imperative Computation Thomas Cortina, Frank Pfenning, Rob Simmons, Penny Anderson Lecture 22 June 20, 2014 1 Introduction In the data structures implementing

More information

TREES. Trees - Introduction

TREES. Trees - Introduction TREES Chapter 6 Trees - Introduction All previous data organizations we've studied are linear each element can have only one predecessor and successor Accessing all elements in a linear sequence is O(n)

More information

Objectives. Upon completion you will be able to:

Objectives. Upon completion you will be able to: Chapter 10 Multiway Trees Objectives Upon completion you will be able to: Define and discuss multiway tree structures Understand the operation and use of the B-tree ADT Discuss the use and implementation

More information

B-Trees and External Memory

B-Trees and External Memory Presentation for use with the textbook, Algorithm Design and Applications, by M. T. Goodrich and R. Tamassia, Wiley, 2015 B-Trees and External Memory 1 (2, 4) Trees: Generalization of BSTs Each internal

More information

Binary Search Trees. Contents. Steven J. Zeil. July 11, Definition: Binary Search Trees The Binary Search Tree ADT...

Binary Search Trees. Contents. Steven J. Zeil. July 11, Definition: Binary Search Trees The Binary Search Tree ADT... Steven J. Zeil July 11, 2013 Contents 1 Definition: Binary Search Trees 2 1.1 The Binary Search Tree ADT.................................................... 3 2 Implementing Binary Search Trees 7 2.1 Searching

More information

Lecture Notes on Tries

Lecture Notes on Tries Lecture Notes on Tries 15-122: Principles of Imperative Computation Thomas Cortina Notes by Frank Pfenning Lecture 24 April 19, 2011 1 Introduction In the data structures implementing associative arrays

More information

IX. Binary Trees (Chapter 10) Linear search can be used for lists stored in an array as well as for linked lists. (It's the method used in the find

IX. Binary Trees (Chapter 10) Linear search can be used for lists stored in an array as well as for linked lists. (It's the method used in the find IX. Binary Trees IX-1 IX. Binary Trees (Chapter 10) A. Introduction: Searching a linked list. 1. Linear Search /* To linear search a list for a particular Item */ 1. Set Loc = 0; 2. Repeat the following:

More information

B-Trees & its Variants

B-Trees & its Variants B-Trees & its Variants Advanced Data Structure Spring 2007 Zareen Alamgir Motivation Yet another Tree! Why do we need another Tree-Structure? Data Retrieval from External Storage In database programs,

More information

Data Structures. Outline. Introduction Linked Lists Stacks Queues Trees Deitel & Associates, Inc. All rights reserved.

Data Structures. Outline. Introduction Linked Lists Stacks Queues Trees Deitel & Associates, Inc. All rights reserved. Data Structures Outline Introduction Linked Lists Stacks Queues Trees Introduction dynamic data structures - grow and shrink during execution Linked lists - insertions and removals made anywhere Stacks

More information

CS302 - Data Structures using C++

CS302 - Data Structures using C++ CS302 - Data Structures using C++ Topic: Tree Implementations Kostas Alexis Nodes in a Binary Tree Representing tree nodes Must contain both data and pointers to node s children Each node will be an object

More information

Data Structure - Advanced Topics in Tree -

Data Structure - Advanced Topics in Tree - Data Structure - Advanced Topics in Tree - AVL, Red-Black, B-tree Hanyang University Jong-Il Park AVL TREE Division of Computer Science and Engineering, Hanyang University Balanced binary trees Non-random

More information

Multi-way Search Trees. (Multi-way Search Trees) Data Structures and Programming Spring / 25

Multi-way Search Trees. (Multi-way Search Trees) Data Structures and Programming Spring / 25 Multi-way Search Trees (Multi-way Search Trees) Data Structures and Programming Spring 2017 1 / 25 Multi-way Search Trees Each internal node of a multi-way search tree T: has at least two children contains

More information

Trees can be used to store entire records from a database, serving as an in-memory representation of the collection of records in a file.

Trees can be used to store entire records from a database, serving as an in-memory representation of the collection of records in a file. Large Trees 1 Trees can be used to store entire records from a database, serving as an in-memory representation of the collection of records in a file. Trees can also be used to store indices of the collection

More information

Search Trees. The term refers to a family of implementations, that may have different properties. We will discuss:

Search Trees. The term refers to a family of implementations, that may have different properties. We will discuss: Search Trees CSE 2320 Algorithms and Data Structures Alexandra Stefan Based on slides and notes from: Vassilis Athitsos and Bob Weems University of Texas at Arlington 1 Search Trees Preliminary note: "search

More information

B-Trees. Version of October 2, B-Trees Version of October 2, / 22

B-Trees. Version of October 2, B-Trees Version of October 2, / 22 B-Trees Version of October 2, 2014 B-Trees Version of October 2, 2014 1 / 22 Motivation An AVL tree can be an excellent data structure for implementing dictionary search, insertion and deletion Each operation

More information

Laboratory Module Trees

Laboratory Module Trees Purpose: understand the notion of 2-3 trees to build, in C, a 2-3 tree 1 2-3 Trees 1.1 General Presentation Laboratory Module 7 2-3 Trees 2-3 Trees represent a the simplest type of multiway trees trees

More information

Tree: non-recursive definition. Trees, Binary Search Trees, and Heaps. Tree: recursive definition. Tree: example.

Tree: non-recursive definition. Trees, Binary Search Trees, and Heaps. Tree: recursive definition. Tree: example. Trees, Binary Search Trees, and Heaps CS 5301 Fall 2013 Jill Seaman Tree: non-recursive definition Tree: set of nodes and directed edges - root: one node is distinguished as the root - Every node (except

More information

B-Trees and External Memory

B-Trees and External Memory Presentation for use with the textbook, Algorithm Design and Applications, by M. T. Goodrich and R. Tamassia, Wiley, 2015 and External Memory 1 1 (2, 4) Trees: Generalization of BSTs Each internal node

More information

Programming Languages and Techniques (CIS120)

Programming Languages and Techniques (CIS120) Programming Languages and Techniques () Lecture 6 January 24, 2018 Binary Search Trees (Lecture notes Chapter 7) Announcements Homework 2: Computing Human Evolution due Tuesday, September 19 th Reading:

More information

Balanced Search Trees. CS 3110 Fall 2010

Balanced Search Trees. CS 3110 Fall 2010 Balanced Search Trees CS 3110 Fall 2010 Some Search Structures Sorted Arrays Advantages Search in O(log n) time (binary search) Disadvantages Need to know size in advance Insertion, deletion O(n) need

More information

CS302 - Data Structures using C++

CS302 - Data Structures using C++ CS302 - Data Structures using C++ Topic: Implementation Kostas Alexis s Definition: A (BST) is a binary tree where each node has a Comparable key (and an associated value) and satisfies the restriction

More information

B-Trees. Disk Storage. What is a multiway tree? What is a B-tree? Why B-trees? Insertion in a B-tree. Deletion in a B-tree

B-Trees. Disk Storage. What is a multiway tree? What is a B-tree? Why B-trees? Insertion in a B-tree. Deletion in a B-tree B-Trees Disk Storage What is a multiway tree? What is a B-tree? Why B-trees? Insertion in a B-tree Deletion in a B-tree Disk Storage Data is stored on disk (i.e., secondary memory) in blocks. A block is

More information

B-Trees. Based on materials by D. Frey and T. Anastasio

B-Trees. Based on materials by D. Frey and T. Anastasio B-Trees Based on materials by D. Frey and T. Anastasio 1 Large Trees n Tailored toward applications where tree doesn t fit in memory q operations much faster than disk accesses q want to limit levels of

More information

CSCI-401 Examlet #5. Name: Class: Date: True/False Indicate whether the sentence or statement is true or false.

CSCI-401 Examlet #5. Name: Class: Date: True/False Indicate whether the sentence or statement is true or false. Name: Class: Date: CSCI-401 Examlet #5 True/False Indicate whether the sentence or statement is true or false. 1. The root node of the standard binary tree can be drawn anywhere in the tree diagram. 2.

More information

Lecture 3: B-Trees. October Lecture 3: B-Trees

Lecture 3: B-Trees. October Lecture 3: B-Trees October 2017 Remarks Search trees The dynamic set operations search, minimum, maximum, successor, predecessor, insert and del can be performed efficiently (in O(log n) time) if the search tree is balanced.

More information

Trees. Chapter 6. strings. 3 Both position and Enumerator are similar in concept to C++ iterators, although the details are quite different.

Trees. Chapter 6. strings. 3 Both position and Enumerator are similar in concept to C++ iterators, although the details are quite different. Chapter 6 Trees In a hash table, the items are not stored in any particular order in the table. This is fine for implementing Sets and Maps, since for those abstract data types, the only thing that matters

More information

CS 310 B-trees, Page 1. Motives. Large-scale databases are stored in disks/hard drives.

CS 310 B-trees, Page 1. Motives. Large-scale databases are stored in disks/hard drives. CS 310 B-trees, Page 1 Motives Large-scale databases are stored in disks/hard drives. Disks are quite different from main memory. Data in a disk are accessed through a read-write head. To read a piece

More information

B-Trees. Introduction. Definitions

B-Trees. Introduction. Definitions 1 of 10 B-Trees Introduction A B-tree is a specialized multiway tree designed especially for use on disk. In a B-tree each node may contain a large number of keys. The number of subtrees of each node,

More information

Advanced Java Concepts Unit 5: Trees. Notes and Exercises

Advanced Java Concepts Unit 5: Trees. Notes and Exercises Advanced Java Concepts Unit 5: Trees. Notes and Exercises A Tree is a data structure like the figure shown below. We don t usually care about unordered trees but that s where we ll start. Later we will

More information

CS 350 : Data Structures Binary Search Trees

CS 350 : Data Structures Binary Search Trees CS 350 : Data Structures Binary Search Trees David Babcock (courtesy of James Moscola) Department of Physical Sciences York College of Pennsylvania James Moscola Introduction to Binary Search Trees A binary

More information

Data Structures and Algorithms (DSA) Course 9 Lists / Graphs / Trees. Iulian Năstac

Data Structures and Algorithms (DSA) Course 9 Lists / Graphs / Trees. Iulian Năstac Data Structures and Algorithms (DSA) Course 9 Lists / Graphs / Trees Iulian Năstac Recapitulation It is considered the following type: typedef struct nod { ; struct nod *next; } NOD; 2 Circular

More information

Some Search Structures. Balanced Search Trees. Binary Search Trees. A Binary Search Tree. Review Binary Search Trees

Some Search Structures. Balanced Search Trees. Binary Search Trees. A Binary Search Tree. Review Binary Search Trees Some Search Structures Balanced Search Trees Lecture 8 CS Fall Sorted Arrays Advantages Search in O(log n) time (binary search) Disadvantages Need to know size in advance Insertion, deletion O(n) need

More information

Binary Trees, Binary Search Trees

Binary Trees, Binary Search Trees Binary Trees, Binary Search Trees Trees Linear access time of linked lists is prohibitive Does there exist any simple data structure for which the running time of most operations (search, insert, delete)

More information

Augmenting Data Structures

Augmenting Data Structures Augmenting Data Structures [Not in G &T Text. In CLRS chapter 14.] An AVL tree by itself is not very useful. To support more useful queries we need more structure. General Definition: An augmented data

More information

BBM 201 Data structures

BBM 201 Data structures BBM 201 Data structures Lecture 11: Trees 2018-2019 Fall Content Terminology The Binary Tree The Binary Search Tree Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, 2013

More information

Extra: B+ Trees. Motivations. Differences between BST and B+ 10/27/2017. CS1: Java Programming Colorado State University

Extra: B+ Trees. Motivations. Differences between BST and B+ 10/27/2017. CS1: Java Programming Colorado State University Extra: B+ Trees CS1: Java Programming Colorado State University Slides by Wim Bohm and Russ Wakefield 1 Motivations Many times you want to minimize the disk accesses while doing a search. A binary search

More information

Search Trees: BSTs and B-Trees

Search Trees: BSTs and B-Trees 3//13 Search Trees: BSTs and B-Trees Administrative David Kauchak cs302 Spring 13 Proof by contradiction Number guessing game I m thinking of a number between 1 and n You are trying to guess the answer

More information

Friday Four Square! 4:15PM, Outside Gates

Friday Four Square! 4:15PM, Outside Gates Binary Search Trees Friday Four Square! 4:15PM, Outside Gates Implementing Set On Monday and Wednesday, we saw how to implement the Map and Lexicon, respectively. Let's now turn our attention to the Set.

More information

Algorithms. AVL Tree

Algorithms. AVL Tree Algorithms AVL Tree Balanced binary tree The disadvantage of a binary search tree is that its height can be as large as N-1 This means that the time needed to perform insertion and deletion and many other

More information

Solution to CSE 250 Final Exam

Solution to CSE 250 Final Exam Solution to CSE 250 Final Exam Fall 2013 Time: 3 hours. December 13, 2013 Total points: 100 14 pages Please use the space provided for each question, and the back of the page if you need to. Please do

More information

Multiway Search Trees. Multiway-Search Trees (cont d)

Multiway Search Trees. Multiway-Search Trees (cont d) Multiway Search Trees Each internal node v of a multi-way search tree T has at least two children contains d-1 items, where d is the number of children of v an item is of the form (k i,x i ) for 1 i d-1,

More information

Trees 11/15/16. Chapter 11. Terminology. Terminology. Terminology. Terminology. Terminology

Trees 11/15/16. Chapter 11. Terminology. Terminology. Terminology. Terminology. Terminology Chapter 11 Trees Definition of a general tree A general tree T is a set of one or more nodes such that T is partitioned into disjoint subsets: A single node r, the root Sets that are general trees, called

More information

CS350: Data Structures Binary Search Trees

CS350: Data Structures Binary Search Trees Binary Search Trees James Moscola Department of Engineering & Computer Science York College of Pennsylvania James Moscola Introduction to Binary Search Trees A binary search tree is a binary tree that

More information

Binary Trees. College of Computing & Information Technology King Abdulaziz University. CPCS-204 Data Structures I

Binary Trees. College of Computing & Information Technology King Abdulaziz University. CPCS-204 Data Structures I Binary Trees College of Computing & Information Technology King Abdulaziz University CPCS-204 Data Structures I Outline Tree Stuff Trees Binary Trees Implementation of a Binary Tree Tree Traversals Depth

More information

Tree-Structured Indexes

Tree-Structured Indexes Tree-Structured Indexes Chapter 9 Database Management Systems, R. Ramakrishnan and J. Gehrke 1 Introduction As for any index, 3 alternatives for data entries k*: Data record with key value k

More information