CISC 235 Topic 3 General Trees, Binary Trees, Binary Search Trees.

Slides:



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

Jan Binary Search Trees What is a search binary tree? Inorder search of a binary search tree Find Min & Max Predecessor and successor BST insertion.
Analysis of Algorithms CS 477/677 Binary Search Trees Instructor: George Bebis (Appendix B5.2, Chapter 12)
ALGORITHMS THIRD YEAR BANHA UNIVERSITY FACULTY OF COMPUTERS AND INFORMATIC Lecture six Dr. Hamdy M. Mousa.
UNC Chapel Hill Lin/Foskey/Manocha Binary Search Tree Her bir node u bir object olan bir linked data structure ile temsil edilebilir. Her bir node key,
Binary Search Trees Azhar Maqsood School of Electrical Engineering and Computer Sciences (SEECS-NUST)
Binary Search Trees Comp 550.
Binary Trees, Binary Search Trees CMPS 2133 Spring 2008.
Binary Trees, Binary Search Trees COMP171 Fall 2006.
Trees, Binary Trees, and Binary Search Trees COMP171.
Trees, Binary Trees, and Binary Search Trees. 2 Trees * Linear access time of linked lists is prohibitive n Does there exist any simple data structure.
1 Trees. 2 Outline –Tree Structures –Tree Node Level and Path Length –Binary Tree Definition –Binary Tree Nodes –Binary Search Trees.
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.1 Data Structure and Algorithm Lecture 12 Binary Search Trees Topics Reference: Introduction to Algorithm by Cormen Chapter 13: Binary Search Trees.
Data Structures, Spring 2006 © L. Joskowicz 1 Data Structures – LECTURE Binary search trees Motivation Operations on binary search trees: –Search –Minimum,
1 abstract containers hierarchical (1 to many) graph (many to many) first ith last sequence/linear (1 to 1) set.
Version TCSS 342, Winter 2006 Lecture Notes Trees Binary Trees Binary Search Trees.
CS 146: Data Structures and Algorithms June 18 Class Meeting Department of Computer Science San Jose State University Summer 2015 Instructor: Ron Mak
1 Chapter 18 Trees Objective To learn general trees and recursion binary trees and recursion tree traversal.
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 Objectives  To learn how to use a tree to represent a hierarchical organization of information  To learn how to use recursion to process trees.
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.
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,
DATA STRUCTURES AND ALGORITHMS Lecture Notes 5 Prepared by İnanç TAHRALI.
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.
Tree Data Structures.
Trees, Binary Trees, and Binary Search Trees COMP171.
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,
Binary Search Tree Qamar Abbas.
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.
1 Storing Hierarchical Information Lists, Stacks, and Queues represent linear sequences Data often contain hierarchical relationships that cannot be expressed.
1 Algorithms CSCI 235, Fall 2015 Lecture 22 Binary Search Trees.
Tree Traversals, TreeSort 20 February Expression Tree Leaves are operands Interior nodes are operators A binary tree to represent (A - B) + C.
CSC 172 DATA STRUCTURES. LISTS We have seen lists: public class Node { Object data; Node next; } 
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.
M180: Data Structures & Algorithms in Java Trees & Binary Trees Arab Open University 1.
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 Chapter 4 Trees Basic concept How tree are used to implement the file system How tree can be used to evaluate arithmetic expressions How to use trees.
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.
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.
TREES K. Birman’s and G. Bebis’s Slides. Tree Overview 2  Tree: recursive data structure (similar to list)  Each cell may have zero or more successors.
1 Trees What is a Tree? Tree terminology Why trees? What is a general tree? Implementing trees Binary trees Binary tree implementation Application of Binary.
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.
Trees CSIT 402 Data Structures II 1. 2 Why Do We Need Trees? Lists, Stacks, and Queues are linear relationships Information often contains hierarchical.
1 CMSC 341 Introduction to Trees Textbook sections:
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.
CSE 373 Data Structures Lecture 7
CC 215 Data Structures Trees
Data Structures & Algorithm Design
CSE 373 Data Structures Lecture 7
TREE DATA STRUCTURE Data Structure 21-Sep-18 Tree
Binary Trees, Binary Search Trees
Ch. 12: Binary Search Trees Ming-Te Chi
TREES General trees Binary trees Binary search trees AVL trees
Trees CSE 373 Data Structures.
Ch. 12: Binary Search Trees Ming-Te Chi
Binary Trees, Binary Search Trees
Trees.
Trees CSE 373 Data Structures.
Trees CSE 373 Data Structures.
Binary Trees, Binary Search Trees
Presentation transcript:

CISC 235 Topic 3 General Trees, Binary Trees, Binary Search Trees

CISC 235 Topic 32 Outline General Trees –Terminology, Representation, Properties Binary Trees –Representations, Properties, Traversals Recursive Algorithms for Trees Binary Search Trees –Searching, Insertion, and Deletion –Analysis of Complexity

CISC 235 Topic 33 Rooted Trees In a sequence, each element has zero or one predecessors & zero or one successors In a rooted tree, each element has either zero or one predecessor (the “parent”) and zero or more successors (the “children”)

CISC 235 Topic 34 Tree Terminology Parent of x The node directly above node x in the tree Child of x A node directly below node x in the tree Siblings Nodes with common parent Root Only node with no parent Leaf or External Node A node with no children Internal Node Nonleaf node A HGFED C B I

CISC 235 Topic 35 Tree Terminology Path A sequence of connected nodes Ancestor of x A node on the path from the root to x Descendent of x A node on a path from x to a leaf Empty Tree A tree with no nodes A HGFED C B I

CISC 235 Topic 36 Tree Terminology Height of Tree Number of edges on the longest path from the root to a leaf Depth of x Number of ancestors x has Level of x Number of edges on the path from the root to x (= depth) Degree of x Number of children x has A HGFED C B I

CISC 235 Topic 37 Tree Properties The number of edges in a tree is one less than the number of nodes. |E| = |V| - 1 A HGFED C B I

CISC 235 Topic 38 Binary Trees: Recursive Definition A binary tree is one of: An empty tree A root with two binary trees as children (its left and right sub-trees) A HG F E D C B I

CISC 235 Topic 39 Tree Implementations What would a linked representation of a binary tree look like? What would a linked representation of a general tree look like? What would an array representation of a binary tree look like?

CISC 235 Topic 310 Representing a General Tree as a Binary Tree I D B A E C FGH I D B A E C FGH

CISC 235 Topic 311 Array Implementation Node Location Root1 Left Child of x2 * location of x Right Child of x (2 * location of x) + 1 A E D C B ABCDE

CISC 235 Topic 312 Traversal Sequences of Binary Trees Level Order Preorder Root Inorder Root Postorder Root A HG F E D C B I LTRT LT RT

CISC 235 Topic 313 Inorder Traversal Algorithm Inorder-Tree-Walk ( x ) if x  NIL then Inorder-Tree-Walk( left[x] ) print key[x] Inorder-Tree-Walk( right[x] )

CISC 235 Topic 314 Inorder Traversal Print Method void printInorder ( TreeNode root ) { if ( root != null ) {printInorder( root.left ); System.out.println ( root.data ); printInorder( root.right ); }

CISC 235 Topic 315 Preorder Traversal for General Tree Preorder-Tree-Walk ( x ) print key[x] for each child w of x do Preorder-Tree-Walk( w ) A HGFED C B I

CISC 235 Topic 316 Application: Expression Trees Infix – customary form log a * (b + c) Prefix – every operator before its operand(s) * log a + b c Postfix – every operator after its operand(s) a log b c + * c + log * ab

CISC 235 Topic 317 Recursive Thinking with Binary Trees Assume you have the solution for both the left and right sub-tree (i.e., the recursive calls). Then figure out the solution for the whole tree.

CISC 235 Topic 318 Recursive Thinking with General Trees Assume you have the solution for all of the sub-trees (i.e., the recursive calls). Then figure out the solution for the whole tree. …

CISC 235 Topic 319 Practice Thinking Recursively 1.Write an algorithm to calculate the total number of nodes in a binary tree. 2.Write an algorithm to calculate the height of a binary tree.

CISC 235 Topic 320 Application: File Systems 1.Print a list of all the files and folders in a hierarchical directory structure 2.Calculate the total number of blocks used by all files in the directory system.

CISC 235 Topic 321 Print a Directory List (pseudocode) void listAll( int depth ) { printName( depth ); // Print name of object if ( isDirectory( ) ) for each file c in this directory (each child) c.listAll( depth + 1 ); }

CISC 235 Topic 322 Calculate Total Size of All Files in a Directory (pseudocode) int size( ) { int totalSize = sizeOfThisFile( ); if ( isDirectory( ) ) for each file c in this directory (each child) totalSize += c.size( ); return totalSize; }

CISC 235 Topic 323 Binary Tree Properties Minimum # of nodes, minN, in a tree of height h: minN = ___________________ Maximum height, maxH, for tree of n nodes: maxH = __________________ Maximum # of nodes, maxN, in a tree of height h: maxN = __________________ Minimum height, minH, in tree of n nodes: minH = __________________

CISC 235 Topic 324 Searching Methods Search: Given an integer x and a list L, presorted in increasing order, return the location of x in L, or -1 if x is not in L Algorithms: Linear Search: O(n) Binary Search: O( log n )

CISC 235 Topic 325 Comparison Tree Consider a list of names: Ali, Ania, Chi, Eva, Guy, Iori, Jen, Jui, Omar, Qi, Reni, Tess, Val, Zeya Eva Jen Qi Val OmarGuy Reni Chi ZeyaIoriTessJui Ali Ania

CISC 235 Topic 326 Binary Search Tree Property Let x be a node in a binary search tree. If y is a node in the left subtree of x, then key[ y ]  key[ x ]. If y is a node in the right sub-tree of x, then key[ x ]  key[ y ] Which is not a BST? Why not?

CISC 235 Topic 327 Binary Search Tree Algorithms Find, FindMin, FindMax

CISC 235 Topic 328 Recursive Search Algorithm // Returns node with key k in BST rooted // at node x, or NIL if not found Tree-Search( x, k ) if x = NIL or k = key[ x ] then return x if k < key[ x ] then return Tree-Search( left[ x ], k ) else return Tree-Search( right[ x ], k )

CISC 235 Topic 329 Iterative Search Algorithm // Returns node with key k in BST rooted // at node x, or NIL if not found Iterative-Tree-Search( x, k ) while x  NIL and k  key[ x ] do if k < key[ x ] then x  left[ x ] else x  right[ x ] return x

CISC 235 Topic 330 Algorithm to Find Minimum // Returns minimum in BST // rooted at node x Tree-Minimum( x ) while left[ x ]  NIL do x  left[ x ] return x

CISC 235 Topic 331 Algorithm to Find Maximum // Returns maximum in BST // rooted at node x Tree-Maximum( x ) while right[ x ]  NIL do x  right[ x ] return x

CISC 235 Topic 332 Binary Search Tree: Insertion Insert New node Search for the element as if it were in the tree. When reach a null node, that’s where to insert.

CISC 235 Topic 333 Recursive Insertion Algorithm for BST with no pointers to parents // Inserts node y into BST rooted at node root // Only for BSTs with no pointers to parents BSTInsert ( y, root ) if ( root = NIL ) // have found where to insert then root  y else if key[ y ] < key[ root ] then left[root] = BSTInsert( y, left[ root ] ) else if key[ y ] > key[ root ] then right[root] = BSTInsert( y, right[ root ] ) else y is a duplicate; handle duplicate case return root

CISC 235 Topic 334 Recursive Insert Function for BST with no pointers to parents Node insert ( int x, Node root ) { Node p = new Node( x ); if( root == null ) // empty tree root = p; else if ( x < root.data ) // goes in left sub-tree root.left = insert( x, root.left ); else if ( x > root.data ) // goes in right sub-tree root.right = insert( x, root.right ); else System.out.println( x + ” is already in tree” ); return root; }

CISC 235 Topic 335 Iterative Insertion Algorithm for BST with pointers to parents Tree-Insert ( T, z ) // Inserts node z into BST T y  NIL x  root[ T ] while x  NIL// Find position at which to insert do y  x if key[ z ] < key[ x ] then x  left[ x ] else x  right[ x ] p[ z ]  y // Set parent link of new node if y = NIL // If tree T was empty then root[ T ]  z // New node is root, else else if key[ z ] < key[ y ] // Connect node to parent then left[ y ]  z else right[ y ]  z

CISC 235 Topic 336 BST: Find Successor of a Node Find Successor of If right sub-tree of x is not empty, successor of x is leftmost node in its right sub-tree.

CISC 235 Topic 337 BST: Find Successor of a Node Find Successor of If right sub-tree of x is empty, the successor of x is the lowest ancestor of x whose left child is also an ancestor of x

CISC 235 Topic 338 Find Successor Algorithm for BST with pointers to parents // Returns node in BST that is the successor // of node x, or NIL if no successor Tree-Successor( x ) if right[ x ]  NIL then return Tree-Minimum( right[ x ] ) y  p[ x ] while y  NIL and x = right[ y ] do x  y y  p[ y ] return y

CISC 235 Topic 339 Deletion of Node with Zero or One Child Delete Bypass the node to be deleted by setting its parent to point to its child

CISC 235 Topic 340 Deletion of Node with Two Children Delete Step1: Overwrite the element of the node to be deleted with the minimum element of its right sub-tree

CISC 235 Topic 341 Deletion of Node with Two Children Delete Step2: Recursively delete the node that contains the minimum element of its right sub-tree Delete this node

CISC 235 Topic 342 Iterative Deletion Algorithm for BST with pointers to parents // Deletes node z from BST T Tree-Delete ( T, z ) // Set y to point to node to splice out if left[ z ] = NIL or right[ z ] = NIL then y  z else y  Tree-Successor( z ) // Set x to non-NIL child of y, // or to NIL if y has no children if left[ y ]  NIL then x  left[ y ] else x  right[ y ]

CISC 235 Topic 343 Deletion Algorithm, con. // Splice out node y (next 7 lines) if x  NIL then p[ x ]  p[ y ] if p[ y ] = NIL then root[ T ]  x else if y = left[ p[ y ] ] then left[ p[ y ] ]  x else right[ p[ y ] ]  x // If successor to z was spliced out, copy y’s data to z if y  z then key[ z ]  key[ y ] return y

CISC 235 Topic 344 Iterative Deletion for BST with no pointers to parents Node delete ( Node z ) // 0 or 1 child: bypass node if z has no left child z  right[ z ] else if z has no right child z  left[ z ] else // 2 children: replace z with min of right sub-tree & delete min Node previous  z Node tmp  right[ z ] Repeatedly set previous to tmp and tmp to left[ tmp ] until tmp is pointing to the minimum, with previous pointing to its parent Copy data value of tmp (minimum) to z Delete tmp (minimum) by setting the child of previous that points to tmp to instead point to right[ tmp ] return z

CISC 235 Topic 345 Binary Search Tree: Complexity Search, insertion, and deletion in a binary search tree are all O(h), where h is the height of the tree. What does this imply about the complexity in terms of n ?