18_BinarySearchTrees.ppt - Binary Search Trees 1 Binary...

This preview shows page 1 - 11 out of 40 pages.

1 Binary Search Trees
Image of page 1

Subscribe to view the full document.

2 Binary Search Usually outperforms a linear search Disadvantage: Requires a sequential storage Not appropriate for linked lists (Why?) It is possible to use a linked structure which can be searched in a binary-like manner
Image of page 2
3 Binary Search Tree Consider the following ordered list of integers 1. Examine middle element 2. Examine left, right sublist (maintain pointers) 3. (Recursively) examine left, right sublists 80 66 62 49 35 28 13
Image of page 3

Subscribe to view the full document.

4 Binary Search Tree Redraw the previous structure so that it has a treelike shape – a binary tree
Image of page 4
5 BST Definition A binary tree T is a binary search tree (BST) provided at each node u of T containing key value K: (1) all key values in the left subtree of u are ≤ K; and (2) all key values in the right subtree of u are > K Basic operations Construct an empty BST Determine if BST is empty Search BST for given item
Image of page 5

Subscribe to view the full document.

6 Basic Operations Construct an empty BST Determine if BST is empty Search BST for given item Insert a new item in the BST Maintain the BST property Delete an item from the BST Maintain the BST property Traverse the BST Visit each node exactly once
Image of page 6
7 ADT Binary Search Tree (BST) Node class (BinNode) declared in the private section of the BST declaration
Image of page 7

Subscribe to view the full document.

/* After the BinNode declaration, BST.h contains the declaration of class template BST. Basic operations: Constructor: Constructs an empty BST empty: Checks if a BST is empty search: Search a BST for an item insert: Inserts a value into a BST remove: Removes a value from a BST inorder: Inorder traversal of a BST -- output the data values postorder: Postorder traversal – output the data valules
Image of page 8
Private utility helper operations: search2: Used by delete inorderAux: Used by inorder postorderAux: Used by postorder Other operations (Exercises): destructor copy constructor assignment operator
Image of page 9

Subscribe to view the full document.

10 BST Traversals Note that recursive calls must be made To left subtree To right subtree Must use two functions Public method to send message to BST object Private auxiliary method that can access BinNodes
Image of page 10
Image of page 11
  • Fall '19
  • Tree traversal, locptr

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern

Ask Expert Tutors You can ask 0 bonus questions You can ask 0 questions (0 expire soon) You can ask 0 questions (will expire )
Answers in as fast as 15 minutes