BST Trees 5 3 8 1 4 9.

Slides:



Advertisements
Similar presentations
Trees Types and Operations
Advertisements

S. Sudarshan Based partly on material from Fawzi Emad & Chau-Wen Tseng
Binary Trees, Binary Search Trees CMPS 2133 Spring 2008.
Binary Trees, Binary Search Trees COMP171 Fall 2006.
CSC 213 Lecture 7: Binary, AVL, and Splay Trees. Binary Search Trees (§ 9.1) Binary search tree (BST) is a binary tree storing key- value pairs (entries):
1 Section 9.2 Tree Applications. 2 Binary Search Trees Goal is implementation of an efficient searching algorithm Binary Search Tree: –binary tree in.
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.
Properties: -Each node has a value -The left subtree contains only values less than the parent node’s value -The right subtree contains only values greater.
CSCE 3110 Data Structures & Algorithm Analysis Binary Search Trees Reading: Chap. 4 (4.3) Weiss.
Trees, Binary Search Trees, Recursion, Project 2 Bryce Boe 2013/08/01 CS24, Summer 2013 C.
Lecture 17 Non-Linear data structures Richard Gesick.
Searching: Binary Trees and Hash Tables CHAPTER 12 6/4/15 Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education,
Binary Trees Chapter 10. Introduction Previous chapter considered linked lists –nodes connected by two or more links We seek to organize data in a linked.
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.
Tree (new ADT) Terminology:  A tree is a collection of elements (nodes)  Each node may have 0 or more successors (called children)  How many does a.
Binary Trees, Binary Search Trees RIZWAN REHMAN CENTRE FOR COMPUTER STUDIES DIBRUGARH UNIVERSITY.
Chapter 19: Binary Trees Java Programming: Program Design Including Data Structures Program Design Including Data Structures.
Binary Search Trees Binary Search Trees (BST)  the tree from the previous slide is a special kind of binary tree called a binary.
Starting at Binary Trees
Search Trees. Binary Search Tree (§10.1) A binary search tree is a binary tree storing keys (or key-element pairs) at its internal nodes and satisfying.
Topic 15 The Binary Search Tree ADT Binary Search Tree A binary search tree (BST) is a binary tree with an ordering property of its elements, such.
 Trees Data Structures Trees Data Structures  Trees Trees  Binary Search Trees Binary Search Trees  Binary Tree Implementation Binary Tree Implementation.
Lec 15 Oct 18 Binary Search Trees (Chapter 5 of text)
Binary Trees Chapter 10. Introduction Previous chapter considered linked lists –nodes connected by two or more links We seek to organize data in a linked.
1 Chapter 7 Objectives Upon completion you will be able to: Create and implement binary search trees Understand the operation of the binary search tree.
Binary Search Trees (BSTs) 18 February Binary Search Tree (BST) An important special kind of binary tree is the BST Each node stores some information.
1/14/20161 BST Operations Data Structures Ananda Gunawardena.
1 Binary Trees and Binary Search Trees Based on Dale & Co: Object-Oriented Data Structures using C++ (graphics)
Binary Search Trees (BST)
COSC 2007 Data Structures II Chapter 13 Advanced Implementation of Tables I.
COSC 2007 Data Structures II Chapter 13 Advanced Implementation of Tables II.
Binary Search Trees Chapter 7 Objectives
AA Trees.
BST Trees Saurav Karmakar
CSCE 3110 Data Structures & Algorithm Analysis
CSCE 3110 Data Structures & Algorithm Analysis
Search Trees.
Binary Trees and Binary Search Trees
AVL Trees 6/25/2018 Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia, and M.
UNIT III TREES.
Week 6 - Wednesday CS221.
Binary Search Tree (BST)
Binary Search Trees.
Binary Search Tree Chapter 10.
Red-Black Trees 9/12/ :44 AM AVL Trees v z AVL Trees.
Lecture 22 Binary Search Trees Chapter 10 of textbook
COMP 103 Binary Search Trees.
CMSC 341 Lecture 13 Leftist Heaps
Binary Trees, Binary Search Trees
Chapter 22 : Binary Trees, AVL Trees, and Priority Queues
Binary Search Trees.
AVL Trees 4/29/15 Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia, and M. H.
Red-Black Trees 11/13/2018 2:07 AM AVL Trees v z AVL Trees.
CSE 373 AVL trees read: Weiss Ch. 4, section
Lec 12 March 9, 11 Mid-term # 1 (March 21?)
AVL Trees: AVL Trees: Balanced binary search tree
Red-Black Trees 11/26/2018 3:42 PM AVL Trees v z AVL Trees.
Search Sorted Array: Binary Search Linked List: Linear Search
CMSC 341 Binary Search Trees.
Binary Trees, Binary Search Trees
Red-Black Trees 2/24/ :17 AM AVL Trees v z AVL Trees.
CMSC 341 Binary Search Trees.
Mark Redekopp David Kempe
Red-Black Trees 5/19/2019 6:39 AM AVL Trees v z AVL Trees.
1 Lecture 13 CS2013.
Non-Linear data structures
Binary Trees, Binary Search Trees
Data Structures Using C++ 2E
Chapter 11 Trees © 2011 Pearson Addison-Wesley. All rights reserved.
AVL Trees: AVL Trees: Balanced binary search tree
Presentation transcript:

BST Trees 5 3 8 1 4 9

Binary search tree (BST) 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 smaller than the key of this node the key of every node in the right subtree is larger than the key of this node Note: Duplication nodes ? 5 3 8 1 4 9

Binary search tree The left subtree and the right subtree are also BST (Recursion) No two nodes in a BST have the same key If we traverse a BST inorder, we list the key in ascending order 5 3 8 1 4 9

Some examples 1 7 9 3 5 8 15 10 2 12 6 14 3 4 16 8 8 4 2 6 1 3 7 5 12 10 14 9 11 15 13

Is this a BST? This is NOT BST! Every node satisfies the following: -- the key of the left node is smaller than the its own key -- the key of the right node is larger than its own key This is an incorrect check of BST. 5 3 8 1 7 9

Search BST 5 Find To find a key in a BST, we start at the root. Compare the target key with the key of the current node. - target == p->key, Done. - target < p->key, go left - target > p->key, go right Note that the principle behind is similar to binary search... 8 4 2 6 1 3 7 5 12 10 14 9 11 15 13 target

Basic Functions Search Find Min Find Max Insert(x) Delete

C++ Implementation of BST Search Class BinNode { public: DataType data; BinNode *left; BinNode *right; // Default setting – data part contains item; // -- both links are null BinNode(DataType item) : data(item), left(0), right(0) };

C++ Implementation of BST Search // Internal method to find an item in a subtree. // x is item to search for // t is the node that roots the tree // Return node containing the matched item. template <class Comparable> BinaryNode<Comparable> * BinarySearchTree<Comparable>:: find( const Comparable & x, Node *t ) const { while( t != NULL ) { if( x < t->element ) // element is the key value for each node t = t->left; else if( t->element < x ) t = t->right; else return t; // Match } return NULL; // Not found

Balance tree The efficiency of BST search depends on the shape of the tree. If the tree is balance, (minimum height, very bushy), the search is fast (log(n) steps). If the tree is a long narrow chain, the search is slow (n steps) 1 15 2 14 Compare this with sequential and binary search ... 8 4 2 6 1 3 7 5 12 10 14 9 11 15 13 3 log(16)=4 4 5 13

Binary Search Tree Class - FindMin/Max template <class Comparable> BinaryNode<Comparable> * BinarySearchTree<Comparable>::findMin( Node *t ) const{ if( t != NULL ){ while( t->left != NULL ) t = t->left; // the leftmost node } return t; BinaryNode<Comparable> * BinarySearchTree<Comparable>::findMax( Node *t ) const{ If( t != NULL ) while( t->right != NULL ) t = t->right; // the rightmost node

Insert & Delete in BST First we’ll study simple-minded Insert and Delete procedure. They may result in highly unbalanced tree Then we’ll study a special kind of BST called AVL tree which maintains near balance in inserting and deleting.

Insert in BST (recursive version) // Internal method to insert into a subtree // x is the item to insert // t is the node that roots the tree // Set the new root // Throw an exception if x is already in t template <class Comparable> void BinarySearchTree<Comparable>:: insert( const Comparable & x, Node * & t ) const { if( t == NULL ) t = new Node( x, NULL, NULL ); else if( x < t->element ) insert( x, t->left ); else if( x > t->element) insert( x, t->right ); else throw DuplicateItemException( ); } 10 5 12 4 7 14 2 9 17 6 8 1 15 3 New nodes are always added as leaves.

Delete in BST  Case 1: If it has no children, that is, it is a leaf, we just remove it.

Delete in BST L  Case 2: If it has only one child, (i.e. only one subtree), we splice out it. That is, we attach that subtree to the parent of the node.

Delete in BST, examples   6 16 15 9 10 5 12 7 14 17 6 16 15 9 10 12 Case 2: delete a node that has only one subtree.

Delete in BST R L  R’ Min in R Case 3: If it has nonempty left and right subtrees. Find the minimum node in right subtree, then copy its key value to X, and remove the min node from right subtree. Or Find the min node in right subtree, then swap it with the target node, and remove the target node

Delete Tree, example 10 10  5 12 6 12 4 8 8 16 3 1 2 4 16 2 6 9 7 9 14 14 1 3 7 15 15 Case 3: delete a node that has nonempty left and right subtrees.

Binary Search Tree Binary search tree, using simple insert and delete procedures the tree is nearly balance add - fast O(log n) delete a target - fast O(log n) search - fast O(log n) the tree is highly unbalance, it becomes a singly linked list (the worst case) add, delete and search - slow O(n) effectively using sequential search to find a location