Binary Search Trees - PowerPoint PPT Presentation

1 / 28
About This Presentation
Title:

Binary Search Trees

Description:

Binary Search Trees Binary Search Tree (BST) A binary search tree (BST) is a binary tree which has the following properties: Each node has a value. – PowerPoint PPT presentation

Number of Views:72
Avg rating:3.0/5.0
Slides: 29
Provided by: Charles484
Category:
Tags: binary | search | trees

less

Transcript and Presenter's Notes

Title: Binary Search Trees


1
Binary Search Trees

2
Binary Search Tree (BST)
  • A binary search tree (BST) is a binary tree
    which has the following properties
  • Each node has a value.
  • An order is defined on these values.
  • The left subtree of a node contains only values
    less than or equal to the node's value.
  • The right subtree of a node contains only values
    greater than or equal to the node's value.

7
11
3
9
13
1
5
3
Binary Search Tree (BST)
  • The major advantage of binary search trees is
    that the related sorting algorithms and search
    algorithms such as in-order traversal can be very
    efficient.

4
Searching a BST
  • Searching a binary tree for a specific value is
    a process that can be performed recursively
    because of the order in which values are stored.
    We begin by examining the root. If the value we
    are searching for equals the root, the value
    exists in the tree. If it is less than the root,
    then it must be in the left subtree, so we
    recursively search the left subtree in the same
    manner. Similarly, if it is greater than the
    root, then it must be in the right subtree, so we
    recursively search the right subtree. If we reach
    a leaf and have not found the value, then the
    item is not where it would be if it were present,
    so it does not lie in the tree at all.

5
Searching in a BST
  • Write an algorithm in pseudocode that searches a
    BST for the key value 9
  • Algorithm TreeSearchFor9( Root )
  • if Root is external
  • then
  • 9 isnt here - return false
  • if Root contains 9
  • then
  • we found it return true
  • else
  • if 9 lt Root.element()
  • then
  • Search(leftSubtree)
  • else
  • Search(rightSubtree)

7
11
3
9
13
1
5
6
TreeSearch Algorithm
  • Algorithm TreeSearch( target, v )
  • IN target key to search for
  • v node to start the search
  • OUT Node w that is either the target or an
    external node where the target should go
  • if v is external node then return v
  • if v.element() target then return v
  • else if target lt v.element() then
  • return TreeSearch( target, leftChild(v) )
  • else
  • return TreeSearch( target, rightChild(v) )

7
Key
  • This is the part of information used to order
    the elements in a binary search tree.
  • Must be able to put it in an order or rank
    i.e., we must be able to compare two keys and
    determine whether one is larger than the other
  • Can be externally generated and assigned
  • Examples
  • Student ID used to identify students
  • Social security number used to identify workers
    in the U.S.
  • Account ID used to identify an account holder
  • ISBN used to identify a book
  • Call number used to identify a library resource

8
BST Stores Keys
  • The data itself can be its own key

D
These letters are data that we can put into an
order based on their ASCII code.
F
B
E
G
A
C
  • The keys may represent other data

6274
These are account numbers that can be ordered,
and they represent records containing additional
data.
8837
2843
9523
1892
4892
9
Item
  • Class that contains a key and the element
    associated with the key
  • Objects of this class are stored in the nodes of
    a BST
  • We search for the value in the key field
  • When we find the key, then we get the element, too

BST Node
10
Item Class
  • templatelttypename Key, typename Elementgt
  • class Item
  • private //Every Item object has two data members
  • Key _key
  • Element _elem
  • public
  • Item( const Key k Key(),
  • const Element e Element())
  • _key(k), _elem(e)
  • const Key key() const return _key
  • Element element() return _elem
  • //...

11
BinarySearchTree Class
  • Public Operations
  • int size()
  • bool isEmpty()
  • pos find( key )
  • void insertItem( key, element )
  • void removeElement( key )
  • Data Member
  • LinkedBinaryTreeltBSTItemgt T

12
TreeSearch Implementationby finder() called by
find()
  • //Algorithm TreeSearch( target, v )
  • // if v is external node then return v
  • // if v.element() target then return v
  • // else if target lt v.element() then
  • // return TreeSearch( target, leftChild(v) )
  • // else
  • // return TreeSearch( target, rightChild(v) )
  • BTPosition finder( const Key k, const
    BTPosition p )
  • if( T.isExternal(p) ) return p // k wasnt
    found
  • Key curKey key(p) //Get the key of node p
  • if( k lt curKey )
  • return finder( k, T.leftChild(p) )
  • else if( k gt curKey )
  • return finder( k, T.rightChild(p) )
  • else
  • return p // k curKey, so we found it

13
Using the Position Object
  • The BinarySearchTree defines its own Position
    class so that it will be able to hold an Item
    object p. 421
  • Its methods make it easy for us to get the
    element or the key
  • Example
  • int recNum p.element()

14
Position
  • typedef typename LinkedBinaryTreeltBSTItemgtPositi
    on BTPosition
  • class Position
  • private
  • BTPosition btPos
  • public
  • Position(const BTPosition p NULL) btPos(p)
  • Element element() // get element
  • return btPos.element().element()
  • const Key key() const // get key
  • return btPos.element().key()
  • bool isNull() const // a null position?
  • return btPos.isNull()

15
Inserting into a BST
  • Insertion begins as a search would begin if the
    root is not equal to the value, we search the
    left or right subtrees as before. Eventually, we
    will reach an external node and add the value as
    its right or left child, depending on the node's
    value. In other words, we examine the root and
    recursively insert the new node to the left
    subtree if the new value is less than or equal
    the root, or the right subtree if the new value
    is greater than the root.

16
InsertingKey 4892, Element 2
6274 0
8837 3
2843 1
17
InsertingKey 4892, Element 2
  • Find the position of an external node where the
    key should be inserted

p index.finder( 4892, T.root() )
6274 0
8837 3
2843 1
18
InsertingKey 4892, Element 2
  • Find an external node where the key should be
    inserted
  • Expand the node

T.expandExternal(p)
6274 0
8837 3
2843 1
19
InsertingKey 4892, Element 2
  • Find an external node where the key should be
    inserted
  • Expand the node
  • Add the key and element at the node

6274 0
8837 3
2843 1
4892 2
20
Deleting from a BST
  • There are several cases to be considered
  • Deleting a leaf Deleting a node with no children
    is easy, as we can simply remove it from the
    tree.
  • Deleting a node with one child Delete it and
    replace it with its child.
  • Deleting a node with two children Suppose the
    node to be deleted is called N. We replace the
    value of N with either its in-order successor
    (the left-most child of the right subtree) or the
    in-order predecessor (the right-most child of the
    left subtree).

21
Deleting a node with one child (Key 4892)
  • Find the position of the node that contains the
    key

6274 0
8837 3
2843 1
r
9523 5
1892 4
4892 2
3165 6
22
Deleting a node with one child (Key 4892)
  • Find the position of the node that contains the
    key
  • If one of the nodes children is external remove
    the node and replace it with the sibling of its
    external child

6274 0
8837 3
2843 1
r
9523 5
1892 4
4892 2
p
3165 6
23
Deleting a node with one child (Key 4892)
  • Find the position of the node that contains the
    key
  • If one of the nodes children is external remove
    the node and replace it with the sibling of its
    external child

6274 0
8837 3
2843 1
9523 5
1892 4
3165 6
24
Deleting a node with two children (Key 2843)
  • Find the position of the node that contains the
    key, call it r

6274 0
r
8837 3
2843 1
9523 5
1892 4
4892 2
3165 6
25
Deleting a node with two children (Key 2843)
  • Find the position of the node that contains the
    key, call it r
  • If both of rs children are internal
  • Find the left-most node in rs right subtree,
    call it p
  • Move the contents of ps parent into r
  • Remove p and ps parent

6274 0
r
8837 3
2843 1
9523 5
1892 4
4892 2
3165 6
p
26
Deleting a node with two children (Key 2843)
  • Find the position of the node that contains the
    key, call it r
  • If both of rs children are internal
  • Find the left-most node in rs right subtree,
    call it p
  • Move the contents of ps parent into r
  • Remove p and ps parent

6274 0
r
8837 3
3165 6
9523 5
1892 4
4892 2
3165 6
p
27
Deleting a node with two children (Key 2843)
  • Find the position of the node that contains the
    key, call it r
  • If both of rs children are internal
  • Find the left-most node in rs right subtree,
    call it p
  • Move the contents of ps parent into r
  • Remove p and ps parent

6274 0
r
8837 3
3165 6
9523 5
1892 4
4892 2
28
Removing Implemented by remover() called by
removeElement()
  • BTPosition remover( const BTPosition r ) //r
    was located with finder()
  • BTPosition p
  • if( T.isExternal( T.leftChild(r) )) p
    T.leftChild(r)
  • else if( T.isExternal( T.rightChild(r) )) p
    T.rightChild(r)
  • else
  • p T.rightChild(r)
  • do p T.leftChild(p) while(
    T.isInternal(p) )
  • setItem( r, T.parent(p).element() )
  • return T.removeAboveExternal(p)
Write a Comment
User Comments (0)
About PowerShow.com