TDDB56 DALGOPT-D TDDB57 DALG-C Lecture 5 Jan Maluszynski - HT 20065.1 TDDB56 – TDDB57 Lecture 5 Trees Terminology, Implementation Binary Search Trees.

Slides:



Advertisements
Similar presentations
1 abstract containers hierarchical (1 to many) graph (many to many) first ith last sequence/linear (1 to 1) set.
Advertisements

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, Binary Search Trees CMPS 2133 Spring 2008.
Binary Trees, Binary Search Trees COMP171 Fall 2006.
TTIT33 Algorithms and Optimization – Lecture 5 Algorithms Jan Maluszynski - HT TTIT33 – Algorithms and optimization Lecture 5 Algorithms ADT Map,
Data Structures: Trees i206 Fall 2010 John Chuang Some slides adapted from Marti Hearst, Brian Hayes, or Glenn Brookshear.
Fall 2007CS 2251 Trees Chapter 8. Fall 2007CS 2252 Chapter Objectives To learn how to use a tree to represent a hierarchical organization of information.
1 Trees. 2 Outline –Tree Structures –Tree Node Level and Path Length –Binary Tree Definition –Binary Tree Nodes –Binary Search Trees.
TDDB56 DALGOPT-D TDDB57 DALG-C Lecture 6 Jan Maluszynski - HT TDDB56 TDDB57 Lecture 6 Search Trees: AVL-Trees, Multi-way Search Trees, B-Trees.
Lec 15 April 9 Topics: l binary Trees l expression trees Binary Search Trees (Chapter 5 of text)
TTIT33 Algorithms and Optimization – DALG Lecture 6 Jan Maluszynski - HT TTIT33- Algorithms and optimization Algorithms Lecture 5 Balanced Search.
B + -Trees (Part 1). Motivation AVL tree with N nodes is an excellent data structure for searching, indexing, etc. –The Big-Oh analysis shows most operations.
© 2004 Goodrich, Tamassia (2,4) Trees
TDDB56 DALGOPT-D - TDDB57 DALG-C Lecture 7 Jan Maluszynski - HT TDDB56 – DALGOPT-D Algorithms and optimization Lecture 7 Splay Trees. Priority.
1 abstract containers hierarchical (1 to many) graph (many to many) first ith last sequence/linear (1 to 1) set.
Data Structures Using C++ 2E Chapter 11 Binary Trees and B-Trees.
Binary Trees Chapter 6.
Version TCSS 342, Winter 2006 Lecture Notes Trees Binary Trees Binary Search Trees.
CSCE 3110 Data Structures & Algorithm Analysis Binary Search Trees Reading: Chap. 4 (4.3) Weiss.
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.
COSC2007 Data Structures II
Advanced Algorithms Analysis and Design Lecture 8 (Continue Lecture 7…..) Elementry Data Structures By Engr Huma Ayub Vine.
Chapter 19: Binary Trees. Objectives In this chapter, you will: – Learn about binary trees – Explore various binary tree traversal algorithms – Organize.
CS Data Structures Chapter 5 Trees. Chapter 5 Trees: Outline  Introduction  Representation Of Trees  Binary Trees  Binary Tree Traversals 
Spring 2010CS 2251 Trees Chapter 6. Spring 2010CS 2252 Chapter Objectives Learn to use a tree to represent a hierarchical organization of information.
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,
COSC 1030 Lecture 9 Binary Trees. Topics Basic Concept and Terminology Applications of Binary Tree Complete Tree Representation Traversing Binary Trees.
Binary Trees, Binary Search Trees RIZWAN REHMAN CENTRE FOR COMPUTER STUDIES DIBRUGARH UNIVERSITY.
B + -Trees. Motivation An AVL tree with N nodes is an excellent data structure for searching, indexing, etc. The Big-Oh analysis shows that most operations.
Compiled by: Dr. Mohammad Omar Alhawarat
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
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,
1 Searching the dictionary ADT binary search binary search trees.
Review 1 Queue Operations on Queues A Dequeue Operation An Enqueue Operation Array Implementation Link list Implementation Examples.
© 2004 Goodrich, Tamassia Trees
Chapter 4: Trees Part I: General Tree Concepts Mark Allen Weiss: Data Structures and Algorithm Analysis in Java.
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 (BSTs) 18 February Binary Search Tree (BST) An important special kind of binary tree is the BST Each node stores some information.
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.
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.
COSC 2007 Data Structures II Chapter 13 Advanced Implementation of Tables I.
24 January Trees CSE 2011 Winter Trees Linear access time of linked lists is prohibitive  Does there exist any simple data structure for.
CS 367 Introduction to Data Structures Lecture 8.
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 What is a Tree? Tree terminology Why trees? What is a general tree? Implementing trees Binary trees Binary tree implementation Application of Binary.
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.
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.
1 CMSC 341 Introduction to Trees Textbook sections:
© 2004 Goodrich, Tamassia BINARY SEARCH TREES Binary Search Trees   
1 Trees. 2 Trees Trees. Binary Trees Tree Traversal.
Tree Representation and Terminology Binary Trees Binary Search Trees Pointer-Based Representation of a Binary Tree Array-Based Representation of a Binary.
What is a Tree? Formally, we define a tree T as a set of nodes storing elements such that the nodes have a parent-child relationship, that satisfies the.
Data Structures and Design in Java © Rick Mercer
Trees Chapter 15.
Chapter 10 Search Trees 10.1 Binary Search Trees Search Trees
Binary Search Tree Chapter 10.
Lecture 22 Binary Search Trees Chapter 10 of textbook
CMSC 341 Introduction to Trees.
Lecture 18. Basics and types of Trees
Binary Trees, Binary Search Trees
Binary Search Trees < > =
CMSC 202 Trees.
Binary Trees, Binary Search Trees
Binary Trees, Binary Search Trees
NATURE VIEW OF A TREE leaves branches root. NATURE VIEW OF A TREE leaves branches root.
Cs212: Data Structures Lecture 7: Tree_Part1
Presentation transcript:

TDDB56 DALGOPT-D TDDB57 DALG-C Lecture 5 Jan Maluszynski - HT TDDB56 – TDDB57 Lecture 5 Trees Terminology, Implementation Binary Search Trees

TDDB56 DALGOPT-D TDDB57 DALG-C Lecture 5 Jan Maluszynski - HT Content: Trees: basic terminology [Goodrich & Tamassia 7.1.1] ADT Tree [Goodrich & Tamassia 7.1.2] Binary Trees, representation [Goodrich & Tamassia 7.3] Binary search trees [Goodrich & Tamassia 10.1]

TDDB56 DALGOPT-D TDDB57 DALG-C Lecture 5 Jan Maluszynski - HT Why Trees? Trees appear naturally in many situations… File system in UNIX Hierarchical classification systems Decision trees (e.g. binary search analysis Lecture 4) Hierarchical organization of… –Organizations: department, division, group, … –Documents: book, chapter, section, … –XML documents Representing order or priority… –expression tree for e.g * 3!

TDDB56 DALGOPT-D TDDB57 DALG-C Lecture 5 Jan Maluszynski - HT Trees: Basic terminology Tree = set of nodes and edges, T = (V, E). Nodes v  V store data items in a parent-child relationship. A parent-child relation between nodes u and v is shown as a directed edge ( u,v)  E, where the direction is from u to v. E  V  V Each node has at most one parent node; may have many siblings There is exactly one node that has no parent – the root node. The degree of a node v is the number of children A node with 0 children is a leaf node or external node. The other nodes are internal Example: ( ( a, b, c, d ), ( (a,b),(a,c),(c,d) ) ) a bc d

TDDB56 DALGOPT-D TDDB57 DALG-C Lecture 5 Jan Maluszynski - HT Trees: Basic terminology (cont.) A path is a sequence of nodes (v 1, v 2, …, v k ) such that ( v i v i+1 ) is an edge for i=1,…k-1 A node n is an ancestor of a node v iff there exists a path from n to v in T A node n is an descendent of a node v iff there exists a path from v to n in T The depth d(v) of a node v is the length of the path from the root to v The height h(v) of a node v is the length of the longest path from v to a descendent of v The height h(T) of a tree T is the height of the root.

TDDB56 DALGOPT-D TDDB57 DALG-C Lecture 5 Jan Maluszynski - HT Special kinds of trees Ordered tree: linear order among the children of each node Binary tree: ordered tree with degree  2 for each node  left child, right child… Empty binary tree (  ): binary tree with no nodes Full binary tree: nonempty; degree is either 0 or 2 for each node Fact: number of leaves = 1 + number of interior nodes (proof by induction) Perfect binary tree: full, all leaves have the same depth Fact: number of leaves = 2 h for a perfect binary tree of height h (proof by induction on h ) Complete binary tree: approximation to a perfect tree for 2 h  n <2 h+1 –1 Important and useful property!

TDDB56 DALGOPT-D TDDB57 DALG-C Lecture 5 Jan Maluszynski - HT ADT Tree Operations on a single tree node v of a tree T parent ( v) returns parent of v, error  if v root children( v) returns set of children of v firstChild( v) returns first child of v, or  if v leaf rightSibling( v) returns right sibling of v, or  if none leftSibling( v) returns left sibling of v, or  if none isLeaf( v) returns true iff v is a leaf (an external node) isInternal( v) returns true iff v is a non-leaf node isRoot( v) returns true iff v is the root depth( v) returns depth of v in T height( v) returns height of v in T

TDDB56 DALGOPT-D TDDB57 DALG-C Lecture 5 Jan Maluszynski - HT ADT Tree (cont.) Operations on entire tree T : size( ) returns number of nodes of T root( ) returns root node of T height( ) returns the height of T In addition for a binary tree: left( v) returns the left child of v, or error right( v) returns the right child of v, or error hasLeft( v) test if v has the left child hasRight( v) test if v has the right child

TDDB56 DALGOPT-D TDDB57 DALG-C Lecture 5 Jan Maluszynski - HT Tree representation 1: pointers… Type Tnode denotes a pointer to a structure storing node information: record node_record nchilds: integer child: table [1..nchilds] info: infotype For binary trees: 2 pointers per node, LC and RC Alternatively, the pointers to a node’s children can be stored in a linked list. If required, a “backward” pointer to the parent node can be added.

TDDB56 DALGOPT-D TDDB57 DALG-C Lecture 5 Jan Maluszynski - HT Tree representation 2: Sequential memory For a complete binary tree holds: There is exactly one complete binary tree with n nodes. Implicit representation of edges: Numbering of nodes  index positions [G&T, p.296].: Numbering starts at 1- root numbered 1; extends to binary trees which are not complete

TDDB56 DALGOPT-D TDDB57 DALG-C Lecture 5 Jan Maluszynski - HT : Sequential memory (cont.) Use a table [0.. n -1] leftChild( i ) = 2i+1 (return  if 2i+1  n ) rightChild( i ) = 2i+2 (return  if 2i+1  n ) isLeaf( i ) = i n leftSibling( i ) = i-1 (return  if i=0 or odd(i) ) rightSibling( i ) = i+1 (return  if i=n-1 or even(i) ) parent( i ) =  ( i-1)/2  (return  if i =0 ) isRoot( i ) = ( i = 0)

TDDB56 DALGOPT-D TDDB57 DALG-C Lecture 5 Jan Maluszynski - HT Tree Traversals Regard a tree T as a building: nodes as rooms, edges as doors, root as entry How to explore an unknown (acyclic) labyrinth and get out again? Proceed by always keeping a wall to the right! Generic tree traversal routine: procedure visit(node v ) for all u  children(v) do visit(u) Call visit(Root( T )) and each node in T will be visited exactly once!

TDDB56 DALGOPT-D TDDB57 DALG-C Lecture 5 Jan Maluszynski - HT Tree Traversals (3 of 4) procedure preorder_visit(node v ) do_something( v ){before any children} for all u  children(v) do preorder_visit( u ) procedure postorder_visit(node v ) for all u  children(v) do postorder_visit( u ) do_something( v ){after all children} procedure inorder_visit(node v ) {binary trees only!} inorder_visit( leftChild(v) ) do_something( v ){after all children} inorder_visit( rightChild(v) )

TDDB56 DALGOPT-D TDDB57 DALG-C Lecture 5 Jan Maluszynski - HT Tree Traversals (#4) procedure level_order_visit(node v ) Q  mkEmptyQueue() enqueue( v, Q ) while not isEmpty( Q ) do v  dequeue( Q ) do_something( v ) for all u  children(v) do enqueue( v, Q )

TDDB56 DALGOPT-D TDDB57 DALG-C Lecture 5 Jan Maluszynski - HT Binary Search Tree ( G&T 10.1) A binary search tree (BST) is a binary tree such that: Information associated with a node includes a key,  linear ordering of nodes determined by keys. The key of each node is: greater than (or equal) the keys of all left descendents, and smaller than (or equal) the keys of all right descendents. The leaves do not store any information Variant in [Goodrich &Tamassia]

TDDB56 DALGOPT-D TDDB57 DALG-C Lecture 5 Jan Maluszynski - HT ADT Map as Binary Search Tree… lookUp( k,v) : comparison controlled traversal if key ( v) = k then return k else if k < key(v) then LookUp (k, leftChild(v)) fails if no leftChild else LookUp (k, rightChild(v)) fails if no rightChild worst-case: height(T)+1 comparisons insert( k, x) : add ( k, x) as new leaf on lookUp failure or update the node on lookUp success worst-case: height(T)+1 comparisons remove( k) : lookUp, then… if v is a leaf, remove v if v has one child u, replace v by u if v has two children, replace v by its inorder successor (alternatively: by its inorder predecessor) worst-case: height(T)+1 comparisons

TDDB56 DALGOPT-D TDDB57 DALG-C Lecture 5 Jan Maluszynski - HT BST:s are not unique Same data may generate different BST insert : 1,2,4,5,8 insert: 5, 2, 1, 4, 8

TDDB56 DALGOPT-D TDDB57 DALG-C Lecture 5 Jan Maluszynski - HT Successful LookUp: Worst and Average case Worst Case BST BST degenerated to a linear sequence expected number of comparisons is ( n+1)/ 2 Balanced BST The depths of leaves differ by at most 1  O(log 2 n) comparisons.

TDDB56 DALGOPT-D TDDB57 DALG-C Lecture 5 Jan Maluszynski - HT Thus – Let’s keep ‘em balanced! Some commonly used balanced trees (to be discussed): AVL Trees (2,3) Trees ( or (2,4) Trees), (a,b)-Trees, …Red-Black Trees, B-Trees Splay Trees

TDDB56 DALGOPT-D TDDB57 DALG-C Lecture 5 Jan Maluszynski - HT Exercise Discuss implementation of ADT Dictionary as [G&T] variant of BST Insert(k,v) - no update of entries with the same key findAll(k) – k may occur in many nodes