Presentation is loading. Please wait.

Presentation is loading. Please wait.

Binary Search Trees.

Similar presentations


Presentation on theme: "Binary Search Trees."— Presentation transcript:

1 Binary Search Trees

2 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

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

4 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

5 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

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

7 Example

8 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

9 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

10 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

11 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

12 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

13 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

14 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

15 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

16 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

17 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

18 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

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

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

21 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

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

23 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 >) };

24 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); };

25 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 }

26 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. }

27 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; }

28 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); }

29 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

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

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

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

33 Traversal In-Order: (sorted!) Pre-Order: Post-Order:

34 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 }

35 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)

36 Height BST Height: 3 Height of 70: 2

37 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; }

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


Download ppt "Binary Search Trees."

Similar presentations


Ads by Google