Search Sorted Array: Binary Search Linked List: Linear Search

Slides:



Advertisements
Similar presentations
Chapter 12 Binary Search Trees
Advertisements

S. Sudarshan Based partly on material from Fawzi Emad & Chau-Wen Tseng
Tree Data Structures &Binary Search Tree 1. Trees Data Structures Tree  Nodes  Each node can have 0 or more children  A node can have at most one parent.
AA Trees another alternative to AVL trees. Balanced Binary Search Trees A Binary Search Tree (BST) of N nodes is balanced if height is in O(log N) A balanced.
InOrder Traversal Algorithm // InOrder traversal algorithm inOrder(TreeNode n) { if (n != null) { inOrder(n.getLeft()); visit(n) inOrder(n.getRight());
© 2004 Goodrich, Tamassia Binary Search Trees   
Binary Search Trees1 Part-F1 Binary Search Trees   
BST Data Structure A BST node contains: A BST contains
Chapter 9 contd. Binary Search Trees Anshuman Razdan Div of Computing Studies
Deletion algorithm – Phase 2: Remove node or replace its with successor TreeNode deleteNode(TreeNode n) { // Returns a reference to a node which replaced.
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.
Compiled by: Dr. Mohammad Alhawarat BST, Priority Queue, Heaps - Heapsort CHAPTER 07.
Binary Search Trees Binary Search Trees (BST)  the tree from the previous slide is a special kind of binary tree called a binary.
 Trees Data Structures Trees Data Structures  Trees Trees  Binary Search Trees Binary Search Trees  Binary Tree Implementation Binary Tree Implementation.
Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy Walters, and Godfrey Muganda Modified for use at Midwestern State University Chapter.
D. ChristozovCOS 221 Intro to CS II AVL Trees 1 AVL Trees: Balanced BST Binary Search Trees Performance Height Balanced Trees Rotation AVL: insert, delete.
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 Search Trees (BST)
1. Iterative Preorder Traversal Rpreorder(T) 1. [process the root node] if T!= NULL then Write Data(T) else Write “empty Tree” 2. [process the left subtree]
Copyright © 2012 Pearson Education, Inc. Chapter 20: Binary Trees.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 20: Binary Trees.
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
Binary Search Trees < > =
AA Trees.
File Organization and Processing Week 3
CSCE 3110 Data Structures & Algorithm Analysis
CSCE 3110 Data Structures & Algorithm Analysis
Trees Chapter 11 (continued)
Binary Search Trees < > =
Trees Chapter 11 (continued)
BST Trees
Lecture 15 AVL Trees Slides modified from © 2010 Goodrich, Tamassia & by Prof. Naveen Garg’s Lectures.
CISC220 Fall 2009 James Atlas Lecture 13: Binary Trees.
Binary Search Tree (BST)
Chapter 10 Search Trees 10.1 Binary Search Trees Search Trees
B+ Trees What are B+ Trees used for What is a B Tree What is a B+ Tree
Trees.
Binary Search Trees.
Binary Search Tree Chapter 10.
Lecture 22 Binary Search Trees Chapter 10 of textbook
Binary Search Trees < > = Binary Search Trees
ITEC 2620M Introduction to Data Structures
Tree data structure.
Binary Trees, Binary Search Trees
Chapter 20: Binary Trees.
Binary Search Trees.
Threaded Trees Binary trees have a lot of wasted space: the leaf nodes each have 2 null pointers We can use these pointers to help us in inorder traversals.
Chapter 21: Binary Trees.
Lec 12 March 9, 11 Mid-term # 1 (March 21?)
Wednesday, April 18, 2018 Announcements… For Today…
Lecture 26 Multiway Search Trees Chapter 11 of textbook
Red-Black Trees v z Red-Black Trees 1 Red-Black Trees
Tree data structure.
B+ Trees What are B+ Trees used for What is a B Tree What is a B+ Tree
Binary Search Trees < > = Binary Search Trees
Binary Search Trees.
Binary Search Trees < > =
Binary Trees, Binary Search Trees
Non-Linear Structures
Data Structures Lecture 29 Sohail Aslam.
Binary Search Trees < > =
Basic Data Structures - Trees
Search Sorted Array: Binary Search Linked List: Linear Search
Binary Search Tree (BST)
CSC 205 – Java Programming II
Chapter 11 Trees © 2011 Pearson Addison-Wesley. All rights reserved.
Presentation transcript:

Search Sorted Array: Binary Search Linked List: Linear Search Can we Apply the Binary Search algorithm on a linked list? Why not? O(log N) O(N)

Sorted Array Rigid Structure Search: O (log n) Fixed Size Need to know the size of the largest data set Wastage Search: O (log n) Insertion: O (n) - data movement Deletion: O (n) - data movement

Binary Search Tree Binary Tree Dynamic Structure (size is flexible) Data is stored in a sorted fashion A special class of BST has the following properties: Search: O (log n) Insertion: O (log n) Deletion: O (log n)

Binary Search Tree (BST) A BST is a binary tree with the following properties: Data value in the root node is greater than all the data values stored in the left subtree and is less than or equal to all the values stored in the right subtree. Both the left subtree and right subtree are BSTs.

15 20 10 2 12 18 25 28 14 11 23

23 30 18 10 20 27 32 50 15 6

Violating the condition for BST 23 30 18 10 20 27 32 50 19 6 Violating the condition for BST

search(12) 15 20 10 2 12 18 25 28 14 11 23 15 10 12 t

search(23) 15 20 10 2 12 18 25 28 14 11 23 15 20 25 23 t

Binary Search Tree Search TreeNode * BinaryTree::search(int key) { TreeNode *t = root; while (t != NULL) { if (t->data == key) break; else if (t->data > key) t = t->left; else t = t->right; } return t;

search(13) 15 20 10 2 12 18 25 28 14 11 23 15 10 12 14 Æ t

insert(4) insert(19) insert(13) 15 20 10 2 12 18 25 28 14 11 23

Binary Search Tree Search Parent void BinaryTree::searchParent(int key, TreeNode*& current, TreeNode*& previous) { current = root; previous = NULL; while (current != NULL && current -> data != key) { previous = current; if (current->data > key) current = current->left; else current = current->right; }

Binary Search Tree – Insert void BinaryTree::insert(int key) { TreeNode *newTreeNode, *current, *previous; newTreeNode = new TreeNode; newTreeNode ->left = NULL; newTreeNode ->right = NULL; newTreeNode->data = key; if (!root) root = newTreeNode; else { findParent(key, current, previous); if (current == NULL) { if (previous->data > key) previous->left = newTreeNode; else previous->right = newTreeNode; } delete newTreeNode;

insert(13) 15 20 10 2 12 18 25 28 14 11 23 15 10 12 14 13 Æ Æ

delete(2) delete(14) delete(12) 15 20 10 2 12 18 25 28 14 11 23 13

Delete a node from a BST Locate the desired node by search; call it t If t is a leaf, disconnect it from its parent and set the pointer in the parent node equal to NULL If it has only one child then remove t from the tree by making t’s parent point to its child. Otherwise, find the largest/smallest among t’s LST/RST; call it p. Copy p’s information into t. Delete p.

Time Complexity 15 20 10 2 12 18 25 28 14 11 23 Search Insert Delete

O(k) where k is the height 15 insert(15) 18 insert(18) insert(20) 20 insert(26) 26 insert(27) insert(34) 27 34 Time Complexity O(k) where k is the height

Height Balanced Trees k = log (n)