Fundamentals of Algorithms MCS - 2 Lecture # 17. Binary Search Trees.

Slides:



Advertisements
Similar presentations
Chapter 12 Binary Search Trees
Advertisements

Comp 122, Spring 2004 Binary Search Trees. btrees - 2 Comp 122, Spring 2004 Binary Trees  Recursive definition 1.An empty tree is a binary tree 2.A node.
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,
David Luebke 1 5/4/2015 Binary Search Trees. David Luebke 2 5/4/2015 Dynamic Sets ● Want a data structure for dynamic sets ■ Elements have a key and satellite.
Binary Trees Chapter 6. Linked Lists Suck By now you realize that the title to this slide is true… By now you realize that the title to this slide is.
Binary Trees, Binary Search Trees CMPS 2133 Spring 2008.
Binary Trees, Binary Search Trees COMP171 Fall 2006.
Computer Science 2 Data Structures and Algorithms V section 2 Introduction to Trees Professor: Evan Korth New York University.
Kymberly Fergusson CSE1303 Part A Data Structures and Algorithms Summer Semester 2003 Lecture A12 – Binary Trees.
Universal Hashing When attempting to foil an malicious adversary, randomize the algorithm Universal hashing: pick a hash function randomly when the algorithm.
Lec 15 April 9 Topics: l binary Trees l expression trees Binary Search Trees (Chapter 5 of text)
1.1 Data Structure and Algorithm Lecture 12 Binary Search Trees Topics Reference: Introduction to Algorithm by Cormen Chapter 13: Binary Search Trees.
Kymberly Fergusson CSE1303 Part A Data Structures and Algorithms Summer Semester 2003 Lecture A12 – Binary Trees.
Binary Trees. Linear data structures Here are some of the data structures we have studied so far: –Arrays –Singly-linked lists and doubly-linked lists.
David Luebke 1 7/2/2015 ITCS 6114 Binary Search Trees.
Three Types of Depth-First Search Alyce Brady CS 470: Data Structures CS 510: Computer Algorithms.
Binary Trees. 2 Linear data structures Here are some of the data structures we have studied so far: –Arrays –Singly-linked lists and doubly-linked lists.
Trees Chapter 8. 2 Tree Terminology A tree consists of a collection of elements or nodes, organized hierarchically. The node at the top of a tree is called.
Lecture 81 Data Structures, Algorithms & Complexity Tree Algorithms GRIFFITH COLLEGE DUBLIN.
1 Trees A tree is a data structure used to represent different kinds of data and help solve a number of algorithmic problems Game trees (i.e., chess ),
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.
Binary Trees. 2 Parts of a binary tree A binary tree is composed of zero or more nodes In Java, a reference to a binary tree may be null Each node contains:
Binary SearchTrees [CLRS] – Chap 12. What is a binary tree ? A binary tree is a linked data structure in which each node is an object that contains following.
Computer Science: A Structured Programming Approach Using C Trees Trees are used extensively in computer science to represent algebraic formulas;
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.
Binary Search Tree Qamar Abbas.
 Trees Data Structures Trees Data Structures  Trees Trees  Binary Search Trees Binary Search Trees  Binary Tree Implementation Binary Tree Implementation.
Preview  Graph  Tree Binary Tree Binary Search Tree Binary Search Tree Property Binary Search Tree functions  In-order walk  Pre-order walk  Post-order.
Trees Isaac Sheff. Nodes Building blocks of trees “Parent” node may have “Child” nodes Can be both parent and child Can’t be its own ancestor Can’t have.
Computer Science 112 Fundamentals of Programming II Introduction to Trees.
1 Algorithms CSCI 235, Fall 2015 Lecture 22 Binary Search Trees.
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.
Trees By P.Naga Srinivasu M.tech,(MBA). Basic Tree Concepts A tree consists of finite set of elements, called nodes, and a finite set of directed lines.
12.Binary Search Trees Hsu, Lih-Hsing. Computer Theory Lab. Chapter 12P What is a binary search tree? Binary-search property: Let x be a node in.
Binary Search Trees Lecture 5 1. Binary search tree sort 2.
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.
1 Lecture 21: Binary Search Tree delete etc. operations Lecturer: Santokh Singh CompSci 105 SS 2005 Principles of Computer Science.
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)
Lecture 19. Binary Search Tree 1. Recap Tree is a non linear data structure to present data in hierarchical form. It is also called acyclic data structure.
Fundamentals of Algorithms MCS - 2 Lecture # 17. Binary Search Trees.
DATA STRUCTURE BS(IT)3rd. Tree An Introduction By Yasir Mustafa Roll No. BS(IT) Bahauddin Zakariya University, Multan.
Traversing a tree means visiting each node in a specified order. There are different ways to traverse a tree. Tree Traversing Depth first search Pre-orderIn-orderPost-order.
Binary Trees. 2 Parts of a binary tree A binary tree is composed of zero or more nodes In Java, a reference to a binary tree may be null Each node contains:
CSE 2331/5331 Topic 8: Binary Search Tree Data structure Operations.
Binary Trees.
Binary Trees.
Fundamentals of Programming II Introduction to Trees
Binary Search Tree (BST)
Binary Search Tree Chapter 10.
Section 8.1 Trees.
Binary Trees, Binary Search Trees
Ch. 12: Binary Search Trees Ming-Te Chi
Alyce Brady CS 470: Data Structures CS 510: Computer Algorithms
Binary Trees.
Binary Trees.
Binary Trees.
Ch. 12: Binary Search Trees Ming-Te Chi
Chapter 12: Binary Search Trees
Binary Trees, Binary Search Trees
Binary Trees.
Chapter 20: Binary Trees.
Binary Trees.
Trees.
Binary Trees, Binary Search Trees
Presentation transcript:

Fundamentals of Algorithms MCS - 2 Lecture # 17

Binary Search Trees

BST: Definition  In computer science, a binary search tree (BST), sometimes also called an ordered or sorted binary tree, is a node-based binary tree data structure which has the following BST properties  The left subtree of a node contains only nodes with keys less than the node's key.  The right subtree of a node contains only nodes with keys greater than the node's key.  The left and right subtree each must also be a binary search tree.  There must be no duplicate nodes. BST Property  For all nodes x and y,  if y belongs to the left subtree of x, then the key at y is less than the key at x, and  if y belongs to the right subtree of x, then the key at y is greater than the key at x.

4 Parts of a binary tree  A binary tree is composed of zero or more nodes  Each node contains  A value (some sort of data item)  A reference or pointer to a left child (may be null), and  A reference or pointer to a right child (may be null)  A binary tree may be empty (contain no nodes)  If not empty, a binary tree has a root node  Every node in the binary tree is reachable from the root node by a unique path  A node with neither a left child nor a right child is called a leaf. A binary search tree of size 9 and depth 3, with root 8 and leaves 1, 4, 7 and 13

5 Size and depth of BST  The size of a binary tree is the number of nodes in it  This tree has size 12  The depth of a node is its distance from the root  l is at depth zero  e is at depth 2  The depth of a binary tree is the depth of its deepest node  This tree has depth 4 a bc def ghijk l

Traversal / Walk of the Nodes in a BST  Traversal means visiting all the nodes in a graph.  There are three traversal strategies.  Preorder  The ordering is: the current node, the left subtree, the right subtree.  Inorder  The ordering is: the left subtree, the current node, the right subtree.  Postorder  The ordering is: the left subtree, the right subtree, the current node.

7 BST Traversals using “Flags”  The order in which the nodes are visited during a tree traversal can be easily determined by imagining there is a “flag” attached to each node, as follows:  To traverse the tree, collect the flags: preorderinorderpostorder A BC DEFG A BC DEFG A BC DEFG A B D E C F G D B E A F C G D E B F G C A

Example  Inorder traversal gives  2, 3, 4, 5, 6, 7, 8, 9, 11, 12, 15,19, 20  Preorder traversal gives  7, 4, 2, 3, 6, 5, 12, 9, 8, 11, 19, 15, 20  Postorder traversal gives  3, 2, 5, 6, 4, 8, 11, 9, 15, 20, 19, 12, 7  What is the outcome of inorder, postorder and preorder traversal on this BST?

In-order BST Traversal Pseudo code  INORDER-TREE-WALK(x)  1 if x ≠ NIL  2 then INORDER-TREE-WALK ( left [x] )  3 print key [x]  4 INORDER-TREE-WALK ( right [x] )  Running time  Θ (n), where n is the size of the tree rooted at x

Operations on BST  Search(S,k)  Insert(S,x)  Delete(S,x)  Minimum or Maximum(S)  Successor or Predecessor (S,x)  List All(S)  Merge(S 1,S 2 )

BST Search Pseudo code  Here k is the key that is searched for and x is the start node.  TREE-SEARCH(x, k)  1 if x = NIL or k = key [x]  2 then return x  3 if k < key [x]  4 then return TREE-SEARCH (left [x], k )  5 else return TREE-SEARCH (right [x], k )  Running time: O(h), h – height of tree

Good Luck ! ☻