Chapter 8: Binary Trees

Save this PDF as:

Size: px
Start display at page:

Transcription

2 Why Use Binary Trees? It combines the advantages of two other structures: an ordered array and a linked list. You can search a tree quickly, as you can an ordered array, And you can also insert and delete items quickly, as you can with a linked list. Slow Insertion in an Ordered Array Imagine an array in which all the elements are arranged in order; that is, an ordered array (see Chapter 3) It's quick to search such an array for a particular value, using a binary search. You check in the center of the array; if the object you're looking for is greater than what you find there, you narrow your search to the top half of the array; if it's less, you narrow your search to the bottom half. Applying this process repeatedly finds the object in O(logN) time. It's also quick to iterate through an ordered array, visiting each object in sorted order. 2

3 Slow Insertion in an Ordered Array On the other hand, if you want to insert a new object into an ordered array, you first need to find where the object will go, and then move all the objects with greater keys up one space in the array to make room for it. These multiple moves are time consuming, requiring, on the average, moving half the items (N/2 moves). Deletion involves the same multimove operation, and is thus equally slow. If you're going to be doing a lot of insertions and deletions, an ordered array is a bad choice. Slow Searching in a Linked List On the other hand, as we saw in Chapter 7, "Advanced Sorting, insertions and deletions are quick to perform on a linked list. They are accomplished simply by changing a few references. These operations require O(1) time (the fastest Big-O time). Unfortunately 3

4 Slow Searching in a Linked List Unfortunately, however, finding a specified element in a linked list is not so easy. You must start at the beginning of the list and visit each element until you find the one you're looking for. Thus you will need to visit an average of N/2 objects, comparing each one's key with the desired value. This is slow, requiring O(N) time. (Notice that times considered fast for a sort are slow for data structure operations.) Slow Searching in a Linked List You might think you could speed things up by using an ordered linked list, in which the elements were arranged in order, but this doesn't help. You still must start at the beginning and visit the elements in order,» because there's no way to access a given element without following the chain of references to it. (Of course, in an ordered list it's much quicker to visit the nodes in order than it is in a non-ordered list, but that doesn't help to find an arbitrary object.) 4

5 Trees to the Rescue What Is a Tree? A tree consists of nodes connected by edges. Figure 8.1 below shows a tree. In such a picture of a tree (or in our Workshop applet) the nodes are represented as circles, the edges as lines connecting the circles. Trees have been studied extensively as abstract mathematical entities, so there's a large amount of theoretical knowledge about them. A tree is actually an instance of a more general category called a graph Chapters 13 Graphs Chapters 14 Weighted Graphs 5

6 What Is a Tree? In computer programs, nodes often represent such entities as people, car parts, airline reservations, and so on; in other words, the typical items we store in any kind of data structure. In an OOP language such as Java, these real-world entities are represented by objects. The lines (edges) between the nodes represent the way the nodes are related. Roughly speaking, the lines represent convenience: It's easy (and fast) for a program to get from one node to another if there is a line connecting them. In fact, the only way to get from node to node is to follow a path along the lines. Generally you are restricted to going in one direction along edges: from the root downward. Edges are likely to be represented in a program by references, if the program is written in Java (or by pointers if the program is written in C or C++). Tree (example) node edge 6

7 What Is a Tree? Typically there is one node in the top row of a tree, with lines connecting to more nodes on the second row, even more on the third, and so on. Thus trees are small on the top and large on the bottom. This may seem upside-down compared with real trees, but generally a program starts an operation at the small end of the tree, and it's (arguably) more natural to think about going from top to bottom, as in reading text. What Is a Tree? There are different kinds of trees. The tree shown in Figure 8.1 has more than two children per node. Binary tree: Each node in a binary tree has a maximum of two children. More general trees, in which nodes can have more than two children, are called multiway trees. We'll see an example in Chapter 10, "2-3-4 Tables and External Storage," where we discuss trees. 7

8 An Analogy One commonly encountered tree is the hierarchical file structure in a computer system. The root directory of a given device (designated with the backslash, as in C:\, on many systems) is the tree's root. The directories one level below the root directory are its children. There may be many levels of subdirectories. Files represent leaves; they have no children of their own. Clearly a hierarchical file structure is not a binary tree, because a directory may have many children. A complete pathname, such as C:\SALES\EAST\NOVEMBER\SMITH.DAT, corresponds to the path from the root to the SMITH.DAT leaf. Terms used for the file structure, such as root and path, were borrowed from tree theory. An Analogy A hierarchical file structure differs in a significant way from the trees we'll be discussing here. In the file structure, subdirectories contain no data; only references to other subdirectories or to files. Only files contain data. In a tree, every node contains data (a personnel record, car-part specifications, or whatever). In addition to the data, all nodes except leaves contain references to other nodes. 8

9 How Do Binary Trees Work? Common binary-tree operations: finding a node with a given key, inserting a new node, traversing the tree, deleting a node. The Tree Workshop applet Using the Applet Unbalanced Trees (Next slide.) Unbalanced Trees Notice that some of the trees generated are unbalanced; that is, they have most of their nodes on one side of the root or the other, as shown in Figure 8.6. Individual subtrees may also be unbalanced. Figure 8.6: An unbalanced tree (with an unbalanced subtree) 9

10 Unbalanced Trees Trees become unbalanced because of the order in which the data items are inserted. If these key values are inserted randomly, the tree will be more or less balanced. However, if an ascending sequence (like 11, 18, 33, 42, 65, and so on) or a descending sequence is generated, all the values will be right children (if ascending) or left children (if descending) and the tree will be unbalanced. The key values in the Workshop applet are generated randomly, but of course some short ascending or descending sequences will be created anyway, which will lead to local imbalances. When you learn how to insert items into the tree in the Workshop applet you can try building up a tree by inserting such an ordered sequence of items and see what happens. Unbalanced Trees If you ask for a large number of nodes when you use Fill to create a tree, you may not get as many nodes as you requested. Depending on how unbalanced the tree becomes, some branches may not be able to hold a full number of nodes. This is because the depth of the applet's tree is limited to five; the problem would not arise in a real tree. 10

11 Unbalanced Trees If a tree is created by data items whose key values arrive in random order, the problem of unbalanced trees may not be too much of a problem for larger trees, because the chances of a long run of numbers in sequence is small. But key values can arrive in strict sequence; for example, when a data-entry person arranges a stack of personnel files into order of ascending employee number before entering the data. When this happens, tree efficiency can be seriously degraded. We'll discuss unbalanced trees and what to do about them in Chapter 9, "Red-Black Trees." Representing the Tree in Java Code There are several approaches to representing a tree in the computer's memory. The most common (shown next): Store the nodes at unrelated locations in memory Connect them using references in each node that point to its children. It's also possible to represent a tree in memory as an array, with nodes in specific positions stored in corresponding positions in the array (At the end of this chapter.) See Listing 8.1 (Later) 11

12 Object with links Btree Int data Btree left_chid Btree right_child Btree Int data Btree left_chid Btree right_child Find() Insert() Delete() Find() Insert() Delete() Btree Int data Btree left_chid Btree right_child Find() Insert() Delete() Btree Int data Btree left_chid Btree right_child Find() Insert() Delete() The Node Class A class of node objects: These objects contain the data representing the objects being stored (employees in an employee database, for example) And also references to each of the node's two children. class Node int idata; // data used as key value float fdata; // other data node leftchild; // this node's left child node rightchild; // this node's right child public void displaynode() // (see Listing 8.1 for method body) Some programmers also include a reference to the node's parent. This simplifies some operations but complicates others, so we don't include it. We do include a method called displaynode() to display the node's data, but its code isn't relevant here. 12

13 The Node Class There are other approaches to designing class Node. Instead of placing the data items directly into the node, use a reference to an object representing the data item: class Node person p1; // reference to person object node leftchild; // this node's left child node rightchild; // this node's right child class person int idata; float fdata; This makes it conceptually clearer that the node and the data item it holds aren't the same thing, but it results in somewhat more complicated code, We'll stick to the first approach. The Tree Class A class from which to instantiate the tree itself; The object that holds all the nodes. It has only one field: a Node variable that holds the root. It doesn't need fields for the other nodes because they are all accessed from the root. The Tree class has a number of methods: some for finding, inserting, and deleting nodes, several for different kinds of traverses, and one to display the tree. 13

14 The Tree Class class Tree private Node root; // the only data field in Tree public void find(int key) public void insert(int id, double dd) public void delete(int id) // various other methods // end class Tree The TreeApp Class Finally, we need a way to perform operations on the tree: class TreeApp public static void main(string[] args) Tree thetree = new Tree; // make a tree thetree.insert(50, 1.5); // insert 3 nodes thetree.insert(25, 1.7); thetree.insert(75, 1.9); node found = thetree.find(25); // find node with key 25 if(found!= null) System.out.println("Found the node with key 25"); else System.out.println("Could not find node with key 25"); // end main() // end class TreeApp In Listing 8.1 the main() routine provides a primitive user interface so you can decide from the keyboard whether you want to insert, find, delete, or perform other operations. Next, individual tree operations: finding a node, inserting a node, traversing the tree, and deleting a node. 14

15 Finding a Node Using the Workshop Applet: Figure 8.7: Finding node 57 Java Code for Finding a Node public Node find(int key) // find node with given key // (assumes non-empty tree) Node current = root; // start at root while(current.idata!= key) // while no match, if(key < current.idata) // go left? current = current.leftchild; else current = current.rightchild; // or go right? if(current == null) // if no child, return null; // didn't find it return current; // found it // See next slide for explanation. 15

16 Java Code for Finding a Node This routine uses a variable current to hold the node it is currently examining. The argument key is the value to be found. The routine starts at the root. (It has to; this is the only node it can access directly.) That is, it sets current to the root. Then, in the while loop, it compares the value to be found, key, with the value of the idata field (the key field) in the current node. If key is less than this field, then current is set to the node's left child. If key is greater than (or equal) to the node's idata field, then current is set to the node's right child. Can't Find It If current becomes equal to null, then we couldn't find the next child node in the sequence; we've reached the end of the line without finding the node we were looking for, so it can't exist. We return null to indicate this fact. Found It If the condition of the while loop is not satisfied, so that we exit from the bottom of the loop, then the idata field of current is equal to key; that is, we've found the node we want. We return the node, so that the routine that called find() can access any of the node's data. Finding a Node: Efficiency How long it takes to find a node Depends on how many levels down it is situated. In the Workshop applet there can be up to 31 nodes, but no more than 5 levels so you can find any node using a maximum of only 5 comparisons. This is O(logN) time, or more specifically O(log 2 N) time; the logarithm to the base 2. More toward the end of this chapter. 16

17 Inserting a Node First find the place to insert a node: Much the same process as trying to find a node that turns out not to exist, as described in the section on Find. We follow the path from the root to the appropriate node, which will be the parent of the new node. Once this parent is found, the new node is connected as its left or right child, depending on whether the new node's key is less than or greater than that of the parent. Inserting a Node Using the Workshop Applet Figure 8.8: Inserting a node 17

18 Java Code for Inserting a Node The insert() function starts by creating the new node, using the data supplied as arguments. Next, insert() must determine where to insert the new node. This is done using roughly the same code as finding a node, described in the section above on find(). The difference is that when you're simply trying to find a node and you encounter a null (nonexistent) node, you know the node you're looking for doesn't exist so you return immediately. When you're trying to insert a node you insert it (creating it first, if necessary) before returning. The value to be searched for is the data item passed in the argument id. The while loop uses true as its condition because it doesn't care if it encounters a node with the same value as id; it treats another node with the same key value as if it were simply greater than the key value. (The subject of duplicate nodes: later in this chapter.) A place to insert a new node will always be found (unless you run out of memory); when it is, and the new node is attached, the while loop exits with a return statement. Java Code for Inserting a Node public void insert(int id, double dd) Node newnode = new Node(); // make new node newnode.idata = id; // insert data newnode.ddata = dd; if(root==null) // no node in root root = newnode; else // root occupied Node current = root; // start at root Node parent; while(true) // (exits internally) parent = current; if(id < current.idata) // go left? current = current.leftchild; if(current == null) // if end of the line, // insert on left parent.leftchild = newnode; return; // end if go left else // or go right? current = current.rightchild; if(current == null) // if end of the line // insert on right parent.rightchild = newnode; return; // end else go right // end while // end else not root // end insert() 18

19 Java Code for Inserting a Node We use a new variable, parent (the parent of current), to remember the last non-null node we encountered (50 in the figure). This is necessary because current is set to null in the process of discovering that its previous value did not have an appropriate child. If we didn't save parent, we'd lose track of where we were. To insert the new node, change the appropriate child pointer in parent (the last non-null node you encountered) to point to the new node. If you were looking unsuccessfully for parent's left child, you attach the new node as parent's left child if you were looking for its right child, you attach the new node as its right child. In Figure 8.8, 45 is attached as the left child of 50. Traversing the Tree Traversing a tree: visiting each node in a specified order. Not as commonly used as finding, inserting, and deleting nodes. One reason: Traversal is not particularly fast. But traversing a tree is useful in some circumstances and the algorithm is interesting. Three simple ways to traverse a tree: preorder, inorder, Postorder. The order most commonly used for binary search trees is inorder. 19

20 Inorder Traversal An inorder traversal of a binary search tree will cause all the nodes to be visited in ascending order, based on their key values. If we want to create a sorted list of the data in a binary tree, this is one way to do it. The simplest way to carry out a traversal: Recursion A recursive method to traverse the entire tree is called with a node as an argument. Initially, this node is the root. The method needs to do only three things: 1. Call itself to traverse the node's left subtree 2. Visit the node 3. Call itself to traverse the node's right subtree Visiting a node means doing something to it: displaying it, writing it to a file, or whatever. Traversals work with any binary tree, not just with binary search trees. The traversal mechanism doesn't pay any attention to the key values of the nodes; it only concerns itself with whether a node has children. Java Code for Traversing The routine, inorder(), performs the three steps already described. The visit to the node consists of displaying the contents of the node. Base case: When the node passed as an argument is null. private void inorder(node localroot) if(localroot!= null) inorder(localroot.leftchild); localroot.displaynode(); inorder(localroot.rightchild); This method is initially called with the root as an argument: inorder(root); After that, it's on its own, calling itself recursively until there are no more nodes to visit. 20

21 Traversing a 3-Node Tree Traversing a tree with only three nodes: a root (A) with a left child (B) and a right child (C), Figure 8.9: inorder() method applied to 3-node tree Traversing a 3-Node Tree We start by calling inorder() with the root A as an argument. This incarnation of inorder() we'll call inorder(a). inorder(a) first calls inorder() with its left child, B, as an argument. This second incarnation of inorder() we'll call inorder(b). inorder(b) now calls itself with its left child as an argument. However, it has no left child, so this argument is null. This creates an invocation of inorder() we could call inorder(null). There are now three instances of inorder() in existence: inorder(a), inorder(b), and inorder(null). However, inorder(null) returns immediately when it finds its argument is null. Now inorder(b) goes on to visit B; we'll assume this means to display it. Then inorder(b) calls inorder() again, with its right child as an argument. Again this argument is null, so the second inorder(null) returns immediately. Now inorder(b) has carried out steps 1, 2, and 3, so it returns (and thereby ceases to exist). 21

22 Traversing a 3-Node Tree Now we're back to inorder(a), just returning from traversing A's left child. We visit A, and then call inorder() again with C as an argument, creating inorder(c). Like inorder(b), inorder(c) has no children, so step 1 returns with no action, step 2 visits C, step 3 returns with no action. inorder(b) now returns to inorder(a). However, inorder(a) is now done, so it returns and the entire traversal is complete. The order in which the nodes were visited is A, B, C; they have been visited inorder. In a binary search tree this would be the order of ascending keys. More complex trees are handled similarly. The inorder() function calls itself for each node, until it has worked its way through the entire tree. Traversing with the Workshop Applet Figure 8.10: Traversing a tree inorder Also, see Table 8.1: WORKSHOP APPLET TRAVERSAL 22

23 Tree Traversal (continued) Sequence of preorder traversal: e.g. use for infix parse tree to generate prefix -- Visit the node -- Call itself to traverse the node s left subtree -- Call itself to traverse the node s right subtree Sequence of postorder traversal: e.g. use for infix parse tree to generate postfix -- Call itself to traverse the node s left subtree -- Call itself to traverse the node s right subtree -- Visit the node. Preorder and Postorder Traversals A binary tree (not a binary search tree) can be used to represent an algebraic expression that involves the binary arithmetic operators +, -, /, and *. The root node holds an operator, and each of its subtrees represents either a variable name (like A, B, or C) or another expression. For example, the binary tree shown in Figure 8.11 represents the algebraic expression in infix notation A*(B+C) Figure 8.11: Tree representing an algebraic expression Traversing the tree inorder will generate the correct inorder sequence A*B+C, but you'll need to insert the parentheses yourself. 23

24 What's All This Got To Do With Preorder And Postorder Traversals? For these other traversals the same three steps are used as for inorder, but in a different sequence. Here's the sequence for a preorder() method: 1. Visit the node. 2. Call itself to traverse the node's left subtree. 3. Call itself to traverse the node's right subtree. Traversing the tree shown in Figure 8.11 using preorder would generate the expression in prefix notation. *A+BC Preorder Traversal One of the nice things about prefix notation is that parentheses are never required; The expression is unambiguous without them. It means apply the operator * to the next two things in the expression. These two things are A and +BC. The expression +BC means apply + to the next two things in the expression which are B and C, so this last expression is B+C in inorder notation. Inserting that into the original expression *A+BC (preorder) gives us A*(B+C) in inorder. 24

25 Postorder Traversal 1. Call itself to traverse the node's left subtree. 2. Call itself to traverse the node's right subtree. 3. Visit the node. For the tree in Figure 8.11, visiting the nodes with a postorder traversal would generate the expression in postfix notation. ABC+* Postorder Traversal ABC+* As described in Chapter 4, Stacks and Queues, it means apply the last operator in the expression, *, to the first and second things. The first thing is A, and the second thing is BC+. BC+ means apply the last operator in the expression, +, to the first and second things. The first thing is B and the second thing is C, so this gives us (B+C) in infix. Inserting this in the original expression ABC+* (postfix) gives us A*(B+C) postfix. 25

26 Traversals Listing 8.1 contains methods for preorder and postorder traversals, as well as for inorder. Finding Maximum and Minimum Values Figure 8.12: Minimum value of a tree For the minimum, go to the left child of the root and keep going to the left child until you come to a leaf node. This node is the minimum. For the maximum, go to the right child of the root and keep going to the right child until you come to a leaf node. This node is the maximum. 26

27 Finding Minimum Values Figure 8.12: Minimum value of a tree Here's some code that returns the node with the minimum key value: public Node minimum() // returns node with minimum key value Node current, last; current = root; // start at root while(current!= null) // until the bottom, last = current; // remember node current = current.leftchild; // go to left child return last; Deleting a Node Start by finding the node you want to delete, using the same approach we saw in find() and insert() Then there are three cases to consider: 1. The node to be deleted is a leaf (i.e., no child) 2. The node to be deleted has one child 3. The node to be deleted has two children 27

28 Deletion: Case 1 - Leaf Node To delete a leaf node, simply change the appropriate child field in the node s parent to point to null, instead of to the node. The node still exists, but is no longer a part of the tree. Because of Java s garbage collection feature, the node need not be deleted explicitly. In C and C++ you would need to execute free() or delete() to remove the node from memory. Workshop Applet Figure 8.13: Deleting a node with no children Java Code to Delete a Node With No Children public boolean delete(int key) // delete node with given key // (assumes non-empty list) Node current = root; Node parent = root; boolean isleftchild = true; while(current.idata!= key) // search for node parent = current; if(key < current.idata) // go left? isleftchild = true; current = current.leftchild; else // or go right? isleftchild = false; current = current.rightchild; if(current == null) // end of the line, return false; // didn't find it // end while // found node to delete // continues on the next slide... 28

29 Java Code to Delete a Node With No Children (continued) // delete() continued from last slide... // if no children, simply delete it if(current.leftchild==null && current.rightchild==null) if(current == root) // if root, root = null; // tree is empty else if(isleftchild) parent.leftchild = null; // disconnect else // from parent parent.rightchild = null; // Click here to continue Deletion: Case 2 - One Child The node to be deleted in this case has only two connections: to its parent and to its only child. Connect the child of the node to the node s parent, thus cutting off the connection between the node and its child, and between the node and its parent. Using the Workshop Applet Figure 8.14: Deleting a node with one child 29

30 Java Code to Delete a Node With One Child // delete() continued... (Click here to go back to lines above.) // if no right child, replace with left subtree else if(current.rightchild==null) if(current == root) root = current.leftchild; else if(isleftchild) // left child of parent parent.leftchild = current.leftchild; else // right child of parent parent.rightchild = current.leftchild; // if no left child, replace with right subtree else if(current.leftchild==null) if(current == root) root = current.rightchild; else if(isleftchild) // left child of parent parent.leftchild = current.rightchild; else // right child of parent parent.rightchild = current.rightchild; // continued... Working with references makes it easy to move an entire subtree: Simply disconnecting the old reference to the subtree and creating a new reference to it somewhere else. Deletion: Case 3 - Two Children To delete a node with two children, replace the node with its inorder successor. For each node, the node with the next-highest key (to the deleted node) in the subtree is called its inorder successor. 30

31 Deletion: Two Children Figure 8.15: Can't replace with subtree Delete a node with subtree (case 1) Figure 8.16: Node replaced by its successor 31

32 Find Successor To find the successor, start with the original (deleted) node s right child. Then go to this node s left child and then to its left child and so on, following down the path of left children. The last left child in this path is the successor of the original node. Figure 8.18: The right child is the successor Figure 8.17: Finding the successor Delete a Node with Two Children Using the Workshop Applet Figure 8.15: Can't replace with subtree 32

33 Java Code to Find the Successor // returns node with next-highest value after delnode // goes to right child, then right child's left descendants private node getsuccessor(node delnode) Node successorparent = delnode; Node successor = delnode; Node current = delnode.rightchild; // go to right child while(current!= null) // until no more // left children, successorparent = successor; successor = current; current = current.leftchild; // go to left child // if successor not if(successor!= delnode.rightchild) // right child, // make connections successorparent.leftchild = successor.rightchild; //case 3 step 1 successor.rightchild = delnode.rightchild; //case 3 step 2 return successor; Delete a node with subtree (case 2) successor Is Right Child of delnode Figure 8.19: Deletion when successor is right child 33

34 successor Is Right Child of delnode Here's the code in context (a continuation of the else-if ladder shown earlier): // delete() continued else // two children, so replace with inorder successor // get successor of node to delete (current) Node successor = getsuccessor(current); // connect parent of current to successor instead if(current == root) root = successor; else if(isleftchild) //case 3 step 3 parent.leftchild = successor; else parent.rightchild = successor; // connect successor to current's left child successor.leftchild = current.leftchild; // case 3 step 4 // end else two children // (successor cannot have a left child) return true; // end delete() Delete a node with subtree (case 3) successor Is Left Descendant of Right Child of delnode Figure 8.20: Deletion when successor is left child 34

35 Delete a node with subtree (case 3) successor Is Left Descendant of Right Child of delnode 1. Plug the right child of successor into the leftchild field of the successor's parent. 2. Plug the right child of the node to be deleted into the rightchild field of successor. 3. Unplug current from the rightchild field of its parent, and set this field to point to successor. 4. Unplug current's left child from current, and plug it into the leftchild field of successor. Here's the code for these four steps: 1. successorparent.leftchild = successor.rightchild; 2. successor.rightchild = delnode.rightchild; 3. parent.rightchild = successor; 4. successor.leftchild = current.leftchild; Figure 8.20: Deletion when successor is left child Is Deletion Necessary? Deletion is fairly involved. In fact, it's so complicated that some programmers try to sidestep it altogether. They add a new Boolean field to the node class, called something like isdeleted. To delete a node, they simply set this field to true. Then other operations, like find(), check this field to be sure the node isn't marked as deleted before working with it. This way, deleting a node doesn't change the structure of the tree. Of course, it also means that memory can fill up with "deleted" nodes. This approach is a bit of a cop-out, but it may be appropriate where there won't be many deletions in a tree. For example: If ex-employees remain in the personnel file forever 35

36 , , ,048, ,554, ,073,741,824 Efficiency Table 8.2: NUMBER OF LEVELS FOR SPECIFIED NUMBER OF NODES Number of Nodes Number of Levels Efficiency Assume number of nodes N and number of levels L. N = 2 L -1 N+1 = 2 L L = log 2 (N+1) The time needed to carry out the common tree operations is proportional to the base 2 log of N O(log N) time is required for these operations. 36

37 Trees Represented as Arrays The nodes are stored in an array and are not linked by references. The position of the node in the array corresponds to its position in the tree. The node at index 0 is the root, the node at index 1 is the root's left child, and so on, progressing from left to right along each level of the tree. Figure 8.21: Tree represented by an array Trees Represented as Arrays Every position in the tree, whether it represents an existing node or not, corresponds to a cell in the array. Adding a node at a given position in the tree means inserting the node into the equivalent cell in the array. Cells representing tree positions with no nodes are filled with zero or null. With this scheme, a node's children and parent can be found by applying some simple arithmetic to the node's index number in the array. If a node's index number is index, then this node's left child is 2*index + 1 its right child is 2*index + 2 and its parent is (index-1) / 2 (where the '/' character indicates integer division with no remainder). 37

38 Trees Represented as Arrays In most situations, representing a tree with an array isn't very efficient. Unfilled nodes and deleted nodes leave holes in the array, wasting memory. Even worse, when deletion of a node involves moving subtrees, every node in the subtree must be moved to its new location in the array, which is time-consuming in large trees. However, if deletions aren't allowed, then the array representation may be useful, especially if obtaining memory for each node dynamically is, for some reason, too time consuming. The array representation may also be useful in special situations. The tree in the Workshop applet, for example, is represented internally as an array to make it easy to map the nodes from the array to fixed locations on the screen display. Duplicate Keys In the code shown for insert(), and in the Workshop applet, a node with a duplicate key will be inserted as the right child of its twin. The problem is that the find() routine will find only the first of two (or more) duplicate nodes. The find() routine could be modified to check an additional data item, to distinguish data items even when the keys were the same, but this would be (at least somewhat) time-consuming. One option is to simply forbid duplicate keys. When duplicate keys are excluded by the nature of the data (employee ID numbers, for example) there's no problem. Otherwise, you need to modify the insert() routine to check for equality during the insertion process, and abort the insertion if a duplicate is found. The Fill routine in the Workshop applet excludes duplicates when generating the random keys. 38

39 The Complete tree.java Program Huffman Code Binary tree is used to compress data. Data compression is used in many situations. E.g. sending data over internet. Character Code: Each character in a normal uncompressed text file is represented in the computer by one byte or by two bytes. For text, the most common approach is to reduce the number of bits that represent the most-used characters. E.g. E is the most common letter, so few bits can be used to encode it. No code can be the prefix of any other code. No space characters in binary message, only 0s and 1s. 39

40 Decoding with Huffman Tree Huffman tree is kind of binary tree, used for decoding character codes. The characters in the message appear in the tree as leaf nodes. The higher their frequency in the message, the higher up they appear in the tree. The number outside each node is the frequency. The numbers outside non-leaf nodes are the sums of the frequencies of their children. Decoding (Contd.) For each character start at the root. If we see a 0 bit, go left to the next node, and if we see a 1 bit, then go right. 40

41 Creating Huffman Tree Make a Node object for each character used in the message. Each node has two data items: the character and that character s frequency in the message. Make a tree object for each of these nodes. The node becomes the root of the tree. Insert these trees in a priority queue. They are ordered by frequency, with the smallest frequency having the highest priority. Remove two trees from the priority queue, and make them into children of a new node. The new node has frequency that is the sum of the children s frequencies. Insert this new three-node tree back into the priority queue. Keep repeating these two steps, till only one tree is left in the queue. Coding the Message Create a code table listing the Huffman code alongside each character. The index of each cell would be the numerical value of the character. The contents of the cell would be the Huffman code for the corresponding character. For each character in the original message, use its code as an index into the code table. Then repeatedly append the Huffman code to the end of the coded message until its complete. 41

42 Creating Huffman Code The process is like decoding a message. Start at the root of the Huffman tree and follow every possible path to a leaf node. As we go along the path, remember the sequence of left and right choices, regarding a 0 for a left edge and a 1 for a right edge. When we arrive at the leaf node for a character, the sequence of 0s and 1s is the Huffman code for that character. Put this code into the code table at the appropriate index number. 42

Binary Trees and Huffman Encoding Binary Search Trees

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

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

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).

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

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

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

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

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

Multi-Way Search Trees (B Trees)

Multi-Way Search Trees (B Trees) Multiway Search Trees An m-way search tree is a tree in which, for some integer m called the order of the tree, each node has at most m children. If n

CE204 Data Structures and Algorithms Part 3

CE204 Data Structures and Algorithms Part 3 23/01/2016 CE204 Part 3 1 Trees The ADTs encountered so far have been linear; list and array implementations have been adequate. We now consider a nonlinear

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:

COURSE: B.TECH-ECE. IV Sem. Data structure Using C. b) Deletion of element in an array

COURSE: B.TECH-ECE. IV Sem Data structure Using C 1. Determine the formula to find the address location of an element in three dimensions array, suppose each element takes four bytes of space & elements

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

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

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

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

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

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

Class 28: Binary Search Trees. Binary Search Trees

Introduction to Computation and Problem Solving Class 2: Binary Search Trees Prof. Steven R. Lerman and Dr. V. Judson Harward Binary Search Trees In the previous lecture, we defined the concept of binary

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

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)

Trees & Binary Search Trees

CMSC 132: Object-Oriented Programming II Trees & Binary Search Trees Department of Computer Science University of Maryland, College Park Trees Trees are hierarchical data structures One-to-many relationship

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

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

UIL Computer Science for Dummies by Jake Warren and works from Mr. Fleming

UIL Computer Science for Dummies by Jake Warren and works from Mr. Fleming 1 2 Foreword First of all, this book isn t really for dummies. I wrote it for myself and other kids who are on the team. Everything

Balanced search trees

Lecture 8 Balanced search trees 8.1 Overview In this lecture we discuss search trees as a method for storing data in a way that supports fast insert, lookup, and delete operations. (Data structures handling

Introduction to Data Structures

Introduction to Data Structures Albert Gural October 28, 2011 1 Introduction When trying to convert from an algorithm to the actual code, one important aspect to consider is how to store and manipulate

Chapter 7. Indexes Table of Contents Objectives... 1 Introduction... 2 Context... 2 Review Questions... 3 Single-level Ordered Indexes... 4 Primary Indexes... 4 Clustering Indexes... 8 Secondary Indexes...

Stacks, Queues & Trees. The Stack Interface. The Stack Interface. Prof. Dr. Harald Gall. Institut für Informatik Universität Zürich

Stacks, Queues & Trees Prof. Dr. Harald Gall Institut für Informatik Universität Zürich http://seal.ifi.uzh.ch The Stack Interface!! Stack!! Data structure holding several items.!! New items added to the

BINARY SEARCH TREE PERFORMANCE

BINARY SEARCH TREE PERFORMANCE Operation Best Time Average Time Worst Time (on a tree of n nodes) Find Insert Delete O(lg n)?? O(lg n)?? O(n) Fastest Running Time The find, insert and delete algorithms

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

Symbol Tables. IE 496 Lecture 13

Symbol Tables IE 496 Lecture 13 Reading for This Lecture Horowitz and Sahni, Chapter 2 Symbol Tables and Dictionaries A symbol table is a data structure for storing a list of items, each with a key and

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

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

CHAPTER 4 ESSENTIAL DATA STRUCTRURES

CHAPTER 4 ESSENTIAL DATA STRUCTURES 72 CHAPTER 4 ESSENTIAL DATA STRUCTRURES In every algorithm, there is a need to store data. Ranging from storing a single value in a single variable, to more complex

5. A full binary tree with n leaves contains [A] n nodes. [B] log n 2 nodes. [C] 2n 1 nodes. [D] n 2 nodes.

1. The advantage of.. is that they solve the problem if sequential storage representation. But disadvantage in that is they are sequential lists. [A] Lists [B] Linked Lists [A] Trees [A] Queues 2. The

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

Trees. Definition: A tree is a connected undirected graph with no simple circuits. Example: Which of these graphs are trees?

Section 11.1 Trees Definition: A tree is a connected undirected graph with no simple circuits. Example: Which of these graphs are trees? Solution: G 1 and G 2 are trees both are connected and have no simple

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:

Trees Carlos Delgado Kloos Mª Carmen Fernández Panadero Raquel M. Crespo García Dep. Ingeniería Telemática Univ. Carlos III de Madrid

Trees Carlos Delgado Kloos Mª Carmen Fernández Panadero Raquel M. Crespo García Dep. Ingeniería Telemática Univ. Carlos III de Madrid cdk@it.uc3m.es Java: Trees / 1 Contents Concept Non recursive definition

Persistent Binary Search Trees

Persistent Binary Search Trees Datastructures, UvA. May 30, 2008 0440949, Andreas van Cranenburgh Abstract A persistent binary tree allows access to all previous versions of the tree. This paper presents

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

Why Use Binary Trees?

Binary Search Trees Why Use Binary Trees? Searches are an important application. What other searches have we considered? brute force search (with array or linked list) O(N) binarysearch with a pre-sorted

CSCI Trees. Mark Redekopp David Kempe

1 CSCI 104 2-3 Trees Mark Redekopp David Kempe 2 Properties, Insertion and Removal BINARY SEARCH TREES 3 Binary Search Tree Binary search tree = binary tree where all nodes meet the property that: All

School of Informatics, University of Edinburgh Computer Science 1 Ah [03 04]

School of Informatics, University of Edinburgh Computer Science 1 Ah [03 04] CS1Ah Lecture Note 25 Binary Search Trees This lecture studies binary trees in more detail, in particular their application

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

Lesson 52.. Binary Search Tree

Lesson 52.. Binary Search Tree 52-1 We will begin by showing how a binary search tree is constructed. We will construct our tree from the following sequence of integers;, 56, 52, 25, 74, and 54. Each number

CS104: Data Structures and Object-Oriented Design (Fall 2013) October 24, 2013: Priority Queues Scribes: CS 104 Teaching Team

CS104: Data Structures and Object-Oriented Design (Fall 2013) October 24, 2013: Priority Queues Scribes: CS 104 Teaching Team Lecture Summary In this lecture, we learned about the ADT Priority Queue. A

Module 2 Stacks and Queues: Abstract Data Types

Module 2 Stacks and Queues: Abstract Data Types A stack is one of the most important and useful non-primitive linear data structure in computer science. It is an ordered collection of items into which

Definition. E.g. : Attempting to represent a transport link data with a tree structure:

The ADT Graph Recall the ADT binary tree: a tree structure used mainly to represent 1 to 2 relations, i.e. each item has at most two immediate successors. Limitations of tree structures: an item in a tree

Data Structures 2014

Unit-1 Introduction Short Questions:1. Define data structure. 2. Define algorithm. 3. List out areas in which data structures are applied. 4. List out structure of algorithm. 5. List out properties of

Compiler Design Prof. Y. N. Srikant Department of Computer Science and Automation Indian Institute of Science, Bangalore

Compiler Design Prof. Y. N. Srikant Department of Computer Science and Automation Indian Institute of Science, Bangalore Module No. # 02 Lecture No. # 05 Run-time Environments-Part 3 and Local Optimizations

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

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 Search Trees and Hash Tables

CHAPTER 7 Binary Search Trees and Hash Tables 7.1 Binary Search Trees In Chapter, we introduced containers called a priority queues and dictionaries. A priority queue is a dispenser in which the current

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

CSE 326, Data Structures. Sample Final Exam. Problem Max Points Score 1 14 (2x7) 2 18 (3x6) 3 4 4 7 5 9 6 16 7 8 8 4 9 8 10 4 Total 92.

Name: Email ID: CSE 326, Data Structures Section: Sample Final Exam Instructions: The exam is closed book, closed notes. Unless otherwise stated, N denotes the number of elements in the data structure

COT5405 Analysis of Algorithms Homework 3 Solutions

COT0 Analysis of Algorithms Homework 3 Solutions. Prove or give a counter example: (a) In the textbook, we have two routines for graph traversal - DFS(G) and BFS(G,s) - where G is a graph and s is any

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

Trees LOGO STYLE GUIDE Schools within the University 1 19

Trees 1 Example Tree 2 A Generic Tree 3 Tree ADT n Tree definition q A tree is a set of nodes which may be empty q If not empty, then there is a distinguished node r, called root and zero or more non-empty

Trees. Tree Definitions: Let T = (V, E, r) be a tree. The size of a tree denotes the number of nodes of the tree.

Trees After lists (including stacks and queues) and hash tables, trees represent one of the most widely used data structures. On one hand trees can be used to represent objects that are recursive in nature

Lecture Notes on Binary Search Trees

Lecture Notes on Binary Search Trees 15-122: Principles of Imperative Computation Frank Pfenning André Platzer Lecture 17 October 23, 2014 1 Introduction In this lecture, we will continue considering associative

CS 210 Algorithms and Data Structures College of Information Technology and Engineering Weisberg Division of Engineering and Computer Science

CS 210 Algorithms and Data Structures College of Information Technology and Engineering Weisberg Division of Engineering and Computer Science Semester and Year: Spring 2009 Classroom Section, Meeting Times,

Announcements. CSE332: Data Abstractions. Lecture 9: B Trees. Today. Our goal. M-ary Search Tree. M-ary Search Tree. Ruth Anderson Winter 2011

Announcements CSE2: Data Abstractions Project 2 posted! Partner selection due by 11pm Tues 1/25 at the latest. Homework due Friday Jan 28 st at the BEGINNING of lecture Lecture 9: B Trees Ruth Anderson

2. Compressing data to reduce the amount of transmitted data (e.g., to save money).

Presentation Layer The presentation layer is concerned with preserving the meaning of information sent across a network. The presentation layer may represent (encode) the data in various ways (e.g., data

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

Chapter 7 Linked Lists In order to use linked lists, you need to learn how to allocate memory dynamically on the heap and you need to learn about objects. Recall that local variables are temporary which

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

The relationship of a trees to a graph is very important in solving many problems in Maths and Computer Science

Trees Mathematically speaking trees are a special class of a graph. The relationship of a trees to a graph is very important in solving many problems in Maths and Computer Science However, in computer

Multiway Search Tree (MST)

Multiway Search Tree (MST) Generalization of BSTs Suitable for disk MST of order n: Each node has n or fewer sub-trees S1 S2. Sm, m n Each node has n-1 or fewer keys K1 Κ2 Κm-1 : m-1 keys in ascending

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

Algorithms. Margaret M. Fleck. 18 October 2010

Algorithms Margaret M. Fleck 18 October 2010 These notes cover how to analyze the running time of algorithms (sections 3.1, 3.3, 4.4, and 7.1 of Rosen). 1 Introduction The main reason for studying big-o

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:

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

CSE 5311 Homework 2 Solution

CSE 5311 Homework 2 Solution Problem 6.2-6 Show that the worst-case running time of MAX-HEAPIFY on a heap of size n is Ω(lg n). (Hint: For a heap with n nodes, give node values that cause MAX- HEAPIFY

Definition. (American Heritage Dictionary, 2000)

Recursion 1 recursion Definition (American Heritage Dictionary, 2000) NOUN: Mathematics 1. An expression, such as a polynomial, each term of which is determined by application of a formula to preceding

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)

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

Overview of Data Structures

UNIT 3 Concrete Data Types Classification of Data Structures Concrete vs. Abstract Data Structures Most Important Concrete Data Structures Arrays Records Linked Lists Binary Trees Overview of Data Structures

2 Exercises and Solutions

2 Exercises and Solutions Most of the exercises below have solutions but you should try first to solve them. Each subsection with solutions is after the corresponding subsection with exercises. 2.1 Sorting

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

Theorem A graph T is a tree if, and only if, every two distinct vertices of T are joined by a unique path.

Chapter 3 Trees Section 3. Fundamental Properties of Trees Suppose your city is planning to construct a rapid rail system. They want to construct the most economical system possible that will meet the

1. What is the most important feature of Java? Java is a platform independent language. 2. What do you mean by platform independence? Platform independence means that we can write and compile the java

Balanced Trees Part One

Balanced Trees Part One Balanced Trees Balanced trees are surprisingly versatile data structures. Many programming languages ship with a balanced tree library. C++: std::map / std::set Java: TreeMap /

Common Data Structures

Data Structures 1 Common Data Structures Arrays (single and multiple dimensional) Linked Lists Stacks Queues Trees Graphs You should already be familiar with arrays, so they will not be discussed. Trees

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

Labworks 1: Binary Search Trees and Red-Black trees

Labworks 1: Binary Search Trees and Red-Black trees October 12, 2016 Deadline for these labworks: October 26 2016 How? Make an archive with the source files of your implementation, and send it by email

School of Informatics, University of Edinburgh

CS1Ah Lecture Note 5 Java Expressions Many Java statements can contain expressions, which are program phrases that tell how to compute a data value. Expressions can involve arithmetic calculation and method

In mathematics, it is often important to get a handle on the error term of an approximation. For instance, people will write

Big O notation (with a capital letter O, not a zero), also called Landau's symbol, is a symbolism used in complexity theory, computer science, and mathematics to describe the asymptotic behavior of functions.

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

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

Chapter 7. Multiway Trees. Data Structures and Algorithms in Java

Chapter 7 Multiway Trees Data Structures and Algorithms in Java Objectives Discuss the following topics: The Family of B-Trees Tries Case Study: Spell Checker Data Structures and Algorithms in Java 2 Multiway

Laboratory Module 8 B Trees

Purpose: understand the notion of B trees to build, in C, a B tree 1 2-3 Trees 1.1 General Presentation Laboratory Module 8 B Trees When working with large sets of data, it is often not possible or desirable

Data Structure and Algorithm I Midterm Examination 120 points Time: 9:10am-12:10pm (180 minutes), Friday, November 12, 2010

Data Structure and Algorithm I Midterm Examination 120 points Time: 9:10am-12:10pm (180 minutes), Friday, November 12, 2010 Problem 1. In each of the following question, please specify if the statement

Physical Data Organization

Physical Data Organization Database design using logical model of the database - appropriate level for users to focus on - user independence from implementation details Performance - other major factor

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

06 B: Hashing and Priority Queues

06 B: and CS1102S: Data Structures and Algorithms Martin Henz February 26, 2010 Generated on Friday 26 th February, 2010, 09:23 CS1102S: Data Structures and Algorithms 06 B: and 1 1 2 3 CS1102S: Data Structures

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