Your SlideShare is downloading. ×
0
Trees Chapter 5 Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc....
Binary Search Tree <ul><li>Consider the following ordered list of integers </li></ul><ul><li>Examine middle element </li><...
Binary Search Tree <ul><li>Redraw the previous structure so that it has a treelike shape – a  binary tree </li></ul>Nyhoff...
Trees <ul><li>A data structure which consists of  </li></ul><ul><ul><li>a finite set of elements called  nodes  or vertice...
Trees <ul><li>Tree terminology </li></ul>Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 200...
Binary Trees <ul><li>Each node has at most two children </li></ul><ul><li>Useful in modeling processes where </li></ul><ul...
Array Representation of Binary Trees <ul><li>Store the  i th  node in the  i th  location of the array </li></ul>Nyhoff, A...
Array Representation of Binary Trees <ul><li>Works OK for complete trees, not for sparse trees </li></ul>Nyhoff, ADTs, Dat...
Linked  Representation of Binary Trees <ul><li>Uses space more efficiently </li></ul><ul><li>Provides additional flexibili...
Linked  Representation of Binary Trees <ul><li>Example </li></ul>Nyhoff, ADTs, Data Structures and Problem Solving with C+...
Binary Trees as Recursive Data Structures <ul><li>A binary tree is either empty … </li></ul><ul><li>or </li></ul><ul><li>C...
Tree Traversal is Recursive <ul><li>If the binary tree is empty then do nothing </li></ul><ul><li>Else  N: Visit the root,...
ADT Binary Search Tree (BST) <ul><li>Collection of Data Elements </li></ul><ul><ul><li>binary tree </li></ul></ul><ul><ul>...
ADT Binary Search Tree (BST) <ul><li>Basic operations  </li></ul><ul><ul><li>Insert a new item in the BST </li></ul></ul><...
BST Searches <ul><li>Search begins at root </li></ul><ul><ul><li>If that is desired item, done </li></ul></ul><ul><li>If i...
Inserting into a BST <ul><li>Insert function </li></ul><ul><ul><li>Uses modified version of search to locate insertion loc...
Recursive Deletion <ul><li>Three possible cases to delete a node,  x ,  from a BST </li></ul><ul><li>1. The node,  x,  is ...
Recursive Deletion <ul><li>2. The node,  x  has one child </li></ul>Nyhoff, ADTs, Data Structures and Problem Solving with...
Recursive Deletion <ul><li>x  has two children </li></ul>Nyhoff, ADTs, Data Structures and Problem Solving with C++, Secon...
BST Class Template <ul><li>View complete binary search tree template,  Fig. 12.7 </li></ul><ul><li>View test program for B...
<ul><li>Tree can be balanced </li></ul><ul><ul><li>each node except leaves has exactly 2 child nodes </li></ul></ul>Nyhoff...
<ul><li>Trees can be unbalanced </li></ul><ul><ul><li>not all nodes have exactly 2 child nodes </li></ul></ul>Nyhoff, ADTs...
<ul><li>Trees can be totally lopsided </li></ul><ul><ul><li>Suppose each node has a right child only </li></ul></ul><ul><u...
Hash Tables <ul><li>Recall order of magnitude of searches </li></ul><ul><ul><li>Linear search O(n) </li></ul></ul><ul><ul>...
Non Binary Trees <ul><li>Some applications require more than two children per node </li></ul><ul><ul><li>Genealogical tree...
Upcoming SlideShare
Loading in...5
×

Tree

1,613

Published on

Published in: Education
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total Views
1,613
On Slideshare
0
From Embeds
0
Number of Embeds
3
Actions
Shares
0
Downloads
56
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Transcript of "Tree"

  1. 1. Trees Chapter 5 Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved. 0-13-140909-3
  2. 2. Binary Search Tree <ul><li>Consider the following ordered list of integers </li></ul><ul><li>Examine middle element </li></ul><ul><li>Examine left, right sublist (maintain pointers) </li></ul><ul><li>Examine left, right sublists </li></ul>Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved. 0-13-140909-3 80 66 62 49 35 28 13
  3. 3. Binary Search Tree <ul><li>Redraw the previous structure so that it has a treelike shape – a binary tree </li></ul>Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved. 0-13-140909-3
  4. 4. Trees <ul><li>A data structure which consists of </li></ul><ul><ul><li>a finite set of elements called nodes or vertices </li></ul></ul><ul><ul><li>a finite set of directed arcs which connect the nodes </li></ul></ul>Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved. 0-13-140909-3
  5. 5. Trees <ul><li>Tree terminology </li></ul>Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved. 0-13-140909-3 Root node Leaf nodes <ul><li>Children of the parent (3) </li></ul>
  6. 6. Binary Trees <ul><li>Each node has at most two children </li></ul><ul><li>Useful in modeling processes where </li></ul><ul><ul><li>a comparison or experiment has exactly two possible outcomes </li></ul></ul><ul><ul><li>the test is performed repeatedly </li></ul></ul><ul><li>Example </li></ul><ul><ul><li>multiple coin tosses </li></ul></ul><ul><ul><li>encoding/decoding messages in dots and dashes such as Mores code </li></ul></ul>Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved. 0-13-140909-3
  7. 7. Array Representation of Binary Trees <ul><li>Store the i th node in the i th location of the array </li></ul>Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved. 0-13-140909-3
  8. 8. Array Representation of Binary Trees <ul><li>Works OK for complete trees, not for sparse trees </li></ul>Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved. 0-13-140909-3
  9. 9. Linked Representation of Binary Trees <ul><li>Uses space more efficiently </li></ul><ul><li>Provides additional flexibility </li></ul><ul><li>Each node has two links </li></ul><ul><ul><li>one to the left child of the node </li></ul></ul><ul><ul><li>one to the right child of the node </li></ul></ul><ul><ul><li>if no child node exists for a node, the link is set to NULL </li></ul></ul>Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved. 0-13-140909-3
  10. 10. Linked Representation of Binary Trees <ul><li>Example </li></ul>Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved. 0-13-140909-3
  11. 11. Binary Trees as Recursive Data Structures <ul><li>A binary tree is either empty … </li></ul><ul><li>or </li></ul><ul><li>Consists of </li></ul><ul><ul><li>a node called the root </li></ul></ul><ul><ul><li>root has pointers to two disjoint binary (sub) trees called … </li></ul></ul><ul><ul><ul><li>right (sub) tree </li></ul></ul></ul><ul><ul><ul><li>left (sub) tree </li></ul></ul></ul>Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved. 0-13-140909-3 Anchor Which is either empty … or … Which is either empty … or … Inductive step
  12. 12. Tree Traversal is Recursive <ul><li>If the binary tree is empty then do nothing </li></ul><ul><li>Else N: Visit the root, process data L: Traverse the left subtree R: Traverse the right subtree </li></ul>Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved. 0-13-140909-3 The &quot;anchor&quot; The inductive step
  13. 13. ADT Binary Search Tree (BST) <ul><li>Collection of Data Elements </li></ul><ul><ul><li>binary tree </li></ul></ul><ul><ul><li>each node x, </li></ul></ul><ul><ul><ul><li>value in left child of x value in x in right child of x </li></ul></ul></ul><ul><li>Basic operations </li></ul><ul><ul><li>Construct an empty BST </li></ul></ul><ul><ul><li>Determine if BST is empty </li></ul></ul><ul><ul><li>Search BST for given item </li></ul></ul>Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved. 0-13-140909-3
  14. 14. ADT Binary Search Tree (BST) <ul><li>Basic operations </li></ul><ul><ul><li>Insert a new item in the BST </li></ul></ul><ul><ul><li>Delete an item from the BST </li></ul></ul><ul><ul><li>Traverse the BST </li></ul></ul><ul><ul><ul><li>Visit each node exactly once </li></ul></ul></ul><ul><ul><ul><li>The inorder traversal must visit the values in the nodes in ascending order </li></ul></ul></ul>Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved. 0-13-140909-3
  15. 15. BST Searches <ul><li>Search begins at root </li></ul><ul><ul><li>If that is desired item, done </li></ul></ul><ul><li>If item is less , move down left subtree </li></ul><ul><li>If item searched for is greater , move down right subtree </li></ul><ul><li>If item is not found, we will run into an empty subtree </li></ul><ul><li>View search() </li></ul>Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved. 0-13-140909-3
  16. 16. Inserting into a BST <ul><li>Insert function </li></ul><ul><ul><li>Uses modified version of search to locate insertion location or already existing item </li></ul></ul><ul><ul><li>Pointer parent trails search pointer locptr , keeps track of parent node </li></ul></ul><ul><ul><li>Thus new node can be attached to BST in proper place </li></ul></ul><ul><li>View insert() function </li></ul>Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved. 0-13-140909-3 R
  17. 17. Recursive Deletion <ul><li>Three possible cases to delete a node, x , from a BST </li></ul><ul><li>1. The node, x, is a leaf </li></ul>Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved. 0-13-140909-3
  18. 18. Recursive Deletion <ul><li>2. The node, x has one child </li></ul>Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved. 0-13-140909-3
  19. 19. Recursive Deletion <ul><li>x has two children </li></ul>Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved. 0-13-140909-3 View remove() function Replace contents of x with inorder successor K Delete node pointed to by xSucc as described for cases 1 and 2
  20. 20. BST Class Template <ul><li>View complete binary search tree template, Fig. 12.7 </li></ul><ul><li>View test program for BST, Fig. 12.8 </li></ul>Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved. 0-13-140909-3
  21. 21. <ul><li>Tree can be balanced </li></ul><ul><ul><li>each node except leaves has exactly 2 child nodes </li></ul></ul>Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved. 0-13-140909-3
  22. 22. <ul><li>Trees can be unbalanced </li></ul><ul><ul><li>not all nodes have exactly 2 child nodes </li></ul></ul>Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved. 0-13-140909-3
  23. 23. <ul><li>Trees can be totally lopsided </li></ul><ul><ul><li>Suppose each node has a right child only </li></ul></ul><ul><ul><li>Degenerates into a linked list </li></ul></ul>Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved. 0-13-140909-3 Processing time affected by &quot;shape&quot; of tree
  24. 24. Hash Tables <ul><li>Recall order of magnitude of searches </li></ul><ul><ul><li>Linear search O(n) </li></ul></ul><ul><ul><li>Binary search O(log 2 n) </li></ul></ul><ul><ul><li>Balanced binary tree search O(log 2 n) </li></ul></ul><ul><ul><li>Unbalanced binary tree can degrade to O(n) </li></ul></ul>Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved. 0-13-140909-3
  25. 25. Non Binary Trees <ul><li>Some applications require more than two children per node </li></ul><ul><ul><li>Genealogical tree </li></ul></ul><ul><ul><li>Game tree </li></ul></ul>Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved. 0-13-140909-3
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×