Lec 12 March 9, 11 Mid-term # 1 (March 21?)

Slides:



Advertisements
Similar presentations
Trees Types and Operations
Advertisements

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 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.
Trees, Binary Trees, and Binary Search Trees. 2 Trees * Linear access time of linked lists is prohibitive n Does there exist any simple data structure.
Lec 15 April 9 Topics: l binary Trees l expression trees Binary Search Trees (Chapter 5 of text)
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.
Version TCSS 342, Winter 2006 Lecture Notes Trees Binary Trees Binary Search Trees.
CS 146: Data Structures and Algorithms June 18 Class Meeting Department of Computer Science San Jose State University Summer 2015 Instructor: Ron Mak
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.
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.
1 Search Trees - Motivation Assume you would like to store several (key, value) pairs in a data structure that would support the following operations efficiently.
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 CSE 1342 Programming Concepts Trees. 2 Basic Terminology Trees are made up of nodes and edges. A tree has a single node known as a root. –The root is.
CISC220 Fall 2009 James Atlas Lecture 13: Trees. Skip Lists.
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.
Binary Trees, Binary Search Trees RIZWAN REHMAN CENTRE FOR COMPUTER STUDIES DIBRUGARH UNIVERSITY.
Trees, Binary Trees, and Binary Search Trees COMP171.
Binary Search Trees (10.1) CSE 2011 Winter November 2015.
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)
Preview  Graph  Tree Binary Tree Binary Search Tree Binary Search Tree Property Binary Search Tree functions  In-order walk  Pre-order walk  Post-order.
David Stotts Computer Science Department UNC Chapel Hill.
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.
Binary Tree. Some Terminologies Short review on binary tree Tree traversals Binary Search Tree (BST)‏ Questions.
Binary Search Trees (BST)
BSTs, AVL Trees and Heaps Ezgi Shenqi Bran. What to know about Trees? Height of a tree Length of the longest path from root to a leaf Height of an empty.
Binary Search Trees Chapter 7 Objectives
Lec 13 Oct 17, 2011 AVL tree – height-balanced tree Other options:
BCA-II Data Structure Using C
Binary Search Trees One of the tree applications in Chapter 10 is binary search trees. In Chapter 10, binary search trees are used to implement bags.
Binary Search Trees A binary search tree is a binary tree
BST Trees
UNIT III TREES.
CISC220 Fall 2009 James Atlas Lecture 13: Binary Trees.
Cinda Heeren / Geoffrey Tien
Binary Search Tree (BST)
Binary Search Trees (10.1) CSE 2011 Winter August 2018.
Chapter 10 Search Trees 10.1 Binary Search Trees Search Trees
Binary Search Tree Chapter 10.
Lecture 22 Binary Search Trees Chapter 10 of textbook
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
Binary Search Trees.
Trees, Binary Trees, and Binary Search Trees
Binary Search Trees (10.1) CSE 2011 Winter November 2018.
CMSC 341 Binary Search Trees 11/19/2018.
AVL Trees: AVL Trees: Balanced binary search tree
Binary Search Trees One of the tree applications in Chapter 10 is binary search trees. In Chapter 10, binary search trees are used to implement bags.
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 &
Lecture 10 Oct 1, 2012 Complete BST deletion Height-balanced BST
CMSC 341 Binary Search Trees 5/8/2019.
Tree traversals BST properties Search Insertion
CMSC 341 Binary Search Trees 5/28/2019.
CMSC 341 Binary Search Trees 2/21/2006.
Binary Trees, Binary Search Trees
Binary Search Tree.
AVL Trees: AVL Trees: Balanced binary search tree
Presentation transcript:

Lec 12 March 9, 11 Mid-term # 1 (March 21?) Binary Search Trees (Chapter 4 of text)

Binary Trees A tree in which no node can have more than two children The depth of an “average” binary tree is considerably smaller than N, even though in the worst case, the depth can be as large as N – 1. typical binary tree Worst-case binary tree

Node Struct of Binary Tree Possible operations on the Binary Tree ADT Parent, left_child, right_child, sibling, root, etc Implementation Because a binary tree has at most two children, we can keep direct pointers to them class Tree { int key; Tree* left, right; }

Binary Search Trees (BST) A data structure for efficient searching, inser-tion and deletion (dictionary operations) All operations in worst-case O(log n) time Binary search tree property For every node x: All the keys in its left subtree are smaller than the key value in x All the keys in its right subtree are larger than the key value in x

Binary Search Trees Example: Tree height = 3 A binary search tree Not a binary search tree Tree height = 3 Key requirement of a BST: all the keys in a BST are distinct, no duplication

Binary Search Trees The same set of keys may have different BSTs Average height of a binary search tree is O(log N) Maximum height of a binary search tree is O(N) (N = the number of nodes in the tree) The same set of keys may have different BSTs

Searching BST Example: Suppose T is the tree being searched: If we are searching for 15, then we are done. If we are searching for a key < 15, then we should search in the left subtree. If we are searching for a key > 15, then we should search in the right subtree.

Search (Find) Find X: return a pointer to the node that has key X, or NULL if there is no such node Tree* find(int x, Tree* t) { if (t == NULL) return NULL; else if (x < t->key) return find(x, t->left); else if (x == t->key) return t; else return find(x, t->right); } Time complexity: O(height of the tree) = O(log N) on average. (i.e., if the tree was built using a random sequence of numbers.)

Inorder Traversal of BST Inorder traversal of BST prints out all the keys in sorted order Inorder: 2, 3, 4, 6, 7, 9, 13, 15, 17, 18, 20

findMin/ findMax Goal: return the node containing the smallest (largest) key in the tree Algorithm: Start at the root and go left (right) as long as there is a left (right) child. The stopping point is the smallest (largest) element Tree* findMin(Tree* t) { if (t==NULL)_return NULL; while (t->left != NULL) t = t->left; return t; } Time complexity = O(height of the tree)

Insertion To insert(X): Proceed down the tree as you would for search. If x is found, do nothing (or update some secondary record) Otherwise, insert X at the last spot on the path traversed Time complexity = O(height of the tree) X = 13

Another example of insertion Example: insert(11). Show the path taken and the position at which 11 is inserted. Note: There is a unique place where a new key can be inserted.

Code for insertion Insert is a recursive (helper) function that takes a pointer to a node and inserts the key in the subtree rooted at that node. void insert(int x, Tree* & t) { if (t == NULL) t = new Tree(x, NULL, NULL); else if (x < t->key) insert(x, t->left); else if (x > t->key) insert(x, t->right); else ; // duplicate; do nothing }

Deletion under Different Cases Case 1: the node is a leaf Delete it immediately Case 2: the node has one child Adjust a pointer from the parent to bypass that node

Deletion Case 3 Case 3: the node has 2 children Replace the key of that node with the minimum element at the right subtree Delete that minimum element Has either no child or only right child because if it has a left child, that left child would be smaller and would have been chosen. So invoke case 1 or 2. Time complexity = O(height of the tree)

Code for Deletion First recall the code for findMin. Tree* findMin(Tree* t) { if (t==NULL)_return NULL; while (t->left != NULL) t = t->left; return t; }

Code for Deletion void remove(int x, BinaryTree* & t) { // remove key x from t if (t == NULL) return; // item not found; do nothing if (x < t->key) remove(x, t->left); else if (x > t->key) remove(x, t->right); else if (t->left != NULL && t->right != NULL) { t->key = findMin(t->right)->key; remove(t->element, t->right); } else { Tree* oldNode = t; t = (t->left != NULL) ? t->left; t->right; delete oldNode;

Summary of BST All the dictionary operations (search, insert and delete) as well as deleteMin, deleteMax etc. can be performed in O(h) time where h is the height of a binary search tree. Good news: h is on average O(log n) (if the keys are inserted in a random order). code for implementing dictionary operations is simple. Bad news: worst-case is O(n). some natural order of insertions (sorted in ascending or descending order) lead to O(n) height. (check this!) Solution: enforce some condition on the tree structure that keeps the tree from growing unevenly.