Top Banner
ADT Binary Search Tree Ellen Walker CPSC 201 Data Structures Hiram College
23

ADT Binary Search Tree - cs.hiram.eduwalkerel/cs201/BinSrchTreeJava.pdf• Its root has two subtrees such that each is a binary ... //This algorithm reads nodes in ... //N is the number

Apr 27, 2018

Download

Documents

phungcong
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: ADT Binary Search Tree - cs.hiram.eduwalkerel/cs201/BinSrchTreeJava.pdf• Its root has two subtrees such that each is a binary ... //This algorithm reads nodes in ... //N is the number

ADT Binary Search Tree!

Ellen Walker!CPSC 201 Data Structures!

Hiram College!

Page 2: ADT Binary Search Tree - cs.hiram.eduwalkerel/cs201/BinSrchTreeJava.pdf• Its root has two subtrees such that each is a binary ... //This algorithm reads nodes in ... //N is the number

Binary Search Tree!

•  Value-based storage of information!–  Data is stored in order!–  Data can be retrieved by value efficiently!

•  Is a binary tree!–  Everything in left subtree is < root!–  Everything in right subtree is >root!–  Both left and right subtrees are also BSTʼs!

Page 3: ADT Binary Search Tree - cs.hiram.eduwalkerel/cs201/BinSrchTreeJava.pdf• Its root has two subtrees such that each is a binary ... //This algorithm reads nodes in ... //N is the number

Operations on BST!

•  Some can be inherited from binary tree!–  Constructor (for empty tree)!–  Inorder, Preorder, and Postorder traversal!

•  Some must be defined !–  Insert item!–  Delete item!–  Retrieve item!

Page 4: ADT Binary Search Tree - cs.hiram.eduwalkerel/cs201/BinSrchTreeJava.pdf• Its root has two subtrees such that each is a binary ... //This algorithm reads nodes in ... //N is the number

The Node<E> Class!

•  Just as for a linked list, a node consists of a data part and links to successor nodes!

•  The data part is a reference to type E!•  A binary tree node must have links to both its

left and right subtrees!

Page 5: ADT Binary Search Tree - cs.hiram.eduwalkerel/cs201/BinSrchTreeJava.pdf• Its root has two subtrees such that each is a binary ... //This algorithm reads nodes in ... //N is the number

The BinaryTree<E> Class!

Page 6: ADT Binary Search Tree - cs.hiram.eduwalkerel/cs201/BinSrchTreeJava.pdf• Its root has two subtrees such that each is a binary ... //This algorithm reads nodes in ... //N is the number

The BinaryTree<E> Class (continued)!

Page 7: ADT Binary Search Tree - cs.hiram.eduwalkerel/cs201/BinSrchTreeJava.pdf• Its root has two subtrees such that each is a binary ... //This algorithm reads nodes in ... //N is the number

Overview of a Binary Search Tree!

•  Binary search tree definition!–  A set of nodes T is a binary search tree if either of

the following is true!•  T is empty!•  Its root has two subtrees such that each is a binary

search tree and the value in the root is greater than all values of the left subtree but less than all values in the right subtree!

Page 8: ADT Binary Search Tree - cs.hiram.eduwalkerel/cs201/BinSrchTreeJava.pdf• Its root has two subtrees such that each is a binary ... //This algorithm reads nodes in ... //N is the number

Overview of a Binary Search Tree (continued)!

Page 9: ADT Binary Search Tree - cs.hiram.eduwalkerel/cs201/BinSrchTreeJava.pdf• Its root has two subtrees such that each is a binary ... //This algorithm reads nodes in ... //N is the number

Searching a Binary Tree!

Page 10: ADT Binary Search Tree - cs.hiram.eduwalkerel/cs201/BinSrchTreeJava.pdf• Its root has two subtrees such that each is a binary ... //This algorithm reads nodes in ... //N is the number

Class TreeSet and Interface Search Tree!

Page 11: ADT Binary Search Tree - cs.hiram.eduwalkerel/cs201/BinSrchTreeJava.pdf• Its root has two subtrees such that each is a binary ... //This algorithm reads nodes in ... //N is the number

BinarySearchTree Class!

Page 12: ADT Binary Search Tree - cs.hiram.eduwalkerel/cs201/BinSrchTreeJava.pdf• Its root has two subtrees such that each is a binary ... //This algorithm reads nodes in ... //N is the number

BST Algorithms!

•  Search!•  Insert!•  Delete!•  Print values in order!

–  We already know this, itʼs inorder traversal!–  Thatʼs why itʼs called “in order”!

Page 13: ADT Binary Search Tree - cs.hiram.eduwalkerel/cs201/BinSrchTreeJava.pdf• Its root has two subtrees such that each is a binary ... //This algorithm reads nodes in ... //N is the number

Searching the Binary Tree!

•  If the tree is empty, the item is not found!•  Else if the item is at the root, the item is found!•  Else if the item is < the root, search the left

subtree!•  Else if the item is > the root, search the right

subtree!

Page 14: ADT Binary Search Tree - cs.hiram.eduwalkerel/cs201/BinSrchTreeJava.pdf• Its root has two subtrees such that each is a binary ... //This algorithm reads nodes in ... //N is the number

Inserting into a binary tree!

•  Where is the right place to insert?!–  Wherever an unsuccessful search would have

ended!•  Insert algorithm is a lot like search algorithm!

–  But it keeps going until it gets to a leaf!–  And then adds a new left or right child, as

appropriate.!

Page 15: ADT Binary Search Tree - cs.hiram.eduwalkerel/cs201/BinSrchTreeJava.pdf• Its root has two subtrees such that each is a binary ... //This algorithm reads nodes in ... //N is the number

Insertion into a Binary Search Tree!

Page 16: ADT Binary Search Tree - cs.hiram.eduwalkerel/cs201/BinSrchTreeJava.pdf• Its root has two subtrees such that each is a binary ... //This algorithm reads nodes in ... //N is the number

Removing from a binary tree!

•  Can we “link around the node” as in a linked list?!–  Not quite: when we remove a node, we have two

“orphaned children” to take care of!•  We need to find another node in the tree to

become a “foster parent,” -- it will replace the current node!

•  The foster parent should have no more than one child (so it can be “linked around”)!

Page 17: ADT Binary Search Tree - cs.hiram.eduwalkerel/cs201/BinSrchTreeJava.pdf• Its root has two subtrees such that each is a binary ... //This algorithm reads nodes in ... //N is the number

A nodeʼs immediate successor!

•  Is greater than the node!–  It is in the right subtree!

•  Is smaller than any other nodes that are greater than the node!–  It is the smallest value in the right subtree!–  It is the leftmost node in the right subtree!–  It has no left child! !

•  Every node in the left subtree is smaller than it, and every other node in the right subtree is larger than it!

•  Therefore, it is the perfect “foster parent”!!

Page 18: ADT Binary Search Tree - cs.hiram.eduwalkerel/cs201/BinSrchTreeJava.pdf• Its root has two subtrees such that each is a binary ... //This algorithm reads nodes in ... //N is the number

How to find the nodeʼs successor!

•  Take one step to the right!–  To get into the right subtree!

•  Keep moving to the left until you reach a node with no left child!–  To get the minimum child from that tree!

Page 19: ADT Binary Search Tree - cs.hiram.eduwalkerel/cs201/BinSrchTreeJava.pdf• Its root has two subtrees such that each is a binary ... //This algorithm reads nodes in ... //N is the number

Removing the successor node!

•  Successorʼs right child is adopted by its grandparent!•  Successor nodeʼs data replaces ʻdeletedʼ nodeʼs data !

Data

NULL

Grandparent!

Right child of deletee, adopted by grandparent!

Successor!

More relatives, unaffected!

Page 20: ADT Binary Search Tree - cs.hiram.eduwalkerel/cs201/BinSrchTreeJava.pdf• Its root has two subtrees such that each is a binary ... //This algorithm reads nodes in ... //N is the number

Removing from a Binary Search Tree !

Page 21: ADT Binary Search Tree - cs.hiram.eduwalkerel/cs201/BinSrchTreeJava.pdf• Its root has two subtrees such that each is a binary ... //This algorithm reads nodes in ... //N is the number

Building a BST from a Sorted List!

•  Given a sorted list of items, how do you build a good BST for it?!–  Insert items in sorted order (NO! - why?)!–  Insert items in reverse sorted order?!

–  What is the best choice for a root that will keep the tree balanced?!

–  Which nodes should go in the left subtree?!–  Which nodes go in the right subtree?!

Page 22: ADT Binary Search Tree - cs.hiram.eduwalkerel/cs201/BinSrchTreeJava.pdf• Its root has two subtrees such that each is a binary ... //This algorithm reads nodes in ... //N is the number

Algorithm for building BST!

//This algorithm reads nodes in order, but builds a balanced tree!//N is the number of nodes that go in the tree!Node<String> readtree(Scanner sc, int N){! if(N==0) {! root = null; return root;! }! root = new Node<String>();! root.left = readtree(sc,N/2);! root.data = sc.next();! root.right = readtree(sc, (N-1)-(N/2));! return root;!}!

Page 23: ADT Binary Search Tree - cs.hiram.eduwalkerel/cs201/BinSrchTreeJava.pdf• Its root has two subtrees such that each is a binary ... //This algorithm reads nodes in ... //N is the number

Relationship between BST and Binary Search Algorithm!

•  If we build a BST from the sorted data according to the prior algorithm,!

•  Then the middle element (of each subtree) will be the root!

•  So as we narrow our search, we will always consider the middle element of the remaining elements first.!

•  Therefore, the nodes visited by the BST search algorithm are exactly the same items, in exactly the same order, as these items would be visited by binary search!!