BINARY SEARCH TREE. Binary Trees A binary tree is a tree in which no node can have more than two children. In this case we can keep direct links to the.

Slides:



Advertisements
Similar presentations
Binary Search Tree Smt Genap
Advertisements

DATA STRUCTURES AND ALGORITHMS Lecture Notes 9 Prepared by İnanç TAHRALI.
Trees Types and Operations
IKI 10100: Data Structures & Algorithms Ruli Manurung (acknowledgments to Denny & Ade Azurat) 1 Fasilkom UI Ruli Manurung (Fasilkom UI)IKI10100: Lecture20.
IKI 10100I: Data Structures & Algorithms Ruli Manurung (acknowledgments to Denny & Ade Azurat) 1 Fasilkom UI Ruli Manurung (Fasilkom UI)IKI10100I: Data.
Binary Search Trees Azhar Maqsood School of Electrical Engineering and Computer Sciences (SEECS-NUST)
CS 201 Data Structures and Algorithms Chapter 4: Trees (BST) Text: Read Weiss, §4.3 1Izmir University of Economics.
Binary Trees, Binary Search Trees CMPS 2133 Spring 2008.
Binary Trees, Binary Search Trees COMP171 Fall 2006.
Binary Search Trees Briana B. Morrison Adapted from Alan Eugenio.
Trees, Binary Trees, and Binary Search Trees COMP171.
Lec 15 April 9 Topics: l binary Trees l expression trees Binary Search Trees (Chapter 5 of text)
Review: Search Linear Search Binary Search Search demos: – ndan/dsal/appldsal.htmlhttp://
Starting Out with C++: Early Objects 5/e © 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5 th Edition Chapter 19 Binary.
1 BST Trees A binary search tree is a binary tree in which every node satisfies the following: the key of every node in the left subtree is.
Chapter 08 Binary Trees and Binary Search Trees © John Urrutia 2013, All Rights Reserved.
CS 146: Data Structures and Algorithms June 18 Class Meeting Department of Computer Science San Jose State University Summer 2015 Instructor: Ron Mak
Binary Search Trees. BST Properties Have all properties of binary tree Items in left subtree are smaller than items in any node Items in right subtree.
INTRODUCTION TO AVL TREES P. 839 – 854. INTRO  Review of Binary Trees: –Binary Trees are useful for quick retrieval of items stored in the tree –order.
Chapter 19 - basic definitions - order statistics ( findkth( ) ) - balanced binary search trees - Java implementations Binary Search Trees 1CSCI 3333 Data.
Lecture Objectives  To learn how to use a tree to represent a hierarchical organization of information  To learn how to use recursion to process trees.
1 COP 3538 Data Structures with OOP Chapter 8 - Part 2 Binary Trees.
Tree. Basic characteristic Top node = root Left and right subtree Node 1 is a parent of node 2,5,6. –Node 2 is a parent of node.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 223 – Advanced Data Structures Course Review Midterm.
INTRODUCTION TO BINARY TREES P SORTING  Review of Linear Search: –again, begin with first element and search through list until finding element,
DATA STRUCTURES AND ALGORITHMS Lecture Notes 5 Prepared by İnanç TAHRALI.
Binary Trees, Binary Search Trees RIZWAN REHMAN CENTRE FOR COMPUTER STUDIES DIBRUGARH UNIVERSITY.
Binary Search Trees Binary Search Trees (BST)  the tree from the previous slide is a special kind of binary tree called a binary.
Trees, Binary Trees, and Binary Search Trees COMP171.
Trees  Linear access time of linked lists is prohibitive Does there exist any simple data structure for which the running time of most operations (search,
Lec 15 Oct 18 Binary Search Trees (Chapter 5 of text)
Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy Walters, and Godfrey Muganda Modified for use at Midwestern State University Chapter.
Chapter 19: Binary Search Trees or How I Learned to Love AVL Trees and Balance The Tree Group 6: Tim Munn.
Binary Tree. Some Terminologies Short review on binary tree Tree traversals Binary Search Tree (BST)‏ Questions.
1 Chapter 4 Trees Basic concept How tree are used to implement the file system How tree can be used to evaluate arithmetic expressions How to use trees.
CSCS-200 Data Structure and Algorithms Lecture
Tree Data Structures. Heaps for searching Search in a heap? Search in a heap? Would have to look at root Would have to look at root If search item smaller.
Data Structures Using C++ 2E Chapter 11 Binary Trees.
Definitions Read Weiss, 4.1 – 4.2 Implementation Nodes and Links One Arrays Three Arrays Traversals Preorder, Inorder, Postorder K-ary Trees Converting.
1 Binary search trees Outline This topic covers binary search trees: –Abstract Sorted Lists –Background –Definition and examples –Implementation: Front,
1 Trees 3: The Binary Search Tree Reading: Sections 4.3 and 4.6.
Data Structure and Algorithms
BCA-II Data Structure Using C
Binary Search Trees A binary search tree is a binary tree
BST Trees
CISC220 Fall 2009 James Atlas Lecture 13: Binary Trees.
Binary Search Tree (BST)
CMSC 341 Lecture 13 Leftist Heaps
TREE DATA STRUCTURE Data Structure 21-Sep-18 Tree
Binary Search Trees Why this is a useful data structure. Terminology
Binary Trees, Binary Search Trees
Chapter 20: Binary Trees.
CMSC 341 Lecture 10 Binary Search Trees
Trees 3: The Binary Search Tree
Lec 12 March 9, 11 Mid-term # 1 (March 21?)
CMSC 341 Binary Search Trees 11/19/2018.
Lecture No.16 Data Structures Dr. Sohail Aslam.
CMSC 341 Binary Search Trees.
CMSC 341 Binary Search Trees 1/3/2019.
Binary Trees, Binary Search Trees
CMSC 341 Binary Search Trees 2/23/2019.
CMSC 341 Binary Search Trees.
CSC 143 Binary Search Trees.
BST Insert To insert an element, we essentially do a find( ). When we reach a NULL pointer, we create a new node there. void BST::insert(const Comp &
Tree.
CMSC 341 Binary Search Trees 5/8/2019.
CMSC 341 Binary Search Trees 5/28/2019.
CMSC 341 Binary Search Trees 2/21/2006.
Binary Trees, Binary Search Trees
Data Structures Using C++ 2E
Presentation transcript:

BINARY SEARCH TREE

Binary Trees A binary tree is a tree in which no node can have more than two children. In this case we can keep direct links to the children: struct TreeNode { Object element; TreeNode *left_child; TreeNode *right_child; };

Binary Trees: Recursive Definition ROOT OF TREE T T1 T2 SUBTREES *left_child *right_child

Binary Search Trees Assume that each node in the tree stores an item (e.g., a word from a dictionary). These items can be ordered in some consistent manner. A binary search tree is a binary tree where, for every node X in the tree, the values of the items in its left subtree are smaller than the item in X, and the values of the items in its right subtree are larger than the item in X.

Binary Search Tree ROOT OF TREE T T1 T2 SUBTREES *left_child *right_child X All nodes in T1 have values < X. All nodes in T2 have values > X.

Binary Search Trees in C++ We will use two classes: The class BinaryNode simply constructs individual nodes in the tree. The class BinarySearchTree maintains a pointer to the root of the binary search tree and includes methods for inserting and removing nodes.

Binary Search Trees in C++ element, *left, *right constructor method The BinaryNode Class class BinaryNode { int element; BinaryNode *left; BinaryNode *right; BinaryNode (const int & el, BinaryNode *lt, BinaryNode *rt) : element (el), left (lt), right (rt) { } friend class BinarySearchTree; }; *left*right el

Binary Search Trees in C++ *root find/insert/remove methods The BinarySearchTree Class class BinarySearchTree { public: explicit BinarySearchTree (const int &notFound); const int &find (const int &x) int; void insert (const int &x); void remove (const int &x); private: BinaryNode *root; const int NOT_FOUND; }; *left*right el *root

Binary Search Tree Methods The Find operation returns a pointer to the node in a tree T that has item X, or NULL if there is no such node. The Insert operation inserts a new node (with item X) into the tree T. The Remove operation removes the node (with item X) from the tree T.

The Find Operation // Returns the element at the node const int &elementAt (BinaryNode *t) const { return t == NULL ? NOT_FOUND : t  element; } // Start the search at the root node const int &find (const int & x) const { return elementAt (find (x, root)); } element *t *root

The Find Operation… BinaryNode *find (const int &x, BinaryNode *t) const { if ( t == NULL ) return NULL; else if ( x < t  element ) return find ( x, t  left ); else if ( t  element < x ) return find ( x, t  right ); else return t; // Match } *root Suppose I try to find the node with 7 in it. First go down the right subtree, then go down the left subtree.

The FindMin Operation… BinaryNode *findMin (BinaryNode *t) const { if ( t == NULL ) return NULL; else if ( t  left == NULL ) return t; return findMin (t  left); } *root This function returns a pointer to the node containing the smallest element in the tree. It does so by following the left side of the tree.

The FindMax Operation… BinaryNode *findMax (BinaryNode *t) const { if ( t == NULL ) return NULL; else if ( t  right == NULL ) return t; return findMax (t  right); } *root This function returns a pointer to the node containing the largest element in the tree. It does so by following the right side of the tree.

The Insert Operation Insertion is conceptually simple. To insert X into a tree T, proceed down the tree as you would with a find. If X is found, do nothing. Otherwise insert X at the last spot on the path that has been traversed. *root For example, suppose I want to insert a 1 into this tree…

The Insert Operation void BinarySearchTree insert (const int &x, BinaryNode *&t) const { if (t == NULL) t = new BinaryNode (x, NULL, NULL); else if (x < t  element) insert(x, t  left); else if( t  element < x) insert(x, t  right); else ; // Duplicate entry; do nothing } *root t  left 1 NULL t Note the pointer t is passed using call by reference. In this case this means t  left will be changed to t.

The Removal Operation If the node to be removed is a leaf, it can be deleted immediately. If the node has one child, the node can be deleted after its parent adjusts a link to bypass the deleted node. *root What if the 2 is deleted?

Removal… *root *root t t  right Set t = t  right

Removal… If the node to be removed has two children, the general strategy is to replace the data of this node with the smallest data of the right subtree. Then the node with the smallest data is now removed (this case is easy since this node cannot have two children).

Removal… *root Remove the 2 again… *root

The Removal Operation void remove (const int &x, BinaryNode *&t) const { if ( t == NULL ) return; // Item not found; do nothing if ( x < t  element ) remove( x, t  left ); else if( t  element < x ) remove( x, t  right ); else if( t  left != NULL && t  right != NULL ) // Two children { t  element = findMin( t  right )  element; remove( t  element, t  right ); } else // One child { BinaryNode *oldNode = t; t = ( t  left != NULL ) ? t  left : t  right; delete oldNode; } }

Other Methods Please see the text for the C++ destructor and copy constructors (pages ).

Analysis The running time of these operations is O(d), where d is the depth of the node containing the accessed item. What is the average depth of the nodes in a binary search tree? It depends on how well balanced the tree is.

Average Depth of Nodes Consider this very well-balanced binary search tree. What is the depth of its leaf nodes? N=7 Data Order: 10, 5, 1, 8, 20, 13, 34

A Better Analysis The analysis on the previous slide was for a particularly well-balanced binary search tree. However, not all binary search trees will be this well balanced. In particular, binary search trees are created via insertions of data. Depending on the order of the data, various trees will emerge.

Effect of Data Order Obtained if data is 4, 3, Obtained if data is 1, 2, 3, 4 Note in these cases the average depth of nodes is about N/2, not log(N)!

Depth of Nodes In the best case the depth will be about O(log N). In the worst case, if the data are already ordered, the depth will be about O(N).

Effects of Data Order… So, if the input data are randomly ordered, what is the average depth of the nodes? The analysis is beyond the scope of this course, but it can be shown that the average depth is O(log N), which is a very nice result.

Summary In this lecture we showed that, for an average binary search tree, the average depth of the nodes is O(log N). This is quite amazing, indicating that the bad situations, which are O(N), don’t occur very often. However, for those who are still concerned about the very bad situations, we can try to “balance” the trees. This is the subject for the next lecture.