Binary Search Trees Data Structures Ananda Gunawardena

Slides:



Advertisements
Similar presentations
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.
Advertisements

Binary Trees, Binary Search Trees CMPS 2133 Spring 2008.
Binary Trees, Binary Search Trees COMP171 Fall 2006.
CS 171: Introduction to Computer Science II
Trees, Binary Trees, and Binary Search Trees COMP171.
1 Trees. 2 Outline –Tree Structures –Tree Node Level and Path Length –Binary Tree Definition –Binary Tree Nodes –Binary Search Trees.
Lists A list is a finite, ordered sequence of data items. Two Implementations –Arrays –Linked Lists.
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
Introduction to Data Structure, Fall 2006 Slide- 1 California State University, Fresno Introduction to Data Structure Chapter 10 Ming Li Department of.
Lec 15 April 9 Topics: l binary Trees l expression trees Binary Search Trees (Chapter 5 of text)
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.
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.
BINARY TREES && TREE TRAVERSALS. DEFINITION : Binary Tree A binary tree is made of nodes Each node contains –a "left" pointer -- left child –a "right"
1 Chapter 18 Trees Objective To learn general trees and recursion binary trees and recursion tree traversal.
Lecture 17 Non-Linear data structures Richard Gesick.
1 TK1924 Program Design & Problem Solving Session 2011/2012 L8: Binary Trees.
CS Data Structures Chapter 15 Trees Mehmet H Gunes
1 Joe Meehean. A A B B D D I I C C E E X X A A B B D D I I C C E E X X  Terminology each circle is a node pointers are edges topmost node is the root.
Lecture 10 Trees –Definiton of trees –Uses of trees –Operations on a tree.
S EARCHING AND T REES COMP1927 Computing 15s1 Sedgewick Chapters 5, 12.
CISC220 Fall 2009 James Atlas Lecture 13: Trees. Skip Lists.
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 ),
CMSC 341 Introduction to Trees. 8/3/2007 UMBC CMSC 341 TreeIntro 2 Tree ADT Tree definition  A tree is a set of nodes which may be empty  If not empty,
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.
Compiled by: Dr. Mohammad Omar Alhawarat
Trees, Binary Trees, and Binary Search Trees COMP171.
 Trees Data Structures Trees Data Structures  Trees Trees  Binary Search Trees Binary Search Trees  Binary Tree Implementation Binary Tree Implementation.
Review 1 Queue Operations on Queues A Dequeue Operation An Enqueue Operation Array Implementation Link list Implementation Examples.
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.
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.
CMSC 341 Introduction to Trees. 2/21/20062 Tree ADT Tree definition –A tree is a set of nodes which may be empty –If not empty, then there is a distinguished.
Binary Tree. Some Terminologies Short review on binary tree Tree traversals Binary Search Tree (BST)‏ Questions.
1/14/20161 BST Operations Data Structures Ananda Gunawardena.
ADT Binary Search Tree Ellen Walker CPSC 201 Data Structures Hiram College.
Binary Search Trees (BST)
DATA STRUCTURE BS(IT)3rd. Tree An Introduction By Yasir Mustafa Roll No. BS(IT) Bahauddin Zakariya University, Multan.
Trees Ellen Walker CPSC 201 Data Structures Hiram College.
1 Trees General Trees  Nonrecursive definition: a tree consists of a set of nodes and a set of directed edges that connect pairs of nodes.
1 Joe Meehean. A A B B D D I I C C E E X X A A B B D D I I C C E E X X  Terminology each circle is a node pointers are edges topmost node is the root.
CMSC 202, Version 5/02 1 Trees. CMSC 202, Version 5/02 2 Tree Basics 1.A tree is a set of nodes. 2.A tree may be empty (i.e., contain no nodes). 3.If.
Foundation of Computing Systems Lecture 4 Trees: Part I.
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.
1 Trees General Trees  Nonrecursive definition: a tree consists of a set of nodes and a set of directed edges that connect pairs of nodes.
Question 4 Tutorial 8. Part A Insert 20, 10, 15, 5,7, 30, 25, 18, 37, 12 and 40 in sequence into an empty binary tree
Fundamentals of Algorithms MCS - 2 Lecture # 17. Binary Search Trees.
1 Trees. 2 Trees Trees. Binary Trees Tree Traversal.
TREES From root to leaf. Trees  A tree is a non-linear collection  The elements are in a hierarchical arrangement  The elements are not accessible.
Trees Chapter 15.
Week 6 - Wednesday CS221.
Binary Search Tree (BST)
Tree.
Section 8.1 Trees.
Data Structures & Algorithm Design
ITEC 2620M Introduction to Data Structures
Binary Trees, Binary Search Trees
Binary Trees.
Trees 7/14/2009.
Trees.
Binary Trees.
Binary Trees.
Binary Trees, Binary Search Trees
Trees.
Chapter 20: Binary Trees.
Binary Trees.
Trees.
Binary Trees, Binary Search Trees
Chapter 11 Trees © 2011 Pearson Addison-Wesley. All rights reserved.
Presentation transcript:

Binary Search Trees 15-111 Data Structures Ananda Gunawardena 4/25/2017

Tree Data Structure A non-linear data structure that follows the shape of a tree (i.e. root, children, branches, leaves etc) Most applications require dealing with hierarchical data (eg: organizational structure) Trees allows us to find things efficiently Navigation is O(log n) for a “balanced” tree with n nodes A Binary Search Tree (BST) is a data structure that can be traversed / searched according to an order A binary tree is a tree such that each node can have at most 2 children. 4/25/2017

BST In Pictures Empty Tree x L R 4/25/2017

Definition of Flat T Given a Binary Tree T, Flat(T) is a sequence obtained by traversing the tree using inorder traversal That is for each node, recursively visit Left Tree, Then Root, then Right Tree root L R 4/25/2017

Flattening a BT a b d e f g T flat(T) = e, b,f,a,d,g 4/25/2017

Def: Binary Search Tree A binary Tree is a binary search tree (BST) if and only if flat(T) is an ordered sequence. Equivalently, in (x,L,R) all the nodes in L are less than x, and all the nodes in R are larger than x. 4/25/2017

BT Definitions 4/25/2017

Definitions A path from node n1 to nk is defined as a path n1,n2, …. nk such that ni is the parent of ni+1 Depth of a node is the length of the path from root to the node. Height of a node is length of a path from node to the deepest leaf. Height of the tree is the height of the root 4/25/2017

Definitions Full Binary Tree Complete Binary Tree Perfect Tree Each node has zero or two children Complete Binary Tree All levels of the tree is full, except possibly the last level, where nodes are filled from left to right Perfect Tree A Tree is perfect if total number of nodes in a tree of height h is 2h+1 -1 4/25/2017

Binary Tree Questions What is the maximum height of a binary tree with n nodes? What is the minimum height? What is the minimum and maximum number of nodes in a binary tree of height h? What is the minimum number of nodes in a full tree of height h? Is a complete tree a full tree? Is perfect tree a full and complete tree? 4/25/2017

Binary Tree Properties Counting Nodes in a Binary Tree The max number of nodes at level i is 2i (i=0,1,…,h) Therefore total nodes in all levels is Find a relation between n and h. A complete tree of height, h, has between 2h and 2h+1-1 nodes. A perfect tree of height h has 2h+1-1 nodes 4/25/2017

Binary Tree Questions What is the maximum number of nodes at level k? (root is at level 0) 4/25/2017

BST Operations 4/25/2017

Tree Operations Tree Traversals Insert Node, Delete Node, Find Node Inorder, PreOrder, PostOrder Level Order Insert Node, Delete Node, Find Node Order Statistics for BST’s Find kth largest element num nodes between two values Other operations Count nodes, height of a node, height of a tree, balanced info 4/25/2017

Binary Tree Traversals Inorder Traversals Visit nodes in the order Left-Root-Right PreOrder Traversal Root-Left-Right PostOrder Traversal Visit nodes in Left-Right-Root 4/25/2017

Inorder Traversal private void inorder(BinaryNode root) { if (root != null) { inorder(root.left); process root; inorder(root.right); } 4/25/2017

Preorder Traversal private void preorder(BinaryNode root) { if (root != null) { process root; preorder(root.left); preorder(root.right); } 4/25/2017

Postorder Traversal private void postorder(BinaryNode root) { if (root != null) { postorder(root.left); postorder(root.right); process root; } 4/25/2017

Level order or Breadth-first traversal Visit nodes by levels Root is at level zero At each level visit nodes from left to right Called “Breadth-First-Traversal(BFS)” 4/25/2017

Level order or Breadth-first traversal BFS Algorithm enqueue the root while (the queue is not empty) { dequeue the front element print it enqueue its left child (if present) enqueue its right child (if present) } 4/25/2017