Lecture 21-22

# Lecture 21-22 - 1 CSC212 Data Structures 2  Consider the...

This preview shows pages 1–14. Sign up to view the full content.

1 CSC212: Data Structures

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
2 Consider the search operation FindKey: find an element of a particular key value in a binary tree. In binary tree this operation is O(n) . In a binary tree of 10 6 nodes 10 6 steps required. In a Binary Search Tree (BST) this operation can be performed very efficiently: O(log 2 n) . A binary search tree of 10 6 nodes log 2 (10 6 ) 20 steps only are required.
3 A Binary Search Tree (BST) is a binary tree such that for each node, say N, the following statements are true: 1. If L is any node in the left subtree of N, then L is less than N. 2. If R is any node in the right subtree of N, then R is greater than N.

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
4 91 65 73 53 14 35 93 50 33 0 40 44 56 55 58 81 80 71 87 69
5 Elements: the elements are nodes (BSTNode), each node contains the following data type: Type. Structure: hierarchical structure; each node can have two children: left or right child; there is a root node and a current node. If N is any node in the tree, nodes in the left subtree < N and nodes in the right subtree > N. Domain: the number of nodes in a BST is bounded; type/class name is BST

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
6 Operations: 1. Method FindKey (int tkey, boolean found). requires: none. input: tkey. results: If bst contains a node whose key value is tkey, then that node is made the current node and found is set to true; otherwise found is set to false and either the tree is empty or the current node is the node to which the node with key = tkey would be attached as a child if it were added to the BST. output: found.
7 2. Method Insert (int k, Type e, boolean inserted) requires: Full (bst) is false. input: key, e. results: if bst does not contain k then inserted is set to true and node with k and e is inserted and made the current element; otherwise inserted is set to false and current value does not change. output: inserted. 3. Method Remove_Key (int tkey, boolean removed) input: tkey results: Node with key value tkey is removed from the bst and removed set to true. If BST is not empty then root is made the current. output: removed

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
8 4. Method Update(int key, Type e, boolean updated) requires: Empty(bst) is false. input: key, e. results: current node’s element is replaced with e. Output: updated. These operations have the same specification as ADT Binary Tree. 5. Method Traverse (Order ord) 6. Method DeleteSub ( ) 7. Method Retrieve (Type e) 8. Method Empty ( boolean empty ).
9 9. Method Full (boolean full)

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
10 public class BSTNode <T> { public int key; public T data; public BSTNode<T> left, right; /** Creates a new instance of BSTNode */ public BSTNode( int k, T val) { key = k; data = val; left = right = null ; } public BSTNode( int k, T val, BSTNode<T> l, BSTNode<T> r) { key = k; data = val; left = l; right = r; } }
11 public class BST <T> { BSTNode<T> root, current; /** Creates a new instance of BST */ public BST() { root = current = null ; } public boolean empty() { return root == null ; } public boolean full() { return false ; } public T retrieve () { return current.data; }

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
12 public class BST <T> { BSTNode<T> root, current; /** Creates a new instance of BST */ public BST() { root = current = null; } public boolean empty() { return root == null ; } public boolean full() { return false ; } public T retrieve () { return current.data; } R C null
13

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

### What students are saying

• 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.

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

• 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.

Dana University of Pennsylvania ‘17, Course Hero Intern

• 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.

Jill Tulane University ‘16, Course Hero Intern