Algorithms and Data Structures Lecture 4. Agenda: Trees – fundamental notions, variations Binary search tree.

Slides:



Advertisements
Similar presentations
Trees Types and Operations
Advertisements

SUNY Oneonta Data Structures and Algorithms Visualization Teaching Materials Generation Group Binary Search Tree A running demonstration of binary search.
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.
© 2006 Pearson Addison-Wesley. All rights reserved11 B-1 Chapter 11 (continued) Trees.
Data Structures: Trees i206 Fall 2010 John Chuang Some slides adapted from Marti Hearst, Brian Hayes, or Glenn Brookshear.
Data Structures and Algorithms1 B-Trees with Minimum=1 2-3 Trees.
6/14/2015 6:48 AM(2,4) Trees /14/2015 6:48 AM(2,4) Trees2 Outline and Reading Multi-way search tree (§3.3.1) Definition Search (2,4)
Kymberly Fergusson CSE1303 Part A Data Structures and Algorithms Summer Semester 2003 Lecture A12 – Binary Trees.
CS 104 Introduction to Computer Science and Graphics Problems Data Structure & Algorithms (4) Data Structures 11/18/2008 Yang Song.
Kymberly Fergusson CSE1303 Part A Data Structures and Algorithms Summer Semester 2003 Lecture A12 – Binary Trees.
B + -Trees (Part 2) Lecture 21 COMP171 Fall 2006.
David Luebke 1 7/2/2015 ITCS 6114 Binary Search Trees.
1 Section 9.2 Tree Applications. 2 Binary Search Trees Goal is implementation of an efficient searching algorithm Binary Search Tree: –binary tree in.
AVL Trees / Slide 1 Deletion  To delete a key target, we find it at a leaf x, and remove it. * Two situations to worry about: (1) target is a key in some.
Binary Trees Chapter 6.
Data Structures Arrays both single and multiple dimensions Stacks Queues Trees Linked Lists.
Tree.
Data Structures - CSCI 102 Binary Tree In binary trees, each Node can point to two other Nodes and looks something like this: template class BTNode { public:
Min Chen School of Computer Science and Engineering Seoul National University Data Structure: Chapter 7.
Chapter 12. Binary Search Trees. Search Trees Data structures that support many dynamic-set operations. Can be used both as a dictionary and as a priority.
Lecture 10 Trees –Definiton of trees –Uses of trees –Operations on a tree.
© 2011 Pearson Addison-Wesley. All rights reserved 11 B-1 Chapter 11 (continued) Trees.
Binary Trees 2 Overview Trees. Terminology. Traversal of Binary Trees. Expression Trees. Binary Search Trees.
Chapter 6 Binary Trees. 6.1 Trees, Binary Trees, and Binary Search Trees Linked lists usually are more flexible than arrays, but it is difficult to use.
Binary Search Trees Binary Search Trees (BST)  the tree from the previous slide is a special kind of binary tree called a binary.
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.
Starting at Binary Trees
Data Structures Haim Kaplan and Uri Zwick November 2012 Lecture 3 Dynamic Sets / Dictionaries Binary Search Trees.
2-3 Trees Extended tree.  Tree in which all empty subtrees are replaced by new nodes that are called external nodes.  Original nodes are called internal.
Preview  Graph  Tree Binary Tree Binary Search Tree Binary Search Tree Property Binary Search Tree functions  In-order walk  Pre-order walk  Post-order.
Lecture 9 Algorithm Analysis Arne Kutzner Hanyang University / Seoul Korea.
Topics Definition and Application of Binary Trees Binary Search Tree Operations.
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.
Introduction to Trees IT12112 Lecture 05 Introduction Tree is one of the most important non-linear data structures in computing. It allows us to implement.
Binary Search Trees Lecture 5 1. Binary search tree sort 2.
Red-Black Trees. Review: Binary Search Trees ● Binary Search Trees (BSTs) are an important data structure for dynamic sets ● In addition to satellite.
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.
Rooted Tree a b d ef i j g h c k root parent node (self) child descendent leaf (no children) e, i, k, g, h are leaves internal node (not a leaf) sibling.
Binary Search Trees ©Robert E. Tarjan Dictionary: contains a set S of items, each with associated information. Operations: Access(x): Determine.
CSE 2331/5331 Topic 8: Binary Search Tree Data structure Operations.
BINARY TREES Objectives Define trees as data structures Define the terms associated with trees Discuss tree traversal algorithms Discuss a binary.
TREES General trees Binary trees Binary search trees AVL trees Balanced and Threaded trees.
2 Binary Heaps What if we’re mostly concerned with finding the most relevant data?  A binary heap is a binary tree (2 or fewer subtrees for each node)
Trees By JJ Shepherd. Introduction Last time we discussed searching and sorting in a more efficient way Divide and Conquer – Binary Search – Merge Sort.
CS6045: Advanced Algorithms Data Structures. Dynamic Sets Next few lectures will focus on data structures rather than straight algorithms In particular,
Fundamentals of Algorithms MCS - 2 Lecture # 17. Binary Search Trees.
Tree Representation and Terminology Binary Trees Binary Search Trees Pointer-Based Representation of a Binary Tree Array-Based Representation of a Binary.
Non Linear Data Structure
Recursive Objects (Part 4)
Binary search tree. Removing a node
CISC220 Fall 2009 James Atlas Lecture 13: Binary Trees.
Multiway search trees and the (2,4)-tree
Lecture 22 Binary Search Trees Chapter 10 of textbook
Section 8.1 Trees.
Trees.
Tonga Institute of Higher Education
Lecture 7 Algorithm Analysis
TREES General trees Binary trees Binary search trees AVL trees
Data Structures and Algorithms
Introduction to Trees IT12112 Lecture 05.
Lecture 26 Multiway Search Trees Chapter 11 of textbook
Tree A tree is a data structure in which each node is comprised of some data as well as node pointers to child nodes
Search Sorted Array: Binary Search Linked List: Linear Search
Binary Search Trees (13.1/12.1)
Lecture 7 Algorithm Analysis
Lecture 7 Algorithm Analysis
Chapter 20: Binary Trees.
Trees.
Chapter 11 Trees © 2011 Pearson Addison-Wesley. All rights reserved.
Presentation transcript:

Algorithms and Data Structures Lecture 4

Agenda: Trees – fundamental notions, variations Binary search tree

Data Structures: Trees Tree is a popular data structure; information is organized in form of trees Tree consists of nodes that may have child nodes Node that has no child nodes is a leaf Each tree has unique root node

Data Structures: Trees

Trees are divided into two main groups: Trees with limited number of child nodes, k- tree Particular case if k=2, binary trees Trees with arbitrary number of child nodes, arbitrary tree

Data Structures: binary tree Each node may have two children Each node includes: key of some type, pointer to a left (first) child node, pointer to a right (second) node and, optionally, pointer to a parent node If some node (child or parent) is absent then pointer is set to NULL

Data Structures: binary tree

Data Structures: arbitrary tree Each node may have any number of child nodes

Data Structures: arbitrary tree Number of children is not known Number of children may vary from node to node Direct representation is not available Arbitrary tree is usually represented by “left-child, right- sibling” principle

Data Structures: arbitrary tree Each node includes: key of some type, pointer to a left child (first), pointer to a next sibling of the same level and, optionally, pointer to a parent node If some node (child, sibling or parent) is absent then pointer is set to NULL

Data Structures: arbitrary tree

Data Structures: Binary search tree Binary search tree is a binary tree which nodes are arranged by keys If x is an arbitrary node of a binary tree and y is arbitrary node of a left sub-tree with root x and z is an arbitrary node of a right sub-tree with root x, then key[y]<=key[x]<=key[z]

Data Structures: Binary search tree

Operations: Enumerate Search Insert Remove Predecessor Successor Minimum Maximum

Data Structures: Binary search tree -sample

Minimum operation – returns node with minimal key value Procedure goes through the tree (from top to bottom) keeping the left side; returns leftmost and bottommost node Maximum operation – returns node with maximal key value Procedure goes through the tree (from top to bottom) keeping the right side; returns rightmost and bottommost node

Data Structures: Binary search tree -sample

Search operation – returns node with the specified key value, if any; otherwise NULL Procedure goes through the tree (from top to bottom) Key value of each visited node is compared against the interested key value Direction of movement depends on result of the comparison; if given key value is greater – procedure continues with right sub-tree, otherwise with left one

Data Structures: Binary search tree -sample

Successor operation – returns next node in terms of key values; NULL if node is not found Predecessor operation – returns previous node in terms of key values ; NULL if node is not found

Data Structures: Binary search tree -sample

Insert operation – adds the specified node to the tree, preserving the ascending order of nodes (by value of key) During the procedure algorithm goes through the tree (from the top to the bottom) Key value of each visited node is compared against the key value of the new node Direction of the movement depends on the result of comparison; if key value of inserted node is greater – procedure continues with right sub-tree, otherwise with left one

Data Structures: Binary search tree -sample

Remove operation – removes the specified node from the tree, preserving the ascending order of nodes (by value of key) We have to consider three cases: Being removed node has no children at all Being removed node has one child node Being removed node has two children

Data Structures: Binary search tree -sample CASE 1: being removed node has no children at all

Data Structures: Binary search tree -sample CASE 2: being removed node has one child node

Data Structures: Binary search tree -sample Being removed node has two children

Data Structures: Binary search tree -sample Enumerate is Θ(n) Search, Minimum, Maximum, successor, Predecessor, Insert and Remove are O(h), h – is a height of a tree

Data Structures: Binary search tree -sample

Q&A