Binary Search Trees.

Slides:



Advertisements
Similar presentations
Comp 122, Spring 2004 Binary Search Trees. btrees - 2 Comp 122, Spring 2004 Binary Trees  Recursive definition 1.An empty tree is a binary tree 2.A node.
Advertisements

Trees Types and Operations
Computer Science C++ High School Level By Guillermo Moreno.
Recursion practice. Problem 0 Using recursion (and no arrays), write the code to read in a series of numbers (until EOF) and then print them backwards.
BST Data Structure A BST node contains: A BST contains
Version TCSS 342, Winter 2006 Lecture Notes Trees Binary Trees Binary Search Trees.
Properties: -Each node has a value -The left subtree contains only values less than the parent node’s value -The right subtree contains only values greater.
Data Structures - CSCI 102 Binary Tree In binary trees, each Node can point to two other Nodes and looks something like this: template class BTNode { public:
1 TK1924 Program Design & Problem Solving Session 2011/2012 L8: Binary Trees.
1 Chapter 10 Trees. 2 Definition of Tree A tree is a set of linked nodes, such that there is one and only one path from a unique node (called the root.
Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy Walters, and Godfrey Muganda Modified for use at Midwestern State University Chapter.
1 Chapter 7 Objectives Upon completion you will be able to: Create and implement binary search trees Understand the operation of the binary search tree.
CMSC 341 Introduction to Trees. 2/21/20062 Tree ADT Tree definition –A tree is a set of nodes which may be empty –If not empty, then there is a distinguished.
1/14/20161 BST Operations Data Structures Ananda Gunawardena.
Binary Search Tree. Tree  A nonlinear data structure consisting of nodes, each of which contains data and pointers to other nodes.  Each node has only.
ADT Binary Search Tree Ellen Walker CPSC 201 Data Structures Hiram College.
Binary Search Trees (BST)
CMSC 202, Version 5/02 1 Trees. CMSC 202, Version 5/02 2 Tree Basics 1.A tree is a set of nodes. 2.A tree may be empty (i.e., contain no nodes). 3.If.
Trees By JJ Shepherd. Introduction Last time we discussed searching and sorting in a more efficient way Divide and Conquer – Binary Search – Merge Sort.
(c) University of Washington20c-1 CSC 143 Binary Search Trees.
(c) University of Washington20-1 CSC 143 Java Trees.
BSTs, AVL Trees and Heaps Ezgi Shenqi Bran. What to know about Trees? Height of a tree Length of the longest path from root to a leaf Height of an empty.
TREES From root to leaf. Trees  A tree is a non-linear collection  The elements are in a hierarchical arrangement  The elements are not accessible.
Binary Search Trees Chapter 7 Objectives
Data Structures and Design in Java © Rick Mercer
Trees Chapter 15.
CSCE 3110 Data Structures & Algorithm Analysis
CSCE 3110 Data Structures & Algorithm Analysis
Data Structures Binary Trees 1.
BST Trees
UNIT III TREES.
CISC220 Fall 2009 James Atlas Lecture 13: Binary Trees.
Binary Search Tree (BST)
Introduction Applications Balance Factor Rotations Deletion Example
Trees Lecture 12 CS2110 – Fall 2017.
Binary Search Tree Chapter 10.
CMSC 341 Introduction to Trees.
Section 8.1 Trees.
Trees.
Data Structures & Algorithm Design
Lecture 18. Basics and types of Trees
ITEC 2620M Introduction to Data Structures
Tree data structure.
Binary Search Trees Why this is a useful data structure. Terminology
Binary Tree Applications
Data Structures Using C++ 2E
Binary Trees, Binary Search Trees
Chapter 20: Binary Trees.
Chapter 21: Binary Trees.
Trees 7/14/2009.
Lec 12 March 9, 11 Mid-term # 1 (March 21?)
Tree data structure.
CS 367 – Introduction to Data Structures
Find in a linked list? first last 7  4  3  8 NULL
Search Sorted Array: Binary Search Linked List: Linear Search
Trees CMSC 202, Version 5/02.
CMSC 202 Trees.
Binary Trees, Binary Search Trees
Non-Linear Structures
Linked Lists.
CSC 143 Java Trees.
CSC 143 Binary Search Trees.
Chapter 20: Binary Trees.
Binary Trees.
Trees.
Search Sorted Array: Binary Search Linked List: Linear Search
Lecture 9: Intro to Trees
Binary Trees, Binary Search Trees
Data Structures Using C++ 2E
Tree (new ADT) Terminology: A tree is a collection of elements (nodes)
Presentation transcript:

Binary Search Trees

Linear Search A Linear Search examines every node of a list until a matching node is found, or until all nodes have been examined and no match is found. For very long lists that are frequently searched, this can take a large amount of time... sometimes, too much time

Faster Searching To search faster, other algorithms, sometimes using non-linear data structures, are needed. One such data structure is the Binary Search Tree

Binary Search Tree Node Each node has: - a Key member - a Left pointer (to node) - a Right pointer (to node) The node pointed to by Left is called the Left Child The node pointed to by Right is called the Right Child The node itself may be called the Parent

Definition Root A BST consists of a root (pointer to node) which is: - NULL for Empty, or - Points to the first node in the BST, called the Root Node

Definition For every node in a BST: - left points to the root of a (sub) BST - right points to the root of a (sub) BST - key is - greater than or equal to the keys of all nodes in the left subtree - less than all notes in the right subtree.

Example

Search Given: r - root of a (sub) BST, skey - a value to search for If r is EMPTY NOT FOUND! else if key of r == skey FOUND! else if skey < key of r search r's left child for skey else (skey > key of r) search r's right child for skey

Search Given: r - root of a (sub) BST, skey - a value to search for If r is EMPTY NOT FOUND! else if key of r == skey FOUND! else if skey < key of r search r's left child for skey else (skey > key of r) search r's right child for skey

Search Given: r - root of a (sub) BST, skey - a value to search for If r is EMPTY NOT FOUND! else if key of r == skey FOUND! else if skey < key of r search r's left child for skey else (skey > key of r) search r's right child for skey

Search Given: r - root of a (sub) BST, skey - a value to search for If r is EMPTY NOT FOUND! else if key of r == skey FOUND! else if skey < key of r search r's left child for skey else (skey > key of r) search r's right child for skey

Search Given: r - root of a (sub) BST, skey - a value to search for If r is EMPTY NOT FOUND! else if key of r == skey FOUND! else if skey < key of r search r's left child for skey else (skey > key of r) search r's right child for skey

Algorithms Insert Given: r - root of a (sub) BST n - pointer to a new node to insert If r is EMPTY make r point to n else if key of n <= key of r insert n into r's left child else insert n into r's right child

Insert Given: r - root of a (sub) BST n - pointer to new node If r is EMPTY make r point to n else if key of n <= key of r insert n into r's left child else insert n into r's right child

Insert Given: r - root of a (sub) BST n - a new node to insert If r is EMPTY make r point to n else if key of n <= key of r insert n into r's left child else insert n into r's right child

Insert Given: r - root of a (sub) BST n - a new node to insert If r is EMPTY make r point to n else if key of n <= key of r insert n into r's left child else insert n into r's right child

Insert Given: r - root of a (sub) BST n - a new node to insert If r is EMPTY make r point to n else if key of n <= key of r insert n into r's left child else insert n into r's right child

Remove To remove a node r from a BST: - detach r from its parent (root is special case) - if r has no children: done - if r has one child - attach r's parent to r's child - detach r's child - if r has two children (this is more difficult) - attach r's parent to one of r's childRen - insert the other child (and its whole subtree) into the BST - detach r's children

Remove detach r from its parent - if r has no children: done

Remove - detach r from its parent - if r has one child - attach r's parent to r's child - detach

Remove - detach r from its parent - if r has two children, attach 1 to r's parent, Insert the other child into the tree - detach r's children

Balancing A BST can become unbalanced depending on the order that nodes are inserted. This lessens its effectiveness of fast-searching. The BST can be Balanced by reconstructing it by inserting nodes in the correct order.

Code: node class class node { friend class BST; private: int key = 0; string data = ""; // could be more data members node * left = NULL; // ptr to left subtree (keys <=) node * right = NULL; // ptr to right subtree (keys >) };

Code: BST class class BST { // more methods could be added public: void insert(int newKey, string newData); string search(int forKey); void print(); int height(); private: node * root = NULL; void insert(node * n, node * r); node * search(int forKey, node * r); void print(node * r); int height(node * r); };

Code: insert() // public: allocate, populate, check for empty tree void BST::insert(int newkey, string newdata) { node * n = new node; // allocate n->key = newkey; // populate n->data = newdata; if (root == NULL) // if empty tree root = n; // new node is new root else // else insert new node into insert(n, root); // entire existing tree }

Code: insert() // private: n points to new node, already allocated/populated // r points to subroot (root of a subtree, not THE root) - never NULL void BST::insert(node * n, node * r) { if (n->key <= r->key) // if new node should go on left subtree if (r->left == NULL) // if there is no left subtree yet r->left = n; // new node becomes root of left subtree else // else there is already a left subtree insert(n, r->left); // insert the new node into it. else // n->key > r->key, new node goes right if (r->right == NULL) // if there is no right subtree yet r->right = n; // new node becomes root of right subtree else // else there is already a right subtree insert(n, r->right); // insert new node into it. }

Code: search() // public: return data of found node, or "" for not found string BST::search(int forKey) { node * p = search(forKey, root); if (p==NULL) return ""; else return p->data; }

Code: search() // private: return pointer to found node, or NULL not found node * BST::search(int forKey, node * r) { if (r==NULL) return NULL; // not found! if (r->key == forKey) return r; // found! if (forKey <= r->key) return search(forKey, r->left); else return search(forKey, r->right); }

Traversal Traversal is an algorithm that "visits" all nodes of a tree in a particular order. For a Binary Tree, there are three orders: - In-Order - Pre-Order - Post Order

Traversal In-Order Traversal: given r-the (sub)root of a BST - Traverse r's left child - Visit r - Traverse r's right child

Traversal Pre-Order Traversal: given r-the (sub)root of a BST - Visit r - Traverse r's left child - Traverse r's right child

Traversal Post-Order Traversal: given r-the (sub)root of a BST - Traverse r's left child - Traverse r's right child - Visit r

Traversal In-Order: (sorted!) 50 55 65 70 75 80 Pre-Order: 65 55 50 75 70 80 Post-Order: 50 55 70 80 75 65

Code: print() void BST::print() { print(root); } // get it started void BST::print(node * r) { // prints keys sorted lowest to highest (IN-ORDER) if (r==NULL) return; // nothing to print print(r->left); // everything <= this node cout << r->key << " "; // print this node print(r->right); // everything > this node }

Height Node Height is the number of edges (arrows) between the root and the node (height of root is 0) BST Height is the maximum of the heights of all nodes. (or: count nodes along longest path minus 1)

Height BST Height: 3 Height of 70: 2

Code: height() int BST::height() { return height(root); } // get it started int BST::height(node * r) { if (r==NULL) return 0; // height of empty is 0 int heightLeft = height(r->left); // get height of left child int heightRight = height(r->right); // get height of right child if (heightLeft > heightRight) // My height is the height return heightLeft + 1; // of my tallest child else // plus one for me. return heightRight + 1; }

Vocabulary Term Definition Binary Search Tree (BST) a data structure that consists of a root (pointer to the first node), where each node contains a key, left child (pointer to node) and right child (pointer to node), where they keys of all nodes in the left child are less than or equal to the key of the node, and the keys of all nodes in the left child are greater than the key of the node. root pointer to the first node in a BST leaf a BST node where both children are NULL parent a name for a BST node in relation to its two children balanced describes a BST with the minimum height possible for the number of nodes it contains height (of a BST) the longest path from the root to any leaf traversal an algorithm that visits (accesses/examines) all nodes of the BST.