CS 240: Data Structures Monday, July 30 th Binary Search Trees.

Slides:



Advertisements
Similar presentations
Chapter 12 Binary Search Trees
Advertisements

CS Fall 2012, Lab 08 Haohan Zhu. Boston University Slideshow Title Goes Here CS Fall 2012, Lab /17/2015 Tree - Data Structure  Basic.
SUNY Oneonta Data Structures and Algorithms Visualization Teaching Materials Generation Group Binary Search Tree A running demonstration of binary search.
Analysis of Algorithms CS 477/677 Binary Search Trees Instructor: George Bebis (Appendix B5.2, Chapter 12)
CS 332: Algorithms Binary Search Trees. Review: Dynamic Sets ● Next few lectures will focus on data structures rather than straight algorithms ● In particular,
Binary Trees, Binary Search Trees CMPS 2133 Spring 2008.
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.
CS 171: Introduction to Computer Science II
CS 104 Introduction to Computer Science and Graphics Problems Data Structure & Algorithms (4) Data Structures 11/18/2008 Yang Song.
BST Data Structure A BST node contains: A BST contains
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.
David Luebke 1 7/2/2015 ITCS 6114 Binary Search Trees.
Binary Search Trees Chapter 7 Objectives
By : Budi Arifitama Pertemuan ke Objectives Upon completion you will be able to: Create and implement binary search trees Understand the operation.
Types of Binary Trees Introduction. Types of Binary Trees There are several types of binary trees possible each with its own properties. Few important.
TREES A tree's a tree. How many more do you need to look at? --Ronald Reagan.
Review Binary Tree Binary Tree Representation Array Representation Link List Representation Operations on Binary Trees Traversing Binary Trees Pre-Order.
Thought for the Day “To become truly great, one has to stand with people, not above them.” – Charles de Montesquieu.
Lecture 17 Non-Linear data structures Richard Gesick.
1 TK1924 Program Design & Problem Solving Session 2011/2012 L8: Binary Trees.
CISC220 Fall 2009 James Atlas Lecture 13: Trees. Skip Lists.
Binary Trees 2 Overview Trees. Terminology. Traversal of Binary Trees. Expression Trees. Binary Search Trees.
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.
Tree 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.
1 Chapter 10 Trees. 2 Definition of Tree A tree is a set of linked nodes, such that there is one and only one path from a unique node (called the root.
Binary Search Tree Traversal Methods. How are they different from Binary Trees?  In computer science, a binary tree is a tree data structure in which.
Chapter 6 (cont’) Binary Tree. 6.4 Tree Traversal Tree traversal is the process of visiting each node in the tree exactly one time. This definition does.
Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy Walters, and Godfrey Muganda Modified for use at Midwestern State University Chapter.
Topics Definition and Application of Binary Trees Binary Search Tree Operations.
Week 7 - Friday.  What did we talk about last time?  Trees in general  Binary search trees.
CSE 3358 NOTE SET 10 Data Structures and Algorithms.
Binary Trees In computer science, a binary tree is a tree data structure in which each node has at most two children, which are referred to as the left.
Discrete Mathematics Chapter 5 Trees.
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 Tree. Some Terminologies Short review on binary tree Tree traversals Binary Search Tree (BST)‏ Questions.
ADT Binary Search Tree Ellen Walker CPSC 201 Data Structures Hiram College.
Lecture - 11 on Data Structures. Prepared by, Jesmin Akhter, Lecturer, IIT,JU Threaded Trees Binary trees have a lot of wasted space: the leaf nodes each.
Binary Search Trees (BST)
Copyright © 2012 Pearson Education, Inc. Chapter 20: Binary Trees.
Data Structures: A Pseudocode Approach with C, Second Edition 1 Chapter 7 Objectives Create and implement binary search trees Understand the operation.
Binary Tree Implementation. Binary Search Trees (BST) Nodes in Left subtree has smaller values Nodes in right subtree has bigger values.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 20: Binary Trees.
More Binary Search Trees, Project 2 Bryce Boe 2013/08/06 CS24, Summer 2013 C.
Hello Everyone!!! 1. Tree And Graphs 2 Features of Trees  Tree Nodes Each node have 0 or more children A node have must one parent  Binary tree Tree.
Trees By JJ Shepherd. Introduction Last time we discussed searching and sorting in a more efficient way Divide and Conquer – Binary Search – Merge Sort.
Chapter 7 Trees_ Part2 TREES. Depth and Height 2  Let v be a node of a tree T. The depth of v is the number of ancestors of v, excluding v itself. 
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 20: Binary Trees.
Binary Search Trees Chapter 7 Objectives
AA Trees.
Recursive Objects (Part 4)
Lecture No.13 Data Structures Dr. Sohail Aslam
CISC220 Fall 2009 James Atlas Lecture 13: Binary Trees.
Week 6 - Wednesday CS221.
Binary Search Tree (BST)
Binary Search Tree Chapter 10.
Tree.
Section 8.1 Trees.
Trees.
Binary Trees, Binary Search Trees
Chapter 20: Binary Trees.
Chapter 21: Binary Trees.
Binary Tree Chapter 8 (cont’) Part2.
Binary Search Trees Chapter 7 Objectives
Chapter 20: Binary Trees.
Trees.
Non-Linear data structures
Data Structures Using C++ 2E
A Binary Tree is a tree in which each node has at most 2 children
Presentation transcript:

CS 240: Data Structures Monday, July 30 th Binary Search Trees

What are binary search trees? Binary search trees are a data structure where each node has two links – compared to linked list which has 1. Binary search trees are a data structure where each node has two links – compared to linked list which has 1. Generally, the links are called left and right. Generally, the links are called left and right. Node *left; Node *left; Node *right; Node *right; These, alternatively, could be stored in a list/vector of type “Node *” contained in each node These, alternatively, could be stored in a list/vector of type “Node *” contained in each node

Rules of the BST A binary search tree has rules that govern how insertion works. A binary search tree has rules that govern how insertion works. Smaller data goes on the left, larger/equal data goes on the right. Smaller data goes on the left, larger/equal data goes on the right. We will refer to the first node as root. We will refer to the first node as root.

5 Root NULL Initialize TreeInsert 5 Root NULL Insert 7 5 Root NULL 7 Insert 3 5 Root 7 NULL 3

Rules, cont The insertion rule is important to the functionality of the BST The insertion rule is important to the functionality of the BST This is what allows us to search in O(lg n) time This is what allows us to search in O(lg n) time Removal is a little trickier Removal is a little trickier

Removing nodes If a node has 1 or less children (next nodes) it is similar to how we’ve done it with linked list If a node has 1 or less children (next nodes) it is similar to how we’ve done it with linked list If left==right==NULL, the parent points to NULL (instead of the node we remove) If left==right==NULL, the parent points to NULL (instead of the node we remove) If left or right != NULL, but the other is NULL, the parent points to the non-NULL value If left or right != NULL, but the other is NULL, the parent points to the non-NULL value If both are != NULL, find the smallest value on the right subtree. Copy that value into our node. Remove “smallest value” from the right subtree. If both are != NULL, find the smallest value on the right subtree. Copy that value into our node. Remove “smallest value” from the right subtree. This form of remove will call itself with a different “target” value This form of remove will call itself with a different “target” value

Traversal There are three types of traversal which allow us to see the data in the tree There are three types of traversal which allow us to see the data in the tree Preorder: NLR Preorder: NLR Inorder: LNR Inorder: LNR Postorder: LRN Postorder: LRN N = look at node’s data N = look at node’s data L = go left L = go left R = go right R = go right These traversals indicate the order we perform these three operations These traversals indicate the order we perform these three operations

5 Root 7 NULL 3 Preorder - NLR Starting at root Starting at root NLR: 5LR: 5 NLR: 3LR: 3 Nothing R: 3 Nothing 3 R: 5 NLR: 7LR: 7 Nothing R: 7 Nothing 7 5 Done

5 Root 7 NULL 3 Inorder - LNR Starting at root Starting at root LNR: 5 LNR: 3 Nothing NR: 3R: 3 Nothing 3 NR: 5R: 5 LNR: 7 Nothing NR: 7R: 7 Nothing 7 5Done

5 Root 7 NULL 3 Postorder - LRN Starting at root Starting at root LRN: 5 Nothing LRN: 3RN: 3 Nothing N: 33 RN: 5 LRN: 7 Nothing RN: 7 Nothing N: 77 N: 55Done