- Home
- Documents
*Binary Tree Search (BTS) - College of DuPage - Home Binary Tree Search (BTS) A binary search tree...*

prev

next

out of 7

View

233Download

1

Embed Size (px)

1

Binary Tree Search (BTS) A binary search tree (BST) is a binary tree where each node has a Comparable key (and an associated value) and satisfies the restriction that the key in any node is larger than the keys in all nodes in that node's left sub-tree and smaller than the keys in all nodes in that node's right sub-tree. Example of BST The node key (27) has all less-valued keys on the left sub-tree and higher valued keys on the right sub-tree.

In computer science, binary search trees (BST), sometimes called ordered or sorted binary trees, are a particular type of containers: data structures that store "items" (such as numbers, names etc.) in memory. Balanced binary trees A binary tree is balanced if each node has (roughly) the same number of descendants in its left sub-tree as it has in its right sub-tree. A full binary tree (sometimes proper binary tree or 2-tree) is a tree in which every node other than the leaves has two children.

2

A complete binary tree is a binary tree in which every level, except possibly the last, is completely filled, and all nodes are as far left as possible.

Important fact: For balanced binary trees, the height is proportional to the base-two logarithm of the number of nodes in the tree: h = O(log n).

3

Binary Heaps

Introduction A binary heap is a complete binary tree, which satisfies the heap ordering property. The ordering can be one of two types: o the min-heap property: the value of each node is greater than or equal to the value of its

parent, with the minimum-value element at the root. o the max-heap property: the value of each node is less than or equal to the value of its

parent, with the maximum-value element at the root. min-heap max-heap

In a heap the highest (or lowest) priority element is always stored at the root, hence the name "heap". A heap is not a sorted structure and can be regarded as partially ordered. As you see from the picture, there is no particular relationship among nodes on any given level, even among the siblings. Since a heap is a complete binary tree, it has a smallest possible height - a heap with N nodes always has O(log n) height. A heap is useful data structure when you need to remove the object with the highest (or lowest) priority. A common use of a heap is to implement a priority queue.

4

What's the difference between a binary search tree and a binary heap? Both binary search trees and binary heaps are both in the set of trees and are tree-based data structures. Heaps require the nodes to have a priority over their children. In a max heap, each node's children must be less than itself. This is the opposite for a min heap:

Binary search trees (BST) follow a specific ordering (pre-order, in-order, post-order) among sibling nodes. The tree must be sorted, unlike heaps:

BST have average of O(log n) for insertion, deletion, and search. Binary Heaps have average O(1) for findMin/findMax and O(log n) for insertion and deletion

5

How Heap Sort Works

Initially on receiving an unsorted list, the first step in heap sort is to create a Heap data structure(Max-Heap or Min-Heap).

Once heap is built, the first element of the Heap is either largest or smallest(depending upon Max-Heap or Min-Heap), so we put the first element of the heap in our array.

Then we again make heap using the remaining elements, to again pick the first element of the heap and put it into the array.

We keep on doing the same repeatedly untill we have the complete sorted list in our array.

In the below algorithm, initially heapsort() function is called, which calls buildheap() to build heap, which inturn uses satisfyheap() to build the heap. Sorting using Heap Sort Algorithm void heapsort(int[], int); void buildheap(int [], int); void satisfyheap(int [], int, int); void main() { int a[10], i, size; cout > size; cout

6

void heapsort(int a[], int length) { buildheap(a, length); int heapsize, i, temp; heapsize = length - 1; for( i=heapsize; i >= 0; i--) { temp = a[0]; a[0] = a[heapsize]; a[heapsize] = temp; heapsize--; satisfyheap(a, 0, heapsize); } for( i=0; i < length; i++) { cout

7

largest = i; } if( r a[largest]) { largest = r; } if(largest != i) { temp = a[i]; a[i] = a[largest]; a[largest] = temp; satisfyheap(a, largest, heapsize); } } Complexity Analysis of Heap Sort Worst Case Time Complexity: O(n log n) Best Case Time Complexity: O(n log n) Average Time Complexity: O(n log n) Space Complexity: O(n) Heap sort is not a Stable sort, and requires a constant space for sorting a list. Heap Sort is very fast and is widely used for sorting.