1 / 62

C++ Programming: Program Design Including Data Structures, Fourth Edition

C++ Programming: Program Design Including Data Structures, Fourth Edition. Chapter 20: Binary Trees. Objectives. In this chapter, you will: Learn about binary trees Explore various binary tree traversal algorithms Learn how to organize data in a binary search tree

aric
Télécharger la présentation

C++ Programming: Program Design Including Data Structures, Fourth Edition

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 20: Binary Trees

  2. Objectives In this chapter, you will: • Learn about binary trees • Explore various binary tree traversal algorithms • Learn how to organize data in a binary search tree • Learn how to insert and delete items in a binary search tree • Explore nonrecursive binary tree traversal algorithms C++ Programming: Program Design Including Data Structures, Fourth Edition

  3. Binary Trees C++ Programming: Program Design Including Data Structures, Fourth Edition

  4. Binary Trees (continued) Root node, and Parent of B and C Left child of A Right child of A Directed edge, directed branch, or branch Node Empty subtree (F’s right subtree) C++ Programming: Program Design Including Data Structures, Fourth Edition

  5. Binary Trees (continued) • Every node has at most two children • A node: • Stores its own information • Keeps track of its left subtree and right subtree • lLink and rLink pointers C++ Programming: Program Design Including Data Structures, Fourth Edition

  6. Binary Trees (continued) • A pointer to the root node of the binary tree is stored outside the tree in a pointer variable C++ Programming: Program Design Including Data Structures, Fourth Edition

  7. Binary Trees (continued) • Leaf: node that has no left and right children • U is parent of V if there’s a branch from U to V • There’s a unique path from root to every node • Length of a path: number of branches on path • Level of a node: number of branches on the path from the root to the node • The level of the root node of a binary tree is 0 • Height of a binary tree: number of nodes on the longest path from the root to a leaf C++ Programming: Program Design Including Data Structures, Fourth Edition

  8. A is the parent of B and C ABDG is a path (of length 3) from node A to node G A leaf • The longest path from root to a leaf is ABDGI • The number of nodes on this path is 5  the height of the tree is 5

  9. Binary Trees (continued) • How can we calculate the height of a binary tree? • This is a recursive algorithm: C++ Programming: Program Design Including Data Structures, Fourth Edition

  10. Copy Tree C++ Programming: Program Design Including Data Structures, Fourth Edition

  11. Binary Tree Traversal • Inorder traversal • Traverse the left subtree • Visit the node • Traverse the right subtree • Preorder traversal • Visit the node • Traverse the left subtree • Traverse the right subtree C++ Programming: Program Design Including Data Structures, Fourth Edition

  12. Binary Tree Traversal (continued) • Postorder traversal • Traverse the left subtree • Traverse the right subtree • Visit the node C++ Programming: Program Design Including Data Structures, Fourth Edition

  13. Binary Tree Traversal (continued) • Inorder sequence: listing of the nodes produced by the inorder traversal of the tree • Preorder sequence: listing of the nodes produced by the preorder traversal of the tree • Postorder sequence: listing of the nodes produced by the postorder traversal of the tree C++ Programming: Program Design Including Data Structures, Fourth Edition

  14. Binary Tree Traversal (continued) • Inorder sequence: B D A C • Preorder sequence: A B D C • Postorder sequence: D B C A C++ Programming: Program Design Including Data Structures, Fourth Edition

  15. Implementing Binary Trees • Typical operations: • Determine whether the binary tree is empty • Search the binary tree for a particular item • Insert an item in the binary tree • Delete an item from the binary tree • Find the height of the binary tree • Find the number of nodes in the binary tree • Find the number of leaves in the binary tree • Traverse the binary tree • Copy the binary tree C++ Programming: Program Design Including Data Structures, Fourth Edition

  16. Binary Search Trees • We can traverse the tree to determine whether 53 is in the binary tree  this is slow C++ Programming: Program Design Including Data Structures, Fourth Edition

  17. Binary Search Trees (continued) C++ Programming: Program Design Including Data Structures, Fourth Edition

  18. Binary Search Trees (continued) • Every binary search tree is a binary tree C++ Programming: Program Design Including Data Structures, Fourth Edition

  19. Binary Search Trees (continued) C++ Programming: Program Design Including Data Structures, Fourth Edition

  20. Search C++ Programming: Program Design Including Data Structures, Fourth Edition

  21. Insert C++ Programming: Program Design Including Data Structures, Fourth Edition

  22. Insert (continued) C++ Programming: Program Design Including Data Structures, Fourth Edition

  23. Delete C++ Programming: Program Design Including Data Structures, Fourth Edition

  24. Delete (continued) • The delete operation has four cases: • The node to be deleted is a leaf • The node to be deleted has no left subtree • The node to be deleted has no right subtree • The node to be deleted has nonempty left and right subtrees C++ Programming: Program Design Including Data Structures, Fourth Edition

  25. Delete (continued) • To delete an item from the binary search tree, we must do the following: • Find the node containing the item (if any) to be deleted • Delete the node C++ Programming: Program Design Including Data Structures, Fourth Edition

  26. Binary Search Tree: Analysis • Let T be a binary search tree with n nodes, where n > 0 • Suppose that we want to determine whether an item, x, is in T • The performance of the search algorithm depends on the shape of T • In the worst case, T is linear C++ Programming: Program Design Including Data Structures, Fourth Edition

  27. Binary Search Tree: Analysis (continued) • Worst case behavior: T is linear • O(n) key comparisons C++ Programming: Program Design Including Data Structures, Fourth Edition

  28. Binary Search Tree: Analysis (continued) • Average-case behavior: • There are n! possible orderings of the keys • We assume that orderings are possible • S(n) and U(n): number of comparisons in average successful and unsuccessful case, respectively C++ Programming: Program Design Including Data Structures, Fourth Edition

  29. Binary Search Tree: Analysis (continued) C++ Programming: Program Design Including Data Structures, Fourth Edition

More Related