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

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.
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 Chapter 8.
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.
Trees, Binary Trees, and Binary Search Trees COMP171.
Trees Chapter 8. Chapter 8: Trees2 Chapter Objectives To learn how to use a tree to represent a hierarchical organization of information To learn how.
Trees Chapter 8. Chapter 8: Trees2 Chapter Objectives To learn how to use a tree to represent a hierarchical organization of information To learn how.
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.
10. Binary Trees A. Introduction: Searching a linked list.
Bioinformatics Programming 1 EE, NCKU Tien-Hao Chang (Darby Chang)
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.
Trees. Tree Terminology Chapter 8: Trees 2 A tree consists of a collection of elements or nodes, with each node linked to its successors The node at the.
Advanced Algorithms Analysis and Design Lecture 8 (Continue Lecture 7…..) Elementry Data Structures By Engr Huma Ayub Vine.
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.
Searching. Motivation Find parts for a system Find an address for name Find a criminal –fingerprint/DNA match Locate all employees in a dept. based on.
CS Data Structures Chapter 5 Trees. Chapter 5 Trees: Outline  Introduction  Representation Of Trees  Binary Trees  Binary Tree Traversals 
Searching: Binary Trees and Hash Tables CHAPTER 12 6/4/15 Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education,
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.
Trees Chapter 8. Chapter 8: Trees2 Chapter Objectives To learn how to use a tree to represent a hierarchical organization of information To learn how.
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 ),
Binary Trees 2 Overview Trees. Terminology. Traversal of Binary Trees. Expression Trees. Binary Search Trees.
Prof. Amr Goneid, AUC1 Analysis & Design of Algorithms (CSCE 321) Prof. Amr Goneid Department of Computer Science, AUC Part R2. Binary Search Trees.
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.
Prof. Amr Goneid, AUC1 CSCE 210 Data Structures and Algorithms Prof. Amr Goneid AUC Part 4. Trees.
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.
Data Structure Chapter# 5 Tree Course Instructor AMEER JAMAL SHAH.
1 10. Binary Trees Read Sec A. Introduction: Searching a linked list. 1. Linear Search /* Linear search a list for a particular item */ 1. Set.
1 Storing Hierarchical Information Lists, Stacks, and Queues represent linear sequences Data often contain hierarchical relationships that cannot be expressed.
Binary Trees Chapter 10. Introduction Previous chapter considered linked lists –nodes connected by two or more links We seek to organize data in a linked.
Trees 2: Section 4.2 and 4.3 Binary trees. Binary Trees Definition: A binary tree is a rooted tree in which no vertex has more than two children
Tree Traversals, TreeSort 20 February Expression Tree Leaves are operands Interior nodes are operators A binary tree to represent (A - B) + C.
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.
Week 10 - Friday.  What did we talk about last time?  Graph representations  Adjacency matrix  Adjacency lists  Depth first search.
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.
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.
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 CSE 326: Data Structures Trees Lecture 6: Friday, Jan 17, 2003.
1 the BSTree class  BSTreeNode has same structure as binary tree nodes  elements stored in a BSTree are a key- value pair  must be a class (or a struct)
18-1 Chapter 18 Binary Trees Data Structures and Design in Java © Rick Mercer.
1 Trees. 2 Trees Trees. Binary Trees Tree Traversal.
Final Exam Review COP4530.
Trees Chapter 15.
CSCE 210 Data Structures and Algorithms
Binary Search Tree (BST)
Lecture 18. Basics and types of Trees
abstract containers sequence/linear (1 to 1) hierarchical (1 to many)
Binary Trees, Binary Search Trees
Chapter 22 : Binary Trees, AVL Trees, and Priority Queues
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.
Final Exam Review COP4530.
Trees Lecture 9 CS2110 – Fall 2009.
Binary Trees, Binary Search Trees
Binary Trees, Binary Search Trees
Data Structures Using C++ 2E
Introduction to Trees Chapter 6 Objectives
Presentation transcript:

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

2 trees  hierarchical organization  each item has 1 predecessor and 0 or more successors  one item (root) has 0 predecessors  general tree - no limit on number of successors  binary tree - 2 successors (left and right)  binary search tree is one use of a binary tree data structure (will deal with later)

3 tree terminology There is a unique path from the root to each node. Root is a level 0, child is at level(parent) + 1. Depth/height of a tree is the length of the longest path. level

4 trees are recursive Each node is the root of a subtree. Many tree processing algorithms are best written recursively.

5 binary tree  Each node has two successors  one called the left child  one called the right child  left child and/or right child may be empty A binary tree is either - empty or - consists of a root and two binary trees, one called the left subtree and one called the right subtree

6 binary tree density (shape)  a binary tree of depth n is complete iff  levels 1.. n have all possible nodes filled-in  level 1: 1  level 2: 2  level 3: 4  level n: ?  nodes at level n occupy the leftmost positions  max nodes level at n: 2 n-1  max nodes in binary tree of depth n: 2 n -1  depth of binary tree with k nodes: >floor[log 2 k]  depth of binary tree with k nodes: <ceil[log 2 k]  longest path is O(log 2 k)

7 representing a binary tree  have to store items and relationships (predecessor and successors information)  array representation  each item has a position number (0.. n-1)  use the position number as an array index  O(1) access to parent and children of item at position i  wastes space unless binary tree is complete  linked representation  each item stored in a node which contains:  the item  a pointer to the left subtree  a pointer to the right subtree

8 array representation  each item has a position number  root is at position 0  root's left child is at position 1  root's right child is at position 2  in general:  left child of i is at 2i + 1  right child of i is at 2i + 2  parent of i is at (i-1)/ works well if n is known in advance and there are no "missing" nodes

9 linked representation class binTreeNode { public: T item; binTreeNode * left; binTreeNode * right; binTreeNode (const T & value) { item = value; left = NULL; right = NULL; } }; binTreeNode * root; root left child of *p? right child of *p? parent of *p? p

10 why is a binary tree so useful?  for storing a collection of values that has a binary hierarchical organization  arithmetic expressions  boolean logic expressions  Morse or Huffman code trees  data structure for a binary search tree  general tree can be stored using a binary tree data structure

11 an expression tree  an arithmetic expression consists of an operator and two operands (either of which may be an arithmetic expression)  some simplifications  only binary operators (+, *, /, -)  only single digit, non- negative integer operands * operands are stored in leaf nodes operators are stored in branch nodes

12 traversing a binary tree  what does it mean to traverse a container?  "visit" each item once in some order  many operations on a collection of items involve traversing the container in which they are stored  displaying all items  making a copy of a container  linear collections (usually) traversed from first to last  last to first is an alternative traversal order

13 binary tree traversals  order in which to "visit" the items?  some common orders - visit an item  before its children (preorder)  after its children (postorder)  between its children (inorder)  level by level (level order)  by convention go left before right

14 traversing an expression tree 4 * + * preorder (1 st touch) * * inorder (2 nd touch) * * 2 postorder (last touch) * *

15 expression tree traversal traverse (ExprTree) { if (root of ExprTree holds a digit) //is a leaf node visit (root); else // root holds an operand { visit (root) traverse (ExprTree's left subtree) traverse (ExprTree's right subtree) }

16 expression tree operations  use "recursive partners"  allow recursive calls to deal with a subtree (identifed by a pointer to its root node)  build uses a preorder traversal  copy uses a preorder traversal  postfix uses a postorder traversal  clear uses a postorder traversal  what kind of traversal does infix need?  what kind of traversal does evaluate need?

17 Associative Containers fast searching

18 STL Containers  Sequence containers  vector  list  deque  Adapters  stack  queue  priority_queue  Associative containers  set  map  multiset  multimap unique keys non unique keys keys only set multiset key, value pairs map multimap

19 map containers  allow storing a collection of items each of which has a key which uniquely identifies it  student records (social security number)  books in a library (ISBN)  identifiers appearing in a function (name)  has operations to  insert an item (key-value pair)  delete the item with a given key  retrieve the item with a given key  operations based on value, not position  searching for a key is the critical operation

20 some possible data structures  array (or STL vector)  unordered  ordered by keys  linked list (or STL list)  unordered  ordered by keys  binary search tree  balanced search trees  hash table

21 comparative performance data structure Retrieve Insert Delete unordered array ordered array unordered linked list ordered linked list O(n) O(1) O(n) O(log 2 n) O(n) O(n) O(n) O(1) O(n) O(n) O(n) O(n) search trees and hash table both have better performance

22 binary search tree  each item is stored in a node of a binary tree  each node is the root of a binary tree with the BST property  all items stored in its left subtree have smaller key values  all items stored in its right subtree have larger key values

23 a binary search tree root This is NOT a Heap!! No node

24 searching for root

25 searching for root

26 basic BST search algorithm void search (bstree, searchKey) { if (bstree is empty) //base case: item not found // take needed action else if (key in bstree's root == search Key) // base case: item found // take needed action else if (searchKey < key in bstree's root ) search (leftSubtree, searchKey); else search (rightSubtree, searchKey); }

27 traversing a binary search tree  can use any of the binary tree traversal orders – preorder, inorder, postorder  base case is reaching an empty tree  inorder traversal visits the elements in order of their key values  how would you visit the elements in descending order of key values?

28 the BSTree class  BSTreeNode has same structure as binary tree nodes  elements stored in a BSTree are a key- value pair  must be a class (or a struct) which has  a data member for the value  a data member for the key  a method with the signature: KT key( ) const; where KT is the type of the key

29 an example struct treeItem { int id; // key string data; // value int key( ) const { return id; } }; BSTree myBSTree;

30 deletion cases  item to be deleted is in a leaf node  pointer to its node (in parent) must be changed to NULL  item to be deleted is in a node with one empty subtree  pointer to its node (in parent) must be changed to the non-empty subtree  item to be deleted is in a node with two non-empty subtrees

31 the easy cases

32 the “hard” case

33 the “hard” case replace with smallest in right subtree (inorder successor) replace with largest in left subtree (inorder predecessor)

34 big Oh of BST operations  measured by length of the search path  depends on the height of the BST  height determined by order of insertion  height of a BST containing n items is  minimum: floor (log 2 n)  maximum: n - 1  average: ?

35 faster searching  "balanced" search trees guarantee O(log 2 n) search path by controlling height of the search tree  AVL tree  tree  red-black tree (used by STL associative container classes)  hash table allows for O(1) search performance  search time does not increase as n increases