# Binary Trees and Huffman Encoding Binary Search Trees

Save this PDF as:

Size: px
Start display at page:

## Transcription

1 Binary Trees and Huffman Encoding Binary Search Trees Computer Science E119 Harvard Extension School Fall 2012 David G. Sullivan, Ph.D. Motivation: Maintaining a Sorted Collection of Data A data dictionary is a sorted collection of data with the following key operations: search for an item (and possibly delete it) insert a new item If we use a list to implement a data dictionary, efficiency = O(n). data structure searching for an item inserting an item a list implemented using an array a list implemented using a linked list O(log n) using binary search O(n) using linear search (binary search in a linked list is O(n log n)) In the next few lectures, we ll look at data structures (trees and hash tables) that can be used for a more efficient data dictionary. We ll also look at other applications of trees. O(n) because we need to shift items over O(n) (O(1) to do the actual insertion, but O(n) to find where it belongs)

2 What Is a Tree? root edge node A tree consists of: a set of nodes a set of edges, each of which connects a pair of nodes Each node may have one or more data items. each data item consists of one or more fields key field = the field used when searching for a data item multiple data items with the same key are referred to as duplicates The node at the top of the tree is called the root of the tree. Relationships Between Nodes If a node N is connected to other nodes that are directly below it in the tree, N is referred to as their parent and they are referred to as its children. example: node is the parent of nodes 10, 11, and 12 Each node is the child of at most one parent. Other familyrelated terms are also used: nodes with the same parent are siblings a node s ancestors are its parent, its parent s parent, etc. example: node 9 s ancestors are 3 and 1 a node s descendants are its children, their children, etc. example: node 1 s descendants are all of the other nodes

3 Types of Nodes A leaf node is a node without children. An interior node is a node with one or more children. A Tree is a Recursive Data Structure Each node in the tree is the root of a smaller tree! refer to such trees as subtrees to distinguish them from the tree as a whole example: node 2 is the root of the subtree circled above example: node 6 is the root of a subtree with only one node We ll see that tree algorithms often lend themselves to recursive implementations.

4 Path, Depth, Level, and Height level 0 level 1 depth = 2 level 2 There is exactly one path (one sequence of edges) connecting each node to the root. depth of a node = # of edges on the path from it to the root Nodes with the same depth form a level of the tree. The height of a tree is the maximum depth of its nodes. example: the tree above has a height of 2 Binary Trees In a binary tree, nodes have at most two children. Recursive definition: a binary tree is either: 1) empty, or 2) a node (the root of the tree) that has one or more data items a left child, which is itself the root of a binary tree a right child, which is itself the root of a binary tree Example: s left child s right child s left subtree s right subtree How are the edges of the tree represented?

5 Representing a Binary Tree Using Linked Nodes public class LinkedTree { private class Node { private int key; private LLList data; private Node left; private Node right; private Node root; root // list of data items // reference to left child // reference to right child 4 null 12 null null null null null 38 null null see ~cscie119/examples/trees/linkedtree.java Traversing a Binary Tree Traversing a tree involves visiting all of the nodes in the tree. visiting a node = processing its data in some way example: print the key We will look at four types of traversals. Each of them visits the nodes in a different order. To understand traversals, it helps to remember the recursive definition of a binary tree, in which every node is the root of a subtree. 12 is the root of s left subtree 4 is the root of 12 s left subtree is the root of s right subtree

6 Preorder Traversal preorder traversal of the tree whose root is N: 1) visit the root, N 2) recursively perform a preorder traversal of N s left subtree 3) recursively perform a preorder traversal of N s right subtree Preorder traversal of the tree above: Which statespace search strategy visits nodes in this order? Implementing Preorder Traversal public class LinkedTree { private Node root; public void preorderprint() { if (root!= null) preorderprinttree(root); private static void preorderprinttree(node root) { System.out.print(root.key + ); if (root.left!= null) preorderprinttree(root.left); if (root.right!= null) preorderprinttree(root.right); Not always the same as the root of the entire tree. preorderprinttree() is a static, recursive method that takes as a parameter the root of the tree/subtree that you want to print. preorderprint() is a nonstatic method that makes the initial call. It passes in the root of the entire tree as the parameter.

7 Tracing Preorder Traversal void preorderprinttree(node root) { System.out.print(root.key + ); if (root.left!= null) preorderprinttree(root.left); if (root.right!= null) preorderprinttree(root.right); print print 4 print 4 2 print print 6 time... Postorder Traversal postorder traversal of the tree whose root is N: 1) recursively perform a postorder traversal of N s left subtree 2) recursively perform a postorder traversal of N s right subtree 3) visit the root, N Postorder traversal of the tree above:

8 Implementing Postorder Traversal public class LinkedTree { private Node root; public void postorderprint() { if (root!= null) postorderprinttree(root); private static void postorderprinttree(node root) { if (root.left!= null) postorderprinttree(root.left); if (root.right!= null) postorderprinttree(root.right); System.out.print(root.key + ); Note that the root is printed after the two recursive calls. Tracing Postorder Traversal void postorderprinttree(node root) { if (root.left!= null) postorderprinttree(root.left); if (root.right!= null) postorderprinttree(root.right); System.out.print(root.key + ); print print 2 print 6... time

9 Inorder Traversal inorder traversal of the tree whose root is N: 1) recursively perform an inorder traversal of N s left subtree 2) visit the root, N 3) recursively perform an inorder traversal of N s right subtree Inorder traversal of the tree above: Implementing Inorder Traversal public class LinkedTree { private Node root; public void inorderprint() { if (root!= null) inorderprinttree(root); private static void inorderprinttree(node root) { if (root.left!= null) inorderprinttree(root.left); System.out.print(root.key + ); if (root.right!= null) inorderprinttree(root.right); Note that the root is printed between the two recursive calls.

10 Tracing Inorder Traversal void inorderprinttree(node root) { if (root.left!= null) inorderprinttree(root.left); System.out.print(root.key + ); if (root.right!= null) inorderprinttree(root.right); print print 2 print 6 print... time LevelOrder Traversal Visit the nodes one level at a time, from top to bottom and left to right Levelorder traversal of the tree above: Which statespace search strategy visits nodes in this order? How could we implement this type of traversal?

11 TreeTraversal Summary preorder: root, left subtree, right subtree postorder: left subtree, right subtree, root inorder: left subtree, root, right subtree levelorder: top to bottom, left to right Perform each type of traversal on the tree below: Using a Binary Tree for an Algebraic Expression We ll restrict ourselves to fully parenthesized expressions and to the following binary operators: +,, *, / Example expression: ((a + (b * c)) (d / e)) Tree representation: + / a * d e b c Leaf nodes are variables or constants; interior nodes are operators. Because the operators are binary, either a node has two children or it has none.

12 Traversing an AlgebraicExpression Tree Inorder gives conventional algebraic notation. print ( before the recursive call on the left subtree print ) after the recursive call on the right subtree + a * b c d / e for tree at right: ((a + (b * c)) (d / e)) Preorder gives functional notation. print ( s and ) s as for inorder, and commas after the recursive call on the left subtree for tree above: subtr(add(a, mult(b, c)), divide(d, e)) Postorder gives the order in which the computation must be carried out on a stack/rpn calculator. for tree above: push a, push b, push c, multiply, add, see ~cscie119/examples/trees/exprtree.java FixedLength Character Encodings A character encoding maps each character to a number. Computers usually use fixedlength character encodings. ASCII (American Standard Code for Information Interchange) uses 8 bits per character. char dec binary a b c example: bat is stored in a text file as the following sequence of bits: Unicode uses 16 bits per character to accommodate foreignlanguage characters. (ASCII codes are a subset.) Fixedlength encodings are simple, because all character encodings have the same length a given character always has the same encoding

13 VariableLength Character Encodings Problem: fixedlength encodings waste space. Solution: use a variablelength encoding. use encodings of different lengths for different characters assign shorter encodings to frequently occurring characters Example: e 01 test would be encoded as o 100 s 111 t 00 Challenge: when decoding/decompressing an encoded document, how do we determine the boundaries between characters? example: for the above encoding, how do we know whether the next character is 2 bits or 3 bits? One requirement: no character s encoding can be the prefix of another character s encoding (e.g., couldn t have 00 and 001). Huffman Encoding Huffman encoding is a type of variablelength encoding that is based on the actual character frequencies in a given document. Huffman encoding uses a binary tree: to determine the encoding of each character to decode an encoded file i.e., to decompress a compressed file, putting it back into ASCII Example of a Huffman tree (for a text with only six chars): Leaf nodes are characters. 0 1 Left branches are labeled with a 0, and right branches are labeled with a 1. t e If you follow a path from root to leaf, you get the encoding o i a s of the character in the leaf example: 101 = i

14 Building a Huffman Tree 1) Begin by reading through the text to determine the frequencies. 2) Create a list of nodes that contain (character, frequency) pairs for each character that appears in the text. o 21 i 23 a 2 s t 2 e 40 3) Remove and merge the nodes with the two lowest frequencies, forming a new node that is their parent. left child = lowest frequency node right child = the other node frequency of parent = sum of the frequencies of its children in this case, = 44 o i 23 Building a Huffman Tree (cont.) 4) Add the parent to the list of nodes: a 2 s t 2 e o 21 i 23 ) Repeat steps 3 and 4 until there is only a single node in the list, which will be the root of the Huffman tree.

15 Completing the Huffman Tree Example I Merge the two remaining nodes with the lowest frequencies: a s t e o 21 i 23 t 2 e o 21 i 23 a 2 s Completing the Huffman Tree Example II Merge the next two nodes: t e o 21 i 23 a 2 s o 21 i 23 a 2 s t 2 e 40

16 Completing the Huffman Tree Example III Merge again: o i a s t e t 2 e o i a s Completing the Huffman Tree Example IV The next merge creates the final tree: t 2 6 e t 1 e o i a s o i a s Characters that appear more frequently end up higher in the tree, and thus their encodings are shorter.

18 Binary Search Trees Searchtree property: for each node k: all nodes in k s left subtree are < k all nodes in k s right subtree are >= k Our earlier binarytree example is a search tree: < k k k < < 12 Searching for an Item in a Binary Search Tree Algorithm for searching for an item with a key k: if k == the root node s key, you re done else if k < the root node s key, search the left subtree else search the right subtree Example: search for

19 Implementing BinaryTree Search public class LinkedTree { // Nodes have keys that are ints private Node root; public LLList search(int key) { Node n = searchtree(root, key); return (n == null? null : n.data); private static Node searchtree(node root, int key) { // write together If we find a node that has the specified key, we return its data field, which holds a list of the data items for that key. Inserting an Item in a Binary Search Tree We want to insert an item whose key is k. We traverse the tree as if we were searching for k. If we find a node with key k, we add the data item to the list of items for that node. If we don t find it, the last node we encounter will be the parent P of the new node. if k < P s key, make the new node P s left child else make the node P s right child Special case: if the tree is empty, make the new node the root of the tree. The resulting tree is still a search tree. example: insert P

20 Implementing BinaryTree Insertion We'll implement part of the insert() method together. We'll use iteration rather than recursion. Our method will use two references/pointers: trav: performs the traversal down to the point of insertion parent: stays one behind trav like the trail reference that we sometimes use when traversing a linked list parent trav Implementing BinaryTree Insertion public void insert(int key, Object data) { Node parent = null; Node trav = root; while (trav!= null) { if (trav.key == key) { trav.data.additem(data, 0); return; Node newnode = new Node(key, data); if (parent == null) // the tree was empty root = newnode; else if (key < parent.key) parent.left = newnode; else parent.right = newnode;

21 Deleting Items from a Binary Search Tree Three cases for deleting a node x Case 1: x has no children. Remove x from the tree by setting its parent s reference to null. ex: delete Case 2: x has one child. Take the parent s reference to x and make it refer to x s child. ex: delete Deleting Items from a Binary Search Tree (cont.) Case 3: x has two children we can't just delete x. why? instead, we replace x with a node from elsewhere in the tree to maintain the searchtree property, we must choose the replacement carefully example: what nodes could replace below?

22 Deleting Items from a Binary Search Tree (cont.) Case 3: x has two children (continued): replace x with the smallest node in x s right subtree call it y y will either be a leaf node or will have one right child. why? After copying y s item into x, we delete y using case 1 or 2. ex: delete x 30 x y 30 y Implementing BinaryTree Deletion public LLList delete(int key) { // Find the node and its parent. Node parent = null; Node trav = root; while (trav!= null && trav.key!= key) { parent parent = trav; if (key < trav.key) trav = trav.left; trav else trav = trav.right; // Delete the node (if any) and return the removed items. if (trav == null) // no such key return null; else { LLList removeddata = trav.data; deletenode(trav, parent); return removeddata; This method uses a helper method to delete the node.

23 Implementing Case 3 private void deletenode(node todelete, Node parent) { if (todelete.left!= null && todelete.right!= null) { // Find a replacement and // the replacement's parent. Node replaceparent = todelete; // Get the smallest item // in the right subtree. Node replace = todelete.right; // What should go here? 18 4 todelete // Replace todelete's key and data // with those of the replacement item. todelete.key = replace.key; todelete.data = replace.data; // Recursively delete the replacement // item's old node. It has at most one // child, so we don't have to // worry about infinite recursion. deletenode(replace, replaceparent); else { Implementing Cases 1 and 2 private void deletenode(node todelete, Node parent) { if (todelete.left!= null && todelete.right!= null) {... else { Node todeletechild; if (todelete.left!= null) todeletechild = todelete.left; 30 else todeletechild = todelete.right; // Note: in case 1, todeletechild // will have a value of null if (todelete == root) root = todeletechild; else if (todelete.key < parent.key) parent.left = todeletechild; else parent.right = todeletechild; 30 3 parent todelete todeletechild

24 Efficiency of a Binary Search Tree The three key operations (search, insert, and delete) all have the same time complexity. insert and delete both involve a search followed by a constant number of additional operations Time complexity of searching a binary search tree: best case: O(1) worst case: O(h), where h is the height of the tree average case: O(h) What is the height of a tree containing n items? it depends! why? Balanced Trees A tree is balanced if, for each node, the node s subtrees have the same height or have heights that differ by 1. For a balanced tree with n nodes: height = O(log 2 n). gives a worstcase time complexity that is logarithmic (O(log 2 n)) the best worstcase time complexity for a binary tree

25 What If the Tree Isn't Balanced? Extreme case: the tree is equivalent to a linked list height = n 1 4 worstcase time complexity = O(n) 12 We ll look next at searchtree variants that take special measures to ensure balance

### root node level: internal node edge leaf node CS@VT Data Structures & Algorithms 2000-2009 McQuain

inary Trees 1 A binary tree is either empty, or it consists of a node called the root together with two binary trees called the left subtree and the right subtree of the root, which are disjoint from each

### Questions 1 through 25 are worth 2 points each. Choose one best answer for each.

Questions 1 through 25 are worth 2 points each. Choose one best answer for each. 1. For the singly linked list implementation of the queue, where are the enqueues and dequeues performed? c a. Enqueue in

### Binary Heap Algorithms

CS Data Structures and Algorithms Lecture Slides Wednesday, April 5, 2009 Glenn G. Chappell Department of Computer Science University of Alaska Fairbanks CHAPPELLG@member.ams.org 2005 2009 Glenn G. Chappell

### Analysis of Algorithms I: Binary Search Trees

Analysis of Algorithms I: Binary Search Trees Xi Chen Columbia University Hash table: A data structure that maintains a subset of keys from a universe set U = {0, 1,..., p 1} and supports all three dictionary

### Converting a Number from Decimal to Binary

Converting a Number from Decimal to Binary Convert nonnegative integer in decimal format (base 10) into equivalent binary number (base 2) Rightmost bit of x Remainder of x after division by two Recursive

### 1 2-3 Trees: The Basics

CS10: Data Structures and Object-Oriented Design (Fall 2013) November 1, 2013: 2-3 Trees: Inserting and Deleting Scribes: CS 10 Teaching Team Lecture Summary In this class, we investigated 2-3 Trees in

### Binary Search Trees (BST)

Binary Search Trees (BST) 1. Hierarchical data structure with a single reference to node 2. Each node has at most two child nodes (a left and a right child) 3. Nodes are organized by the Binary Search

### Learning Outcomes. COMP202 Complexity of Algorithms. Binary Search Trees and Other Search Trees

Learning Outcomes COMP202 Complexity of Algorithms Binary Search Trees and Other Search Trees [See relevant sections in chapters 2 and 3 in Goodrich and Tamassia.] At the conclusion of this set of lecture

### Binary Search Trees. A Generic Tree. Binary Trees. Nodes in a binary search tree ( B-S-T) are of the form. P parent. Key. Satellite data L R

Binary Search Trees A Generic Tree Nodes in a binary search tree ( B-S-T) are of the form P parent Key A Satellite data L R B C D E F G H I J The B-S-T has a root node which is the only node whose parent

### Previous Lectures. B-Trees. External storage. Two types of memory. B-trees. Main principles

B-Trees Algorithms and data structures for external memory as opposed to the main memory B-Trees Previous Lectures Height balanced binary search trees: AVL trees, red-black trees. Multiway search trees:

### Ordered Lists and Binary Trees

Data Structures and Algorithms Ordered Lists and Binary Trees Chris Brooks Department of Computer Science University of San Francisco Department of Computer Science University of San Francisco p.1/62 6-0:

### Binary Search Trees > = Binary Search Trees 1. 2004 Goodrich, Tamassia

Binary Search Trees < 6 2 > = 1 4 8 9 Binary Search Trees 1 Binary Search Trees A binary search tree is a binary tree storing keyvalue entries at its internal nodes and satisfying the following property:

### The following themes form the major topics of this chapter: The terms and concepts related to trees (Section 5.2).

CHAPTER 5 The Tree Data Model There are many situations in which information has a hierarchical or nested structure like that found in family trees or organization charts. The abstraction that models hierarchical

### Balanced Binary Search Tree

AVL Trees / Slide 1 Balanced Binary Search Tree Worst case height of binary search tree: N-1 Insertion, deletion can be O(N) in the worst case We want a tree with small height Height of a binary tree with

### Class Notes CS 3137. 1 Creating and Using a Huffman Code. Ref: Weiss, page 433

Class Notes CS 3137 1 Creating and Using a Huffman Code. Ref: Weiss, page 433 1. FIXED LENGTH CODES: Codes are used to transmit characters over data links. You are probably aware of the ASCII code, a fixed-length

### 1) The postfix expression for the infix expression A+B*(C+D)/F+D*E is ABCD+*F/DE*++

Answer the following 1) The postfix expression for the infix expression A+B*(C+D)/F+D*E is ABCD+*F/DE*++ 2) Which data structure is needed to convert infix notations to postfix notations? Stack 3) The

### Data Structure [Question Bank]

Unit I (Analysis of Algorithms) 1. What are algorithms and how they are useful? 2. Describe the factor on best algorithms depends on? 3. Differentiate: Correct & Incorrect Algorithms? 4. Write short note:

### DATA STRUCTURES USING C

DATA STRUCTURES USING C QUESTION BANK UNIT I 1. Define data. 2. Define Entity. 3. Define information. 4. Define Array. 5. Define data structure. 6. Give any two applications of data structures. 7. Give

### Data Structure with C

Subject: Data Structure with C Topic : Tree Tree A tree is a set of nodes that either:is empty or has a designated node, called the root, from which hierarchically descend zero or more subtrees, which

### A binary search tree is a binary tree with a special property called the BST-property, which is given as follows:

Chapter 12: Binary Search Trees A binary search tree is a binary tree with a special property called the BST-property, which is given as follows: For all nodes x and y, if y belongs to the left subtree

### Chapter 14 The Binary Search Tree

Chapter 14 The Binary Search Tree In Chapter 5 we discussed the binary search algorithm, which depends on a sorted vector. Although the binary search, being in O(lg(n)), is very efficient, inserting a

### Operations: search;; min;; max;; predecessor;; successor. Time O(h) with h height of the tree (more on later).

Binary search tree Operations: search;; min;; max;; predecessor;; successor. Time O(h) with h height of the tree (more on later). Data strutcure fields usually include for a given node x, the following

### Binary Search Trees CMPSC 122

Binary Search Trees CMPSC 122 Note: This notes packet has significant overlap with the first set of trees notes I do in CMPSC 360, but goes into much greater depth on turning BSTs into pseudocode than

### TREE BASIC TERMINOLOGIES

TREE Trees are very flexible, versatile and powerful non-liner data structure that can be used to represent data items possessing hierarchical relationship between the grand father and his children and

### Data Structures and Algorithms

Data Structures and Algorithms CS245-2016S-06 Binary Search Trees David Galles Department of Computer Science University of San Francisco 06-0: Ordered List ADT Operations: Insert an element in the list

### B-Trees. Algorithms and data structures for external memory as opposed to the main memory B-Trees. B -trees

B-Trees Algorithms and data structures for external memory as opposed to the main memory B-Trees Previous Lectures Height balanced binary search trees: AVL trees, red-black trees. Multiway search trees:

### Sorting revisited. Build the binary search tree: O(n^2) Traverse the binary tree: O(n) Total: O(n^2) + O(n) = O(n^2)

Sorting revisited How did we use a binary search tree to sort an array of elements? Tree Sort Algorithm Given: An array of elements to sort 1. Build a binary search tree out of the elements 2. Traverse

### A binary search tree or BST is a binary tree that is either empty or in which the data element of each node has a key, and:

Binary Search Trees 1 The general binary tree shown in the previous chapter is not terribly useful in practice. The chief use of binary trees is for providing rapid access to data (indexing, if you will)

### Binary Search Trees. Data in each node. Larger than the data in its left child Smaller than the data in its right child

Binary Search Trees Data in each node Larger than the data in its left child Smaller than the data in its right child FIGURE 11-6 Arbitrary binary tree FIGURE 11-7 Binary search tree Data Structures Using

### UNIVERSITI MALAYSIA SARAWAK KOTA SAMARAHAN SARAWAK PSD2023 ALGORITHM & DATA STRUCTURE

STUDENT IDENTIFICATION NO UNIVERSITI MALAYSIA SARAWAK 94300 KOTA SAMARAHAN SARAWAK FAKULTI SAINS KOMPUTER & TEKNOLOGI MAKLUMAT (Faculty of Computer Science & Information Technology) Diploma in Multimedia

### Binary Heaps * * * * * * * / / \ / \ / \ / \ / \ * * * * * * * * * * * / / \ / \ / / \ / \ * * * * * * * * * *

Binary Heaps A binary heap is another data structure. It implements a priority queue. Priority Queue has the following operations: isempty add (with priority) remove (highest priority) peek (at highest

### Binary Search Trees 3/20/14

Binary Search Trees 3/0/4 Presentation for use ith the textbook Data Structures and Algorithms in Java, th edition, by M. T. Goodrich, R. Tamassia, and M. H. Goldasser, Wiley, 04 Binary Search Trees 4

### 12 Abstract Data Types

12 Abstract Data Types 12.1 Source: Foundations of Computer Science Cengage Learning Objectives After studying this chapter, the student should be able to: Define the concept of an abstract data type (ADT).

### Algorithms and Data Structures

Algorithms and Data Structures Part 2: Data Structures PD Dr. rer. nat. habil. Ralf-Peter Mundani Computation in Engineering (CiE) Summer Term 2016 Overview general linked lists stacks queues trees 2 2

### Hash Tables. Computer Science E-119 Harvard Extension School Fall 2012 David G. Sullivan, Ph.D. Data Dictionary Revisited

Hash Tables Computer Science E-119 Harvard Extension School Fall 2012 David G. Sullivan, Ph.D. Data Dictionary Revisited We ve considered several data structures that allow us to store and search for data

### Chapter 8: Binary Trees

Chapter 8: Binary Trees Why Use Binary Trees? Tree Terminology An Analogy How Do Binary Search Trees Work Finding a Node Inserting a Node Traversing the Tree Finding Maximum and Minimum Values Deleting

### Heaps & Priority Queues in the C++ STL 2-3 Trees

Heaps & Priority Queues in the C++ STL 2-3 Trees CS 3 Data Structures and Algorithms Lecture Slides Friday, April 7, 2009 Glenn G. Chappell Department of Computer Science University of Alaska Fairbanks

### CS 253: Algorithms. Chapter 12. Binary Search Trees. * Deletion and Problems. Credit: Dr. George Bebis

CS 2: Algorithms Chapter Binary Search Trees * Deletion and Problems Credit: Dr. George Bebis Binary Search Trees Tree representation: A linked data structure in which each node is an object Node representation:

### Huffman Assignment Intro

Huffman Assignment Intro James Wei Professor Peck April 2, 2014 Slides by James Wei Outline Intro to Huffman The Huffman algorithm Classes of Huffman Understanding the design Implementation Analysis Grading

### Lecture 6: Binary Search Trees CSCI 700 - Algorithms I. Andrew Rosenberg

Lecture 6: Binary Search Trees CSCI 700 - Algorithms I Andrew Rosenberg Last Time Linear Time Sorting Counting Sort Radix Sort Bucket Sort Today Binary Search Trees Data Structures Data structure is a

### DNS LOOKUP SYSTEM DATA STRUCTURES AND ALGORITHMS PROJECT REPORT

DNS LOOKUP SYSTEM DATA STRUCTURES AND ALGORITHMS PROJECT REPORT By GROUP Avadhut Gurjar Mohsin Patel Shraddha Pandhe Page 1 Contents 1. Introduction... 3 2. DNS Recursive Query Mechanism:...5 2.1. Client

### Information, Entropy, and Coding

Chapter 8 Information, Entropy, and Coding 8. The Need for Data Compression To motivate the material in this chapter, we first consider various data sources and some estimates for the amount of data associated

### 10CS35: Data Structures Using C

CS35: Data Structures Using C QUESTION BANK REVIEW OF STRUCTURES AND POINTERS, INTRODUCTION TO SPECIAL FEATURES OF C OBJECTIVE: Learn : Usage of structures, unions - a conventional tool for handling a

### Algorithms Chapter 12 Binary Search Trees

Algorithms Chapter 1 Binary Search Trees Outline Assistant Professor: Ching Chi Lin 林 清 池 助 理 教 授 chingchi.lin@gmail.com Department of Computer Science and Engineering National Taiwan Ocean University

### Output: 12 18 30 72 90 87. struct treenode{ int data; struct treenode *left, *right; } struct treenode *tree_ptr;

50 20 70 10 30 69 90 14 35 68 85 98 16 22 60 34 (c) Execute the algorithm shown below using the tree shown above. Show the exact output produced by the algorithm. Assume that the initial call is: prob3(root)

### From Last Time: Remove (Delete) Operation

CSE 32 Lecture : More on Search Trees Today s Topics: Lazy Operations Run Time Analysis of Binary Search Tree Operations Balanced Search Trees AVL Trees and Rotations Covered in Chapter of the text From

### Analysis of Algorithms I: Optimal Binary Search Trees

Analysis of Algorithms I: Optimal Binary Search Trees Xi Chen Columbia University Given a set of n keys K = {k 1,..., k n } in sorted order: k 1 < k 2 < < k n we wish to build an optimal binary search

### Outline BST Operations Worst case Average case Balancing AVL Red-black B-trees. Binary Search Trees. Lecturer: Georgy Gimel farb

Binary Search Trees Lecturer: Georgy Gimel farb COMPSCI 220 Algorithms and Data Structures 1 / 27 1 Properties of Binary Search Trees 2 Basic BST operations The worst-case time complexity of BST operations

### Home Page. Data Structures. Title Page. Page 1 of 24. Go Back. Full Screen. Close. Quit

Data Structures Page 1 of 24 A.1. Arrays (Vectors) n-element vector start address + ielementsize 0 +1 +2 +3 +4... +n-1 start address continuous memory block static, if size is known at compile time dynamic,

### Binary Heaps. CSE 373 Data Structures

Binary Heaps CSE Data Structures Readings Chapter Section. Binary Heaps BST implementation of a Priority Queue Worst case (degenerate tree) FindMin, DeleteMin and Insert (k) are all O(n) Best case (completely

### Parallelization: Binary Tree Traversal

By Aaron Weeden and Patrick Royal Shodor Education Foundation, Inc. August 2012 Introduction: According to Moore s law, the number of transistors on a computer chip doubles roughly every two years. First

### ER E P M A S S I CONSTRUCTING A BINARY TREE EFFICIENTLYFROM ITS TRAVERSALS DEPARTMENT OF COMPUTER SCIENCE UNIVERSITY OF TAMPERE REPORT A-1998-5

S I N S UN I ER E P S I T VER M A TA S CONSTRUCTING A BINARY TREE EFFICIENTLYFROM ITS TRAVERSALS DEPARTMENT OF COMPUTER SCIENCE UNIVERSITY OF TAMPERE REPORT A-1998-5 UNIVERSITY OF TAMPERE DEPARTMENT OF

### Data Structures. Level 6 C30151. www.fetac.ie. Module Descriptor

The Further Education and Training Awards Council (FETAC) was set up as a statutory body on 11 June 2001 by the Minister for Education and Science. Under the Qualifications (Education & Training) Act,

### 1. The memory address of the first element of an array is called A. floor address B. foundation addressc. first address D.

1. The memory address of the first element of an array is called A. floor address B. foundation addressc. first address D. base address 2. The memory address of fifth element of an array can be calculated

### PES Institute of Technology-BSC QUESTION BANK

PES Institute of Technology-BSC Faculty: Mrs. R.Bharathi CS35: Data Structures Using C QUESTION BANK UNIT I -BASIC CONCEPTS 1. What is an ADT? Briefly explain the categories that classify the functions

### Tree Properties (size vs height) Balanced Binary Trees

Tree Properties (size vs height) Balanced Binary Trees Due now: WA 7 (This is the end of the grace period). Note that a grace period takes the place of a late day. Now is the last time you can turn this

### AS-2261 M.Sc.(First Semester) Examination-2013 Paper -fourth Subject-Data structure with algorithm

AS-2261 M.Sc.(First Semester) Examination-2013 Paper -fourth Subject-Data structure with algorithm Time: Three Hours] [Maximum Marks: 60 Note Attempts all the questions. All carry equal marks Section A

### The Union-Find Problem Kruskal s algorithm for finding an MST presented us with a problem in data-structure design. As we looked at each edge,

The Union-Find Problem Kruskal s algorithm for finding an MST presented us with a problem in data-structure design. As we looked at each edge, cheapest first, we had to determine whether its two endpoints

### Big Data and Scripting. Part 4: Memory Hierarchies

1, Big Data and Scripting Part 4: Memory Hierarchies 2, Model and Definitions memory size: M machine words total storage (on disk) of N elements (N is very large) disk size unlimited (for our considerations)

### Image Compression through DCT and Huffman Coding Technique

International Journal of Current Engineering and Technology E-ISSN 2277 4106, P-ISSN 2347 5161 2015 INPRESSCO, All Rights Reserved Available at http://inpressco.com/category/ijcet Research Article Rahul

### 2. (a) Explain the strassen s matrix multiplication. (b) Write deletion algorithm, of Binary search tree. [8+8]

Code No: R05220502 Set No. 1 1. (a) Describe the performance analysis in detail. (b) Show that f 1 (n)+f 2 (n) = 0(max(g 1 (n), g 2 (n)) where f 1 (n) = 0(g 1 (n)) and f 2 (n) = 0(g 2 (n)). [8+8] 2. (a)

### Symbol Tables. Introduction

Symbol Tables Introduction A compiler needs to collect and use information about the names appearing in the source program. This information is entered into a data structure called a symbol table. The

### Merge Sort. 2004 Goodrich, Tamassia. Merge Sort 1

Merge Sort 7 2 9 4 2 4 7 9 7 2 2 7 9 4 4 9 7 7 2 2 9 9 4 4 Merge Sort 1 Divide-and-Conquer Divide-and conquer is a general algorithm design paradigm: Divide: divide the input data S in two disjoint subsets

### Data Structures, Practice Homework 3, with Solutions (not to be handed in)

Data Structures, Practice Homework 3, with Solutions (not to be handed in) 1. Carrano, 4th edition, Chapter 9, Exercise 1: What is the order of each of the following tasks in the worst case? (a) Computing

### Cpt S 223. School of EECS, WSU

Priority Queues (Heaps) 1 Motivation Queues are a standard mechanism for ordering tasks on a first-come, first-served basis However, some tasks may be more important or timely than others (higher priority)

### B+ Tree Properties B+ Tree Searching B+ Tree Insertion B+ Tree Deletion Static Hashing Extendable Hashing Questions in pass papers

B+ Tree and Hashing B+ Tree Properties B+ Tree Searching B+ Tree Insertion B+ Tree Deletion Static Hashing Extendable Hashing Questions in pass papers B+ Tree Properties Balanced Tree Same height for paths

### OPTIMAL BINARY SEARCH TREES

OPTIMAL BINARY SEARCH TREES 1. PREPARATION BEFORE LAB DATA STRUCTURES An optimal binary search tree is a binary search tree for which the nodes are arranged on levels such that the tree cost is minimum.

### GRAPH THEORY LECTURE 4: TREES

GRAPH THEORY LECTURE 4: TREES Abstract. 3.1 presents some standard characterizations and properties of trees. 3.2 presents several different types of trees. 3.7 develops a counting method based on a bijection

### International Journal of Advanced Research in Computer Science and Software Engineering

Volume 3, Issue 7, July 23 ISSN: 2277 28X International Journal of Advanced Research in Computer Science and Software Engineering Research Paper Available online at: www.ijarcsse.com Greedy Algorithm:

### In our lecture about Binary Search Trees (BST) we have seen that the operations of searching for and inserting an element in a BST can be of the

In our lecture about Binary Search Trees (BST) we have seen that the operations of searching for and inserting an element in a BST can be of the order O(log n) (in the best case) and of the order O(n)

### Algorithms and Data Structures Written Exam Proposed SOLUTION

Algorithms and Data Structures Written Exam Proposed SOLUTION 2005-01-07 from 09:00 to 13:00 Allowed tools: A standard calculator. Grading criteria: You can get at most 30 points. For an E, 15 points are

### A binary heap is a complete binary tree, where each node has a higher priority than its children. This is called heap-order property

CmSc 250 Intro to Algorithms Chapter 6. Transform and Conquer Binary Heaps 1. Definition A binary heap is a complete binary tree, where each node has a higher priority than its children. This is called

### Intro. to the Divide-and-Conquer Strategy via Merge Sort CMPSC 465 CLRS Sections 2.3, Intro. to and various parts of Chapter 4

Intro. to the Divide-and-Conquer Strategy via Merge Sort CMPSC 465 CLRS Sections 2.3, Intro. to and various parts of Chapter 4 I. Algorithm Design and Divide-and-Conquer There are various strategies we

### DATABASE DESIGN - 1DL400

DATABASE DESIGN - 1DL400 Spring 2015 A course on modern database systems!! http://www.it.uu.se/research/group/udbl/kurser/dbii_vt15/ Kjell Orsborn! Uppsala Database Laboratory! Department of Information

### Krishna Institute of Engineering & Technology, Ghaziabad Department of Computer Application MCA-213 : DATA STRUCTURES USING C

Tutorial#1 Q 1:- Explain the terms data, elementary item, entity, primary key, domain, attribute and information? Also give examples in support of your answer? Q 2:- What is a Data Type? Differentiate

### THE SECURITY AND PRIVACY ISSUES OF RFID SYSTEM

THE SECURITY AND PRIVACY ISSUES OF RFID SYSTEM Iuon Chang Lin Department of Management Information Systems, National Chung Hsing University, Taiwan, Department of Photonics and Communication Engineering,

### Binary Search Trees. Ric Glassey glassey@kth.se

Binary Search Trees Ric Glassey glassey@kth.se Outline Binary Search Trees Aim: Demonstrate how a BST can maintain order and fast performance relative to its height Properties Operations Min/Max Search

### Binary search algorithm

Binary search algorithm Definition Search a sorted array by repeatedly dividing the search interval in half. Begin with an interval covering the whole array. If the value of the search key is less than

### CS711008Z Algorithm Design and Analysis

CS711008Z Algorithm Design and Analysis Lecture 7 Binary heap, binomial heap, and Fibonacci heap 1 Dongbo Bu Institute of Computing Technology Chinese Academy of Sciences, Beijing, China 1 The slides were

### Binary Coded Web Access Pattern Tree in Education Domain

Binary Coded Web Access Pattern Tree in Education Domain C. Gomathi P.G. Department of Computer Science Kongu Arts and Science College Erode-638-107, Tamil Nadu, India E-mail: kc.gomathi@gmail.com M. Moorthi

### External Memory Geometric Data Structures

External Memory Geometric Data Structures Lars Arge Department of Computer Science University of Aarhus and Duke University Augues 24, 2005 1 Introduction Many modern applications store and process datasets

### AP Computer Science AB Syllabus 1

AP Computer Science AB Syllabus 1 Course Resources Java Software Solutions for AP Computer Science, J. Lewis, W. Loftus, and C. Cocking, First Edition, 2004, Prentice Hall. Video: Sorting Out Sorting,

### Tables so far. set() get() delete() BST Average O(lg n) O(lg n) O(lg n) Worst O(n) O(n) O(n) RB Tree Average O(lg n) O(lg n) O(lg n)

Hash Tables Tables so far set() get() delete() BST Average O(lg n) O(lg n) O(lg n) Worst O(n) O(n) O(n) RB Tree Average O(lg n) O(lg n) O(lg n) Worst O(lg n) O(lg n) O(lg n) Table naïve array implementation

### Algorithms and Data Structures

Algorithms and Data Structures CMPSC 465 LECTURES 20-21 Priority Queues and Binary Heaps Adam Smith S. Raskhodnikova and A. Smith. Based on slides by C. Leiserson and E. Demaine. 1 Trees Rooted Tree: collection

### Data Structures. Jaehyun Park. CS 97SI Stanford University. June 29, 2015

Data Structures Jaehyun Park CS 97SI Stanford University June 29, 2015 Typical Quarter at Stanford void quarter() { while(true) { // no break :( task x = GetNextTask(tasks); process(x); // new tasks may

### MAX = 5 Current = 0 'This will declare an array with 5 elements. Inserting a Value onto the Stack (Push) -----------------------------------------

=============================================================================================================================== DATA STRUCTURE PSEUDO-CODE EXAMPLES (c) Mubashir N. Mir - www.mubashirnabi.com

### Exercises Software Development I. 11 Recursion, Binary (Search) Trees. Towers of Hanoi // Tree Traversal. January 16, 2013

Exercises Software Development I 11 Recursion, Binary (Search) Trees Towers of Hanoi // Tree Traversal January 16, 2013 Software Development I Winter term 2012/2013 Institute for Pervasive Computing Johannes

### 6 March 2007 1. Array Implementation of Binary Trees

Heaps CSE 0 Winter 00 March 00 1 Array Implementation of Binary Trees Each node v is stored at index i defined as follows: If v is the root, i = 1 The left child of v is in position i The right child of

### 6 Creating the Animation

6 Creating the Animation Now that the animation can be represented, stored, and played back, all that is left to do is understand how it is created. This is where we will use genetic algorithms, and this

### Rotation Operation for Binary Search Trees Idea:

Rotation Operation for Binary Search Trees Idea: Change a few pointers at a particular place in the tree so that one subtree becomes less deep in exchange for another one becoming deeper. A sequence of

### Interconnection Networks. Interconnection Networks. Interconnection networks are used everywhere!

Interconnection Networks Interconnection Networks Interconnection networks are used everywhere! Supercomputers connecting the processors Routers connecting the ports can consider a router as a parallel

### A TOOL FOR DATA STRUCTURE VISUALIZATION AND USER-DEFINED ALGORITHM ANIMATION

A TOOL FOR DATA STRUCTURE VISUALIZATION AND USER-DEFINED ALGORITHM ANIMATION Tao Chen 1, Tarek Sobh 2 Abstract -- In this paper, a software application that features the visualization of commonly used

### A binary search tree or BST is a binary tree that is either empty or in which the data element of each node has a key, and:

1 The general binary tree shown in the previous chapter is not terribly useful in practice. The chief use of binary trees is for providing rapid access to data (indexing, if you will) and the general binary

### character E T A S R I O D frequency

Data Compression Data compression is any process by which a digital (e.g. electronic) file may be transformed to another ( compressed ) file, such that the original file may be fully recovered from the

### Binary Trees (1) Outline and Required Reading: Binary Trees ( 6.3) Data Structures for Representing Trees ( 6.4)

1 Binary Trees (1) Outline and Required Reading: Binary Trees ( 6.3) Data Structures for Representing Trees ( 6.4) COSC 2011, Fall 2003, Section A Instructor: N. Vlajic Binary Tree 2 Binary Tree tree with

### GUJARAT TECHNOLOGICAL UNIVERSITY, AHMEDABAD, GUJARAT. Course Curriculum. DATA STRUCTURES (Code: 3330704)

GUJARAT TECHNOLOGICAL UNIVERSITY, AHMEDABAD, GUJARAT Course Curriculum DATA STRUCTURES (Code: 3330704) Diploma Programme in which this course is offered Semester in which offered Computer Engineering,

### Exam study sheet for CS2711. List of topics

Exam study sheet for CS2711 Here is the list of topics you need to know for the final exam. For each data structure listed below, make sure you can do the following: 1. Give an example of this data structure

### CSE 326: Data Structures B-Trees and B+ Trees

Announcements (4//08) CSE 26: Data Structures B-Trees and B+ Trees Brian Curless Spring 2008 Midterm on Friday Special office hour: 4:-5: Thursday in Jaech Gallery (6 th floor of CSE building) This is

### Quiz 4 Solutions EECS 211: FUNDAMENTALS OF COMPUTER PROGRAMMING II. 1 Q u i z 4 S o l u t i o n s

Quiz 4 Solutions Q1: What value does function mystery return when called with a value of 4? int mystery ( int number ) { if ( number