S EARCHING AND T REES COMP1927 Computing 15s1 Sedgewick Chapters 5, 12.

Slides:



Advertisements
Similar presentations
S. Sudarshan Based partly on material from Fawzi Emad & Chau-Wen Tseng
Advertisements

1 abstract containers hierarchical (1 to many) graph (many to many) first ith last sequence/linear (1 to 1) set.
TREES Chapter 6. Trees - Introduction  All previous data organizations we've studied are linear—each element can have only one predecessor and successor.
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.
Main Index Contents 11 Main Index Contents Week 6 – Binary Trees.
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.
Binary Trees Terminology A graph G = is a collection of nodes and edges. An edge (v 1,v 2 ) is a pair of vertices that are directly connected. A path,
1 Trees. 2 Outline –Tree Structures –Tree Node Level and Path Length –Binary Tree Definition –Binary Tree Nodes –Binary Search Trees.
BST Data Structure A BST node contains: A BST contains
Data Structures Data Structures Topic #8. Today’s Agenda Continue Discussing Table Abstractions But, this time, let’s talk about them in terms of new.
Lec 15 April 9 Topics: l binary Trees l expression trees Binary Search Trees (Chapter 5 of text)
© 2006 Pearson Addison-Wesley. All rights reserved11 A-1 Chapter 11 Trees.
1 abstract containers hierarchical (1 to many) graph (many to many) first ith last sequence/linear (1 to 1) set.
Binary Search Trees Chapter 7 Objectives
Binary Trees Chapter 6.
Version TCSS 342, Winter 2006 Lecture Notes Trees Binary Trees Binary Search Trees.
By : Budi Arifitama Pertemuan ke Objectives Upon completion you will be able to: Create and implement binary search trees Understand the operation.
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.
Advanced Algorithms Analysis and Design Lecture 8 (Continue Lecture 7…..) Elementry Data Structures By Engr Huma Ayub Vine.
1 Trees Tree nomenclature Implementation strategies Traversals –Depth-first –Breadth-first Implementing binary search trees.
Chapter 19: Binary Trees. Objectives In this chapter, you will: – Learn about binary trees – Explore various binary tree traversal algorithms – Organize.
1 TK1924 Program Design & Problem Solving Session 2011/2012 L8: Binary Trees.
CS Data Structures Chapter 5 Trees. Chapter 5 Trees: Outline  Introduction  Representation Of Trees  Binary Trees  Binary Tree Traversals 
Lecture 10 Trees –Definiton of trees –Uses of trees –Operations on a tree.
Spring 2010CS 2251 Trees Chapter 6. Spring 2010CS 2252 Chapter Objectives Learn to use a tree to represent a hierarchical organization of information.
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 ),
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 Trees, Binary Search Trees RIZWAN REHMAN CENTRE FOR COMPUTER STUDIES DIBRUGARH UNIVERSITY.
Binary Trees Definition A binary tree is: (i) empty, or (ii) a node whose left and right children are binary trees typedef struct Node Node; struct Node.
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.
Trees, Binary Trees, and Binary Search Trees COMP171.
Starting at Binary Trees
Trees  Linear access time of linked lists is prohibitive Does there exist any simple data structure for which the running time of most operations (search,
 Trees Data Structures Trees Data Structures  Trees Trees  Binary Search Trees Binary Search Trees  Binary Tree Implementation Binary Tree Implementation.
CSC 143 Trees [Chapter 10].
Chapter 4: Trees Part I: General Tree Concepts Mark Allen Weiss: Data Structures and Algorithm Analysis in Java.
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 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)
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.
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]
BINARY TREES A BINARY TREE t IS EITHER EMPTY OR CONSISTS OF AN ITEM, CALLED THE ROOT ITEM, AND TWO DISTINCT BINARY TREES, CALLED THE LEFT SUBTREE AND.
Concepts of Algorithms CSC-244 Unit 19 & 20 Binary Search Tree (BST) Shahid Iqbal Lone Computer College Qassim University K.S.A.
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.
1 Trees. Objectives To understand the concept of trees and the standard terminology used to describe them. To appreciate the recursive nature of a tree.
BINARY TREES Objectives Define trees as data structures Define the terms associated with trees Discuss tree traversal algorithms Discuss a binary.
353 3/30/98 CSE 143 Trees [Sections , 13.6,13.8]
(c) University of Washington20c-1 CSC 143 Binary Search 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.
1 Trees. 2 Trees Trees. Binary Trees Tree Traversal.
DS.T.1 Trees Chapter 4 Overview Tree Concepts Traversals Binary Trees Binary Search Trees AVL Trees Splay Trees B-Trees.
Trees Chapter 15.
Week 6 - Wednesday CS221.
Binary Search Tree (BST)
Trees.
Tree.
Data Structures & Algorithm Design
abstract containers sequence/linear (1 to 1) hierarchical (1 to many)
Binary Trees, Binary Search Trees
Map interface Empty() - return true if the map is empty; else return false Size() - return the number of elements in the map Find(key) - if there is an.
Binary Trees, Binary Search Trees
Trees.
Binary Trees, Binary Search Trees
Presentation transcript:

S EARCHING AND T REES COMP1927 Computing 15s1 Sedgewick Chapters 5, 12

S EARCHING Storing and searching sorted data: Dilemma: Inserting into a sorted sequence Finding the insertion point on an array – O(log n) but then we have to move everything along to create room for the new item Finding insertion point on a linked list O(n) but then we can add the item in constant time. Can we get the best of both worlds?

T REE TERMINOLOGY Trees are branched data structures consisting of nodes and links (edges), with no cycles each node contains a data value each node has links to ≤ k other nodes ( k=2 below)

T REES AND SUBTREES Trees can be viewed as a set of nested structures: each node has k possibly empty subtrees

U SES OF TREES Trees are used in many contexts, e.g. representing hierarchical data structures (e.g. expressions) efficient searching (e.g. sets, symbol tables,...)

S PECIAL PROPERTIES OF SOME TREES M-ary tree: each internal node has exactly M children Ordered tree: constraints on the data/keys in the nodes Balanced tree: a tree with a minimal height for a given number of nodes Degenerated tree: a tree with the maximal height for a given number of nodes

B INARY TREES For much of this course, we focus on binary trees ( k=2 ) Binary trees can be defined recursively, as follows: A binary tree is either empty (contains no nodes) consists of a node, with two subtrees each node contains a value the left and right subtrees are binary trees

…T REE T ERMINOLGY Node level or depth = path length from root to node Depth of the root is 0 Depth of a node is one higher than the level of its parent We call the length of the longest path from the root to a node the height of a tree 8

B INARY T REES : P ROPERTIES A binary tree with n nodes has a height of at most n-1 (if degenerate) at least floor(log 2 (n)) (if balanced) These properties are important to estimate the runtime complexity of tree algorithms! log log log log log

E XAMPLES OF BINARY SEARCH TREES : Shape of tree is determined by the order of insertion

E XERCISE : I NSERTION INTO BST S For each of the sequences below start from an initially empty binary search tree show the tree resulting from inserting the values in the order given What is the height of each tree? (a) (b) (c)

B INARY T REES IN C Concrete representation: a binary tree is a generalisation of a linked list: nodes are a structure with two links to nodes empty trees are NULL links typedef struct treenode *Treelink; struct treenode { int data; Treelink left, right; } typedef struct treenode *Treelink; struct treenode { int data; Treelink left, right; }

S EARCHING IN BST S Recursive version // Returns non-zero if item is found, // zero otherwise int search(TreeLink n, Item i){ int result; if(n == NULL){ result = 0; }else if(i data){ result = search(n->left,i); }else if(i > n->data) result = search(n->right,i); }else{ // you found the item result = 1; } return result; } * Exercise: Try writing an iterative version

I NSERTION INTO A BST Cases for inserting value V into tree T: T is empty, make new node with V as root of new tree root node contains V, tree unchanged (no dups) V < value in root, insert into left subtree (recursive) V > value in root, insert into right subtree (recursive) Non-recursive insertion of V into tree T: search to location where V belongs, keeping parent make new node and attach to parent whether to attach L or R depends on last move

B INARY T REES : T RAVERSAL For trees, several well-defined visiting orders exist: Depth first traversals preorder (NLR)... visit root, then left subtree, then right subtree inorder (LNR)... visit left subtree, then root, then right subtree postorder (LRN)... visit left subtree, then right subtree, then root Breadth-first traversal or level-order... visit root, then all its children, then all their children

E XAMPLE OF TRAVERSALS ON A B INARY TREE Pre-Order: In-Order: Post-Order Level-Order:

D ELETION FROM BSTS Insertion into a binary search tree is easy: find location in tree where node to be added create node and link to parent Deletion from a binary search tree is harder: find the node to be deleted and its parent unlink node from parent and delete replace node in tree by... ???

D ELETION FROM BSTS … Easy option... don't delete; just mark node as deleted future searches simply ignore marked nodes If we want to delete, three cases to consider... zero subtrees... unlink node from parent one subtree... replace node by child two subtrees... two children; one link in parent

D ELETION FROM BSTS Case 1: value to be deleted is a leaf (zero subtrees)

D ELETION FROM BSTS Case 1: value to be deleted is a leaf (zero subtrees)

D ELETION FROM BSTS Case 2: value to be deleted has one subtree

D ELETION FROM BSTS Case 2: value to be deleted has one subtree

D ELETION FROM BSTS Case 3a: value to be deleted has two subtrees Replace deleted node by its immediate successor The smallest (leftmost) node in the right subtree

D ELETION FROM BSTS Case 3a: value to be deleted has two subtrees

B INARY S EARCH T REE P ROPERTIES Cost for searching/deleting: Worst case: key is not in BST – search the height of the tree Balanced trees – O(log 2 n) Degenerate trees – O(n) Cost for insertion: Always traverse the height of the tree Balanced trees – O(log 2 n) Degenerate trees – O(n)