Presentation is loading. Please wait.

Presentation is loading. Please wait.

Binary Search Trees (BST) Let’s look at some pics …and some code. www.javabeat.net/binary-search-tree-traversal-java/

Similar presentations


Presentation on theme: "Binary Search Trees (BST) Let’s look at some pics …and some code. www.javabeat.net/binary-search-tree-traversal-java/"— Presentation transcript:

1 Binary Search Trees (BST) Let’s look at some pics …and some code. www.javabeat.net/binary-search-tree-traversal-java/

2 BST A binary tree with data arranged such that data in each node >= data in its left child node AND < the data in its right child node Why use them? – one can search, insert, delete items quickly, vs. linked lists

3 Pictoral Representation

4 Building a BST Similar to the way sorted linked list was built!

5 Adding a node Drawing a picture is always a good idea!

6 Adding a node public class BinarySearchTree { private Node root; public void insert(int value){ Node node = new Node<>(value); if ( root == null ) { root = node; return; } insertRec(root, node); } private void insertRec(Node latestRoot, Node node){ if ( latestRoot.value > node.value){ if ( latestRoot.left == null ){ latestRoot.left = node; return; } else{ insertRec(latestRoot.left, node); } } else{ if (latestRoot.right == null){ latestRoot.right = node; return; } else{ insertRec(latestRoot.right, node); } } } }

7 Finding the min… What is the big-O order ?

8 Finding the max… What is the big-O order ?

9 Source code… public int findMinimum(){ if ( root == null ){ return 0; } Node currNode = root; while(currNode.left != null){ currNode = currNode.left; } return currNode.value; }

10 Let’s tiptoe through the BST Three ways to traverse the tree: – Inorder Traversal – Preorder Traversal – Postorder Traversal Each public method: calls a private recursive helper method (similar to what we did for add() in the sorted linked list)

11 Inorder Traversal

12 What is the result here?

13 Code for InOrder Traversal public void printInorder() { printInOrder(root); System.out.println(""); } private void printInOrder(Node currentRoot) { if ( currentRoot == null ){ return; } printInOrder(currentRoot.left); System.out.print(currentRoot.data + ", "); printInOrder(currentRoot.right); }

14 Preorder Traversal Pictorally:

15 Preorder Traversal public void printPreorder() { printPreOrder(root); System.out.println(""); } private void printPreOrder(Node currentRoot) { if (currentRoot == null) { return; } System.out.print(currentRoot.data + ", "); printPreOrder(currentRoot.left); printPreOrder(currentRoot.right); }

16 Postorder Traversal Pictorally:

17 PostOrder Traversal public void printPostorder() { printPostOrderRec(root); System.out.println(""); } private void printPostOrder (Node currentRoot) { if (currentRoot == null) { return; } printPostOrder (currentRoot.left); printPostOrder (currentRoot.right); System.out.print(currentRoot.data + ", "); }


Download ppt "Binary Search Trees (BST) Let’s look at some pics …and some code. www.javabeat.net/binary-search-tree-traversal-java/"

Similar presentations


Ads by Google