This action might not be possible to undo. Are you sure you want to continue?

In computer science, a binary search tree (BST) is a node based binary tree data structure which has the following properties:

The left subtree of a node contains only nodes with keys less than the node's key. The right subtree of a node contains only nodes with keys greater than the node's key. Both the left and right subtrees must also be binary search trees. Each node (item in the tree) has a distinct key.

From the above properties it naturally follows that: Generally, the information represented by each node is a record rather than a single data element. However, for sequencing purposes, nodes are compared according to their keys rather than any part of their their associated records. The major advantage of binary search trees over other data structures is that the related sorting algorithms and search algorithms such as inorder traversal can be very efficient. Binary search trees are a fundamental data structure used to construct more abstract data structures such as sets, multisets, and associative arrays.

A binary search tree of size 9 and depth 3, with root 8 and leaves 1, 4, 7 and 13

Binary Search tree is a binary tree in which each internal node x stores an element such that the

element stored in the left subtree of x are less than or equal to x and elements stored in the right subtree of x are greater than or equal to x. This is called binary-search-tree property.

Binary search tree First of all. that its size is only limited by amount of free memory in the operating system and number of elements may vary during the program run. however.The basic operations on a binary search tree take time proportional to the height of the tree. than the node's value. right subtree of a node contains only values greater. which means. each node contains a value. the same operations takes (n) worst-case time. that definition above doesn't allow duplicates. Notice. Also. in the phone book such a key is a telephone number. For instance. while addition is quite cheap. a total order is defined on these values (every two values can be compared with each other). In practice. Binary search tree is a data structure. data can be often associated with some unique key. such operations runs in (lg n) worst-case time. binary search tree (BST) is a dynamic data structure. Storing such a data in binary search tree allows to look up for the record by key faster. Let us see more formal definition of BST. BST can be . For a complete binary tree with node n. left subtree of a node contains only values lesser. Example of a binary search tree What for binary search trees are used? Binary search tree is used to construct map data structure. Main advantage of binary search trees is rapid search. If the tree is a linear chain of n nodes. which meets the following requirements: it is a binary tree. than if it was stored in unordered list. than the node's value.

Adding a value Adding a value to BST can be divided into two stages: search for a place to put a new element. removal and search a link to the parent is not necessary. Here we will discuss the basic ideas. but in general it is quite reasonable. It is needed in order to implement iterators. Red -Black trees. Each node of binary tree contains the following information: a value (user's data). Splay trees). In order to keep tree balanced and minimize its height. a link to the left child (auxiliary data). which allows to store an unordered collection of unique val ues and make operations with such collections. Binary search tree. node may store a link to the parent. memory overhead may vary. Performance of a binary search tree depends of its height. in order to keep its structure. For basic operations. the sample from the overview changes: Leaf nodes have links to the children. laying in the foundation of binary search trees. With a view to internal representation. programmer want to apply to BST. the idea of binary search trees was advanced in balanced search trees (AVL trees. like addition.utilized to construct set data structure. In a programming language it means. BST requires storing of some additional auxiliary data. Depending on the size of user data. In some implementations. . Binary search tree. that corresponding links are set to NULL. but it depends on algorithm. Internal representation Like any other dynamic data structure. a link to the right child (auxiliary data). but they don't have children.

Otherwise. If so. the algorithm reaches a node. whether value in current node and a new value are equal. 1. that a new value has no duplicate in the tree. handle the right child with the same algorithm. Starting from the root. Search for a place At this stage analgorithm should follow binary search tree property. place for insertion has been found. insert the new element to this place. Just before code snippets. handle the left child with the same algorithm. so it is a leaf. By the moment a place for insertion is found. Example Insert 4 to the tree. than the current node's value. duplicate is found. If a new value is less. we can say for sure. Initially. place for insertion has been found. demonstrating a case of insertion in the binary search tree. else go to the right subtree. o otherwise. 2. go to the left subtree. Let us see these stages in more detail. let's go down to algorithm itself. a new node has no children. . Let us see it at the picture. let us have a look on the example. than the node's value: o if a current node has no left child. Here and in almost every operation on BST recursion is utilized. shown above. than the node's value: o if a current node has no right child. Now. Gray circles indicate possible places for a new node. which has no left or right subtree. 3. if a new value is less. check. if a new value is greater. Following this simple rule. o otherwise.

.

choosing appropriate way to go. algorithm has no way to go. if a root exists. This can be done in the BinarySearchTree class. Search algorithm traverses the tree "in-depth". Principal algorithm is implemented in the BSTNode class. Lookup operation Searching for a value in a BST is very similar to add operation. just create it and don't run a common algorithm for this special case.Code snippets The only the difference. Binary search tree. Algorithm stops in two cases: a node with necessary value is found. If not. between the algorithm above and the real routine is that first we should check. . we are looking for. following binary search tree property and compares value of each visited node with the one.

Just before code snippets. Example Search for 3 in the tree. 2. searched value doesn't exist in the BST. . o otherwise. value is found. demonstrating searching for a value in the binary search tree. than the node's value: o if current node has no left child. If so. Starting from the root. shown above. if a new value is greater. search algorithm utilizes recursion. let us have a look on the example. and almost every operation on BST. handle the right child with the same algorithm. 1. Otherwise. than the node's value: o if current node has no right child. whether value in current node and searched value are equal. if searched value is less. check. handle the left child with the same algorithm. o otherwise. let's see more detailed description of the search algorithm. searched value doesn't exist in the BST.Search algorithm in detail Now. 3. Like an add operation.

in can be divided into two stages: . Basically. than add and search. Removing a node Remove operation on binary search tree is more complicated.Binary search tree.

Node to be removed has one child. Remove algorithm in detail Now. Example. run remove algorithm. except we should track the parent of the current node. It this case. 1. First stage is identical to algorithm for lookup. which are described below. Second part is more tricky. Algorithm sets corresponding link of the parent to NULL and disposes the node. search for a node to remove. let's see more detailed description of a remove algorithm. Remove 18 from a BST. Example. . Remove -4 from a BST. This case is quite simple. Node to be removed has no children. if the node is found. node is cut from the tree and algorithm links single child (with it's subtree) directly to the parent of the removed node. 2. There are three cases.

We are going to use the idea. To solve it.3. that the same set of values may be represented as different binary-search trees. This is the most complex case. Node to be removed has two children. let us see one useful BST property first. For example those BSTs: .

that the node with minimum value has no left child and. 25}. therefore. we can do following: o o o choose minimum element from the right subtree (19 in the example). Now. 19. replace 5 by 19. Remove 12 from a BST. Notice. To transform first tree into second one. . Example. replace value of the node to be removed with found minimum.contains the same values {5. 21. which has two children: o o o find a minimum value in the right subtree. right subtree contains a duplicate! apply remove to the right subtree to remove a duplicate. hang 5 as a left child. it's removal may result in first or second cases only. The same approach can be utilized to remove a node.

. Notice. Now we have two nodes with the same value.Find minimum element in the right subtree of the node to be removed. Replace 12 with 19. In current example it is 19. not nodes. that only values are replaced.

Remove 19 from the left subtree. .

The AVL tree is named after its two inventors. Maximum." The balance factor of a node is the height of its right subtree minus the height of its left subtree and a node with balance factor 1. is significantly slower than random access memory (RAM).M. AVL trees are often compared with red-black trees because they support the same set of operations and because red-black trees also take O(log n) time for the basic operations. and Delete in time proportional to the height of the tree. Predecessor.The AVL tree balancing algorithm appears in many computer science curricula. For example. a tree will be balanced and the height will be log n where n is the number of nodes in the tree. Instead. Lookup. a b-tree with a height of 2 and a branching factor of 1001 can store over one billion keys but requires at most two disk accesses to search for any node (Cormen 384). insertion. therefore. the system often spends more time retrieving data than actually processing data. who published it in their 1962 paper "An algorithm for the organization of information. Adelson-Velskii and E. In fact. the heights of the two child subtrees of any node differ by at most one. and deletion all take O(log n) time in both the average and worst cases. a b-tree tries to minimize the number of disk accesses. B-trees are balanced trees that are optimized for situations when part or all of the tree must be maintained in secondary storage such as a magnetic disk. and it is the first such data structure to be invented. a relatively small portion of the data structure is maintained in primary storage. and additional data is read from secondary storage as needed. Successor. Since disk accesses are expensive (time consuming) operations. Insert. AVL tree. Landis. 0.M. a balanced tree structure like a red-black tree. To ensure that the height of the tree is as small as possible and therefore provide the best running time. the most common form of secondary storage. or -1 is considered balanced. The balance factor is either stored directly at each node or computed from the heights of the subtrees. a magnetic disk. or btree must be used. In an AVL tree. The Structure of B-Trees . B-Trees: Balanced Tree Data Structures Tree structures support various basic dynamic set operations including Search. where n is the number of nodes in the tree prior to the operation. Insertions and deletions may require the tree to be rebalanced by one or more tree rotations. Ideally. it is often not possible or desirable to maintain the entire structure in primary storage (RAM). A node with any other balance factor is considered unbalanced and requires rebalancing the tree. it is also said to be height-balanced. Unfortunately. Minimum.AVL tress An AVL tree is a self-balancing binary search tree. G. AVL trees perform better than red-black trees for lookup-intensive applications. When working with large sets of data.

create. the root node is allowed to violate this property by having fewer than t .1 keys. A b-tree has a minumum number of allowable children for each node known as the minimization factor. Since b-trees strive to minimize disk accesses and the nodes are usually stored on disk. b-trees tend to have smaller heights than other trees with the same asymptotic height. therefore. this single-pass approach will reduce the number of node visits and thus the number of disk accesses. Since all nodes are assumed to be stored in secondary storage (disk) rather than primary storage (memory). Leiserson. and insert operations are shown below. refer to Cormen. Note that these algorithms are single pass. in other words. once a node is modified and it is no longer needed. it must be written out to secondary storage with a write operation denoted by . the number of nodes visited during a search tends to be smaller than required by other tree structures. 2t children. a b-tree is an ideal data structure for situations where all data cannot reside in primary storage and accesses to secondary storage are comparatively expensive (or time consuming). The minimzation factor is usually chosen so that the total size of each node corresponds to a multiple of the block size of the underlying storage device. each node of a b-tree may have a variable number of keys and children.Unlike a binary-tree. it is typically neccessary to traverse relatively few nodes before locating the desired key.1 keys or. Operations on B-Trees The algorithms for the search. The keys are stored in non-decreasing order. Every node may have at most 2t .1 keys. For a proof of the above inequality. If access to each node requires a disk access. equivalently. and Rivest pages 383-384. Simpler double-pass approaches that move back up the tree to fix violations are possible. Under certain circumstances. the base of the logarithm tends to be large. Height of B-Trees For n greater than or equal to one. Although this does not affect the asymptotic worst case height. every node must have at least t . then a btree will minimize the number of disk accesses required. the height of an n-key b-tree T of height h with a minimum degree t greater than or equal to 2. they do not traverse back up the tree. A node also has an additional rightmost child that is the root for a subtree containing all keys greater than any keys in the node. Consequently. Similarly. Since each node tends to have a large branching factor (a large number of children). Each key has an associated child that is the root of a subtree containing all nodes with keys less than or equal to the key but greater than the preceeding key. The worst case height is O(log n). all references to a given node be be preceeded by a read operation denoted by Disk-Read. Since the "branchiness" of a b-tree can be large compared to many other balanced tree structures. If t is this minimization factor. This choice simplifies and optimizes disk access.

New nodes are created and assigned storage with the Allocate-Node call. and. that is. and Allocate-Node functions are operating system and implementation dependent. The heap property: each node is greater than or equal to each of its children according to some comparison predicate which is fixed for the entire data structure. except possibly the last one (deepest) are fully filled. . It can be seen as a binary tree with two additional constraints: The shape property: the tree is a complete binary tree. if the last level of the tree is not complete. the nodes of that level are filled from left to right. Examples Sample B-Tree Searching a B-Tree for Key 21 Inserting Key 33 into a B-Tree (w/ Split) A binary heap is a heap data structure created using a binary tree. The algorithms below assume that all nodes referenced in parameters have already had a corresponding Disk-Read operation. all levels of the tree. Disk-Write.Disk-Write. The implementation details of the Disk-Read.

Compare the added element with its parent. 3. We can do this in O(log n) time. binary heaps support insertion in average constant time. Conventionally. by following this algorithm: 1. since approximately 50% of the elements are leaves and 75% are in the bottom two levels. so the two children of a parent can be freely interchanged. it is likely that the new element to be inserted will only move a few levels upwards to maintain the heap. Adding to the heap If we have a heap. and we add an element. the rows alternate between min heap and max heap. We first place the 15 in the position marked by the X. one must consider the alternating rows with alternating comparisons. However. This idea can be generalised to a min-max-median heap. those where the comparison function is mathematical "less than" are called "min-heaps". Thus. The binary heap is a special case of the d-ary heap in which d = 2. The algorithms are roughly the same. So. in each step. stop. bubble-up. which is O(log n). Add the element on the bottom level of the heap. as long as this does not violate the shape and heap properties (compare with treap). we have the heap looking as follows after the first swap: . if they are in the correct order. but. not necessarily "greater than or equal to" in the mathematical sense (since the quantities are not always numerical). since they are readily applicable for use in priority queues. If not. so we need to swap the 15 and the 8. The performance is roughly the same as a normal single direction heap.[1] To do this. sift-up. Say we have a max-heap and we want to add the number 15 to the heap. using a binary heap. 2. We do this at maximum for each level in the tree—the height of the tree. swap the element with its parent and return to the previous step. percolate-up. Heaps where the comparison function is mathematical "greater than or equal to" are called max-heaps. It is possible to modify the heap structure to allow extraction of both the smallest and largest element in O(logn) time."Greater than or equal to" means according to whatever comparison function is chosen to sort the heap. we can perform an operation known as up-heap. or heapify-up in order to restore the heap property. min-heaps are used. O(1). Note that the ordering of siblings in a heap is not specified by the heap property. the heap property is violated since 15 is greater than 8. However.

the heap was valid. not 0 as is common in many programming languages. i): left ← 2i . [edit] Deleting the root from the heap The procedure for deleting the root from the heap—effectively extracting the maximum element in a max-heap or the minimum element in a min-heap—starts by replacing it with the last element on the last level. then 15 must be greater than 5. percolate-down. sift-down. if we have the same max-heap as before. Max-Heapify[2](A. so we need to swap again: which is a valid max-heap. meaning 11 is greater than 5. If 15 is greater than 11. Note that "A" is indexed starting at 1. The operation that restores the property is called downheap. and 11 is greater than 5. Now the heap property is violated since 8 is greater than 4. we remove the 11 and replace it with the 4.However the heap property is still violated since 15 is greater than 11. swapping the two elements 4 and 8. Before we placed 15 on X. This functionality is achieved by the Max-Heapify function as defined below in pseudocode for an array-backed heap A. There is no need to check the children after this. until it satisfies the heap property in its new position. In this case. bubble-down. So. is enough to restore the heap property and we need not swap elements further: The downward-moving node is swapped with the larger of its children in a max-heap (in a min-heap it would be swapped with its smaller child). or heapify-down.

largest) Note that the down-heap operation (without the preceding swap) can be used in general to modify the value of the root. .right ← 2i + 1 largest ← i if left ≤ heap-length[A] and A[left] > A[i] then: largest ← left if right ≤ heap-length[A] and A[right] > A[largest] then: largest ← right if largest ≠ i then: swap A[i] ↔ A[largest] Max-Heapify(A. even when an element is not being deleted.

- Asha Ta Da Incentives for the Month of Nov-12 Dec-12 Phc - Chandarlapadu
- Asha Ta Da Incentives for the Month of 07-05-2013, Phc - Chandarlapadu
- Asha Day Formate
- Annex -6
- Allot. of Hostels to Phc 2010-11
- Action Plan of Vitamine a+
- Action Plan of Vitamine A
- Aadhaar Card Related
- Unit 7
- Unit 6
- Unit 4
- Unit 3
- Unit 2
- Unit 1
- Streams
- JUNE 2011
- JUNE 2008
- JUNE 2007
- JUNE 2006
- JUNE 2005
- JNUE 2010
- JNUE 2009
- December 2010
- December 2009
- December 2008

Sign up to vote on this title

UsefulNot useful- QB
- Converted
- bst imp (1)
- Chapter+7+-+Tree+-+2009-+2009
- BinarySearchTrees (1)
- Binary Tree
- M WAY TREES PDF
- CS9215-SET I.pdf
- 15-btrees
- Bab 5 - Tree
- Meljun Cortes Data Structures Types of Binary Search Tree
- 15-btrees
- datastructure_Assignment
- English CS
- lec15
- Tree (BST) Implementation half.pptx
- Binary Search Trees
- Lecture 5 Trees
- lec14
- Unit 6final
- Binary Search Trees.ppt
- Definition of Trees
- tu5
- trees-FNL
- unit 2 ds
- UNIT III.docx
- Unit 3 Trees Notes
- 10_MultiwayTrees
- Augmented Data Structure
- Data Structure Lec20 Handout
- About Trees

Are you sure?

This action might not be possible to undo. Are you sure you want to continue?

We've moved you to where you read on your other device.

Get the full title to continue

Get the full title to continue reading from where you left off, or restart the preview.

scribd