Part 3: Binary Trees and BSTs. Whereas arrays arrange data linearly, binary trees can be envisioned as storing data in two dimensions. A special kind of binary tree, called a binary search tree, or BST, allows for a much more optimized search time than with arrays. In addition to reviewing the basics of data structures and their analysis, we also looked at the most commonly used data structure. In Part 2 we looked at the cousins of the Array. List. In Part 2 we also looked at the Hashtable, which is essentially an array that is indexed by some arbitrary object as opposed to by an ordinal value. The Array. List, Stack, Queue, and Hashtable all use an underlying array as the means by which their data is stored. This means that, under the covers, these four data structures are bound by the limitations imposed by an array. Recall from Part 1 that an array is stored linearly in memory, requires explicit resizing when the array's capacity it reached, and suffers from linear searching time. In this third installment of the article series, we will examine a new data structure. As we'll see, binary trees store data in a non- linear fashion. After discussing the properties of binary trees, we'll look at a more specific type of binary tree. A BST imposes certain rules on how the items of the tree are arranged. These rules provide BSTs with a sub- linear search time, making them more efficient for searching than arrays. Arranging Data in a Tree. ![]() Algorithm Software Builder allows for a few minutes to create simple applications. Free Software Maker Algorithm 2 - make your own software and. If you've ever looked at a genealogy table, or at the chain of command in a corporation, you've seen data arranged in a tree. A tree is composed of a collection of nodes, where each node has some associated data and a set of children. A node's children are those nodes that appear immediately beneath the node itself. A node's parent is the node immediately above it. A tree's root is the single node that contains no parent. Figure 1 shows an example of the chain of command in a fictitious company. Tree view of a chain of command in a fictitious company. In this example, the tree's root is Bob Smith, CEO. This node is the root because it has no parent. The Bob Smith node has one child, Tina Jones, President, whose parent is Bob Smith. The Tina Jones node has three children. Jisun Lee, CIOFrank Mitchell, CFODavis Johnson, VP of Sales. Each of these nodes' parent is the Tina Jones node. The Jisun Lee node has two children. The Frank Mitchell node has one child. The Davis Johnson node has three children. Todd Brown. Jimmy Wong. A recursive algorithm to search for a key in a BST. The running times of algorithms on binary search trees depend on the. Data Structures and Algorithms (DSA) Tutorial for. Stack, Parsing Expression, Queue, Priority queue, Tree, Binary Search Tree, B+. Tree Data Structure; DSA. This and other free CS materials are. Introduction To Binary Trees A binary tree is made. Sarah Yates. All trees exhibit the following properties. There is precisely one root. All nodes except the root have precisely one parent. There are no cycles. A Practical Introduction to Data Structures and Algorithm Analysis Third Edition. 5.1.1 The Full Binary Tree Theorem 162.
That is, starting at any given node, there is not some path that can take you back to the starting node. The first two properties. As we will discuss later on in this article, the time to search for an item can be drastically reduced by intelligently arranging the hierarchy. Before we can arrive at that topic, though, we need to first discuss a special kind of tree, the binary tree. Note. These definitions, along with being introduced throughout the text, are listed in Appendix A. Understanding Binary Trees. A binary tree is a special kind of tree, in which all nodes have at most two children. For a given node in a binary tree, the first child is referred to as the left child, while the second child is referred to as the right child. Figure 2 depicts two binary trees. Illustration of two binary trees. Binary tree (a) has 8 nodes, with node 1 as its root. Node 1's left child is node 2; node 1's right child is node 3. Notice that a node doesn't need to have both a left child and right child. In binary tree (a), node 4, for example, has only a right child. Furthermore, a node can have no children. In binary tree (b), nodes 4, 5, 6, and 7 all have no children. Nodes that have no children are referred to as leaf nodes. Nodes that have one or two children are referred to as internal nodes. Using these new definitions, the leaf nodes in binary tree (a) are nodes 6 and 8, and the internal nodes are nodes 1, 2, 3, 4, 5, and 7. Unfortunately, the . NET Framework does not contain a binary tree class, so in order to better understand binary trees, let's take a moment to create our own binary tree class. The First Step: Creating a Node Class. The first step in designing our binary tree class is to create a Node class. The Node class abstractly represents a node in the tree. Realize that each node in a binary tree contains two things. Data. 0, 1, or 2 children. The data stored in the nodes depends on what it is that you need to store. Just like arrays can be created to hold instances of integers, strings, or other classes, nodes, too, are designed to hold some instance of a class. To make the Node class as general as possible, we will have the node store data of type object, just like the Array. List, Queue, Stack, and Hashtable use an underlying array of objects to store data of any type. Note. For more information on using Generics, be sure to read Juval Lowy's article An Introduction to C# Generics. The following is the code for the Node class. Node. . This member variable contains the data stored in the node. Node. This member variable contains a reference to the node's left child. Node. This member variable contains a reference to the node's right child. The remainder of the class contains the constructors and the public properties, which provide access to these three member variables. Notice that the left and right member variables are of type Node. That is, the Node class has member variables that are Node class instances themselves. Creating the Binary. Tree Class. With the Node class complete, the Binary. Tree class is a cinch to develop. The Binary. Tree class contains a single private member variables. This private member variable is exposed as a public property. The Binary. Tree class has a single public method, Clear(), which clears out the contents of the tree. Clear() works by simply setting root to null. Below is the code for the Binary. Tree class. We then must manually add new Node class instances to the appropriate left and right children. For example, to add node 4, which is the left child of the left child of the root, we use. Root. Left. Left = new Node(4). Recall from Part 1 of this article series that an array's elements are stored in a contiguous block of memory. By doing so, arrays exhibit constant- time lookups. That is, the time it takes to access a particular element of an array does not change as the number of elements in the array increases. Binary trees, however, are not stored contiguously in memory, as Figure 3 illustrates. Rather, the Binary. Tree class instance has a reference to the root Node class instance. The root Node class instance has references to its left and right child Node instances; these child instances have references to their child instances, and so on. The point is, the various Node instances that makeup a binary tree can be scattered throughout the CLR managed heap. They are not necessarily contiguous, as are the elements of an array. Figure 3. Binary trees stored in memory. Imagine that we wanted to access a particular node in a binary tree. To accomplish this task, we need to search the binary tree's set of nodes, looking for the particular node. There's no direct access to a given node as with an array. Searching a binary tree takes linear time, as potentially all nodes need to be examined. That is, as the number of nodes in the binary tree increases, the number of steps to find an arbitrary node increases as well. So, if a binary tree's lookup time is linear, and its search time is linear, how is the binary tree any better than an array, whose search time is linear, but whose lookup time is constant? Well, a generic binary tree doesn't offer us any benefit over an array. However, by intelligently organizing the items in a binary tree, we can greatly improve the search time (and therefore the lookup time as well). Improving the Search Time with Binary Search Trees (BSTs)A binary search tree is a special kind of binary tree designed to improve the efficiency of searching through the contents of a binary tree. Binary search trees exhibit the following property: for any node n, every descendant node's value in the left subtree of n is less than the value of n, and every descendant node's value in the right subtree is greater than the value of n. A subtree rooted at node n is the tree formed by imaging node n was a root. That is, the subtree's nodes are the descendants of n and the subtree's root is n itself. Figure 4 illustrates the concept of subtrees and the binary search tree property. Figure 4. Subtrees and the binary search tree property. Figure 5 shows two examples of binary trees. The one on the right, binary tree (b), is a BST because it exhibits the binary search tree property. Binary tree (a), however, is not a BST because not all nodes of the tree exhibit the binary search tree property. Namely, node 1. 0's right child, 8, is less than 1. Similarly, node 8's right child, node 4, is less than 8 but appears on node 8's right subtree. This property is violated in other locations, too. For example, node 9's right subtree contains values less than 9 (8 and 4). Figure 5. Comparison of non- BST binary tree (a) and a BST binary tree (b)Note that for the binary search tree property to be upheld, the data stored in the nodes of a BST must be able to be compared to one another. Specifically, given two nodes, a BST must be able to determine if one is less than, greater than, or equal to the other. Now, imagine that you want to search a BST for a particular node. For example, for the BST in Figure 5 (the binary tree (b)), imagine that we wanted to search for the node 1. A BST, like a regular binary tree, only has a direct reference to one node. Can you think of an optimal way to search the tree to see if node 1. There's a better way than searching through each node in the tree. To see if 1. 0 exists in the tree, we can start with the root.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. Archives
November 2017
Categories |