SlideShare a Scribd company logo
CSCE 3110
   Data Structures &
   Algorithm Analysis

Rada Mihalcea
http://www.cs.unt.edu/~rada/CSCE3110

Binary Search Trees
Reading: Chap. 4 (4.3) Weiss
A Taxonomy of Trees
General Trees – any number of children / node

Binary Trees – max 2 children / node

  Heaps – parent < (>) children

  Binary Search Trees
Binary Trees

Binary search tree
  Every element has a unique key.
  The keys in a nonempty left subtree (right subtree)
  are smaller (larger) than the key in the root of
  subtree.
  The left and right subtrees are also binary search
  trees.
Binary Search Trees

Binary Search Trees (BST) are a type of
Binary Trees with a special organization of
data.

This data organization leads to O(log n)
complexity for searches, insertions and
deletions in certain types of the BST
(balanced trees).
  O(h) in general
Binary Search Algorithm

Binary Search algorithm of an array of sorted items
reduces the search space by one half after each comparison


               34   41   56   63   72   89   95
                0    1    2    3    4    5    6


          34   41   56                  72   89   95
           0    1    2                   4    5    6


                                        72        95
          34        56
                                         4         6
           0         2
Organization Rule for BST

• the values in all nodes in the left subtree of a node are less than

 the node value
• the values in all nodes in the right subtree of a node are greater
                                63
  than the node values



                41                              89



                                           72        95
           34        56
Binary Tree
typedef struct tnode *ptnode;
  typedef struct node {
           short int key;
           ptnode right, left;
  };
BST Operations: Search
Searching in the BST
method search(key)
• implements the binary search based on comparison of the items
  in the tree

• the items in the BST must be comparable (e.g integers, string,
etc.)

The search starts at the root. It probes down, comparing the
values in each node with the target, till it finds the first item equal
to the target. Returns this item or null if there is none.
Search in BST - Pseudocode


if the tree is empty
         return NULL

else if the item in the node equals the target
        return the node value

else if the item in the node is greater than the target
        return the result of searching the left subtree

else if the item in the node is smaller than the target
        return the result of searching the right subtree
Search in a BST: C code

Ptnode search(ptnode root,
      int key)
{
/* return a pointer to the node that
   contains key. If there is no such
   node, return NULL */

    if (!root) return NULL;
    if (key == root->key) return root;
    if (key < root->key)
        return search(root->left,key);
    return search(root->right,key);
}
BST Operations: Insertion
method insert(key)
 places a new item near the frontier of the BST while retaining its
organization of data:
    starting at the root it probes down the tree till it finds a
    node whose left or right pointer is empty and is a logical place
    for the new value
    uses a binary search to locate the insertion point
    is based on comparisons of the new item and values of nodes
    in the BST
       Elements   in nodes must be comparable!
Case 1: The Tree is Empty
  Set the root to a new node containing the item
Case 2: The Tree is Not Empty
  Call a recursive helper method to insert the
  item                                10 > 7
                                  10
                      7

             5                9         10 > 9

        4         6       8        10
Insertion in BST - Pseudocode

if tree is empty
     create a root node with the new key
else
     compare key with the top node
     if key = node key
          replace the node with the new value
     else if key > node key
          compare key with the right subtree:
            if subtree is empty create a leaf node
            else add key in right subtree
      else key < node key
          compare key with the left subtree:
            if the subtree is empty create a leaf node
            else add key to the left subtree
Insertion into a BST: C code

void insert (ptnode *node, int key)
{
  ptnode ptr,
       temp = search(*node, key);
  if (temp || !(*node)) {
    ptr = (ptnode) malloc(sizeof(tnode));
    if (IS_FULL(ptr)) {
      fprintf(stderr, “The memory is fulln”);
      exit(1);
    }
    ptr->key = key;
    ptr->left = ptr->right = NULL;
    if (*node)
      if (key<temp->key) temp->left=ptr;
         else temp->right = ptr;
    else *node = ptr;
  }
}
BST Shapes


 The order of supplying the data determines where it is
placed in the BST , which determines the shape of the BST
 Create BSTs from the same set of data presented each time
in a different order:
a) 17 4 14 19 15 7 9 3 16 10
b) 9 10 17 4 3 7 14 16 15 19
c) 19 17 16 15 14 10 9 7 4 3 can you guess this shape?
BST Operations: Removal



 removes a specified item from the BST and adjusts the tree
 uses a binary search to locate the target item:
     starting at the root it probes down the tree till it finds the
   target or reaches a leaf node (target not in the tree)


removal of a node must not leave a ‘gap’ in the tree,
Removal in BST - Pseudocode


method remove (key)
I if the tree is empty return false
II Attempt to locate the node containing the target using the
   binary search algorithm
   if the target is not found return false
   else the target is found, so remove its node:
   Case 1: if the node has 2 empty subtrees
             replace the link in the parent with null

  Case 2: if the node has a left and a right subtree
       - replace the node's value with the max value in the
         left subtree
       - delete the max node in the left subtree
Removal in BST - Pseudocode


Case 3: if the node has no left child
 - link the parent of the node
 - to the right (non-empty) subtree

Case 4: if the node has no right child
 - link the parent of the target
 - to the left (non-empty) subtree
Removal in BST: Example

Case 1: removing a node with 2 EMPTY SUBTREES


                   parent               7

             cursor             5               9

                            4       6       8       10
                                        7
Removing 4
replace the link in the
                                5               9
parent with null
                                    6       8       10
Removal in BST: Example

 Case 2: removing a node with 2 SUBTREES
  - replace the node's value with the max value in the left subtree
  - delete the max node in the left subtree
                                                 What other element
   Removing 7                                    can be used as
                                      cursor     replacement?
cursor

                  7                                6

        5                 9                5               9

  4           6       8       10    4                  8         10
Removal in BST: Example


Case 3: removing a node with 1 EMPTY SUBTREE
the node has no left child:
link the parent of the node to the right (non-empty) subtree


parent
                                     parent
                 7                                    7
cursor
         5               9         cursor     5                9

             6       8        10                  6       8        10
Removal in BST: Example


Case 4: removing a node with 1 EMPTY SUBTREE
 the node has no right child:
 link the parent of the node to the left (non-empty) subtree
  Removing 5
                                      parent
 parent

                 7               cursor              7
cursor
          5              9                    5                9

   4                 8          10        4              8         10
Analysis of BST Operations


The complexity of operations get, insert and
remove in BST is O(h) , where h is the height.
O(log n) when the tree is balanced. The updating
operations cause the tree to become unbalanced.
The tree can degenerate to a linear shape and the
operations will become O (n)
Best Case

BST tree = new BST();

tree.insert   ("E");
tree.insert   ("C");
tree.insert   ("D");
tree.insert   ("A");
tree.insert   ("H");
tree.insert   ("F");
tree.insert   ("K");


               >>>> Items in advantageous order:
Output:                 K
                     H
                        F
               E
                        D
                     C
                        A
Worst Case

BST tree = new BST();
for (int i = 1; i <= 8; i++)
   tree.insert (i);




Output:          >>>> Items in worst order:
                              8
                             7
                          6
                         5
                      4
                     3
                  2
                 1
Random Case

tree = new BST ();
for (int i = 1; i <= 8; i++)
   tree.insert(random());




Output:           >>>> Items in random order:
                      X
                          U
                  P
                          O
                      H
                          F
                               B
Applications for BST
Sorting with binary search trees
  Input: unsorted array
  Output: sorted array




Algorithm ?
Running time ?
Better Search Trees


Prevent the degeneration of the BST :
  A BST can be set up to maintain balance during
  updating operations (insertions and removals)
  Types of ST which maintain the optimal performance:
     splay trees
     AVL trees
     2-4 Trees
     Red-Black trees
     B-trees

More Related Content

What's hot

Binary Search Tree and AVL
Binary Search Tree and AVLBinary Search Tree and AVL
Binary Search Tree and AVL
Katang Isip
 
Machine Learning
Machine LearningMachine Learning
Machine Learning
Ashwin P N
 
Effective Numerical Computation in NumPy and SciPy
Effective Numerical Computation in NumPy and SciPyEffective Numerical Computation in NumPy and SciPy
Effective Numerical Computation in NumPy and SciPy
Kimikazu Kato
 
Chapter 4
Chapter 4Chapter 4
Chapter 4
Radhika Puttewar
 
C Language Lecture 20
C Language Lecture 20C Language Lecture 20
C Language Lecture 20
Shahzaib Ajmal
 
Introduction to NumPy for Machine Learning Programmers
Introduction to NumPy for Machine Learning ProgrammersIntroduction to NumPy for Machine Learning Programmers
Introduction to NumPy for Machine Learning Programmers
Kimikazu Kato
 
Numpy tutorial(final) 20160303
Numpy tutorial(final) 20160303Numpy tutorial(final) 20160303
Numpy tutorial(final) 20160303
Namgee Lee
 
DSA (Data Structure and Algorithm) Questions
DSA (Data Structure and Algorithm) QuestionsDSA (Data Structure and Algorithm) Questions
DSA (Data Structure and Algorithm) Questions
RESHAN FARAZ
 
Trees (data structure)
Trees (data structure)Trees (data structure)
Trees (data structure)
Trupti Agrawal
 
Himpunan fuzzy i
Himpunan fuzzy iHimpunan fuzzy i
Himpunan fuzzy i
Muhamad Shobirin
 
Introducing R
Introducing RIntroducing R
Introducing R
nzfauna
 
1.5 binary search tree
1.5 binary search tree1.5 binary search tree
1.5 binary search tree
Krish_ver2
 
Introduction to NumPy (PyData SV 2013)
Introduction to NumPy (PyData SV 2013)Introduction to NumPy (PyData SV 2013)
Introduction to NumPy (PyData SV 2013)
PyData
 
Jan. 4 Function L1
Jan. 4 Function L1Jan. 4 Function L1
Jan. 4 Function L1RyanWatt
 
Numpy Talk at SIAM
Numpy Talk at SIAMNumpy Talk at SIAM
Numpy Talk at SIAM
Enthought, Inc.
 
17. Trees and Graphs
17. Trees and Graphs17. Trees and Graphs
17. Trees and Graphs
Intro C# Book
 
Binary Search Tree
Binary Search TreeBinary Search Tree
Binary Search Tree
Zafar Ayub
 

What's hot (20)

Binary Search Tree and AVL
Binary Search Tree and AVLBinary Search Tree and AVL
Binary Search Tree and AVL
 
Machine Learning
Machine LearningMachine Learning
Machine Learning
 
Effective Numerical Computation in NumPy and SciPy
Effective Numerical Computation in NumPy and SciPyEffective Numerical Computation in NumPy and SciPy
Effective Numerical Computation in NumPy and SciPy
 
Chapter 4
Chapter 4Chapter 4
Chapter 4
 
C Language Lecture 20
C Language Lecture 20C Language Lecture 20
C Language Lecture 20
 
Introduction to NumPy for Machine Learning Programmers
Introduction to NumPy for Machine Learning ProgrammersIntroduction to NumPy for Machine Learning Programmers
Introduction to NumPy for Machine Learning Programmers
 
Numpy tutorial(final) 20160303
Numpy tutorial(final) 20160303Numpy tutorial(final) 20160303
Numpy tutorial(final) 20160303
 
DSA (Data Structure and Algorithm) Questions
DSA (Data Structure and Algorithm) QuestionsDSA (Data Structure and Algorithm) Questions
DSA (Data Structure and Algorithm) Questions
 
Trees (data structure)
Trees (data structure)Trees (data structure)
Trees (data structure)
 
Himpunan fuzzy i
Himpunan fuzzy iHimpunan fuzzy i
Himpunan fuzzy i
 
Introducing R
Introducing RIntroducing R
Introducing R
 
Ch02
Ch02Ch02
Ch02
 
1.5 binary search tree
1.5 binary search tree1.5 binary search tree
1.5 binary search tree
 
Introduction to NumPy (PyData SV 2013)
Introduction to NumPy (PyData SV 2013)Introduction to NumPy (PyData SV 2013)
Introduction to NumPy (PyData SV 2013)
 
Binary Tree
Binary  TreeBinary  Tree
Binary Tree
 
Jan. 4 Function L1
Jan. 4 Function L1Jan. 4 Function L1
Jan. 4 Function L1
 
Recursion DM
Recursion DMRecursion DM
Recursion DM
 
Numpy Talk at SIAM
Numpy Talk at SIAMNumpy Talk at SIAM
Numpy Talk at SIAM
 
17. Trees and Graphs
17. Trees and Graphs17. Trees and Graphs
17. Trees and Graphs
 
Binary Search Tree
Binary Search TreeBinary Search Tree
Binary Search Tree
 

Viewers also liked

Discrete math mathematics for computer science 2
Discrete math   mathematics for computer science 2Discrete math   mathematics for computer science 2
Discrete math mathematics for computer science 2
Xavient Information Systems
 
Train name index
Train name indexTrain name index
Train name index
Xavient Information Systems
 

Viewers also liked (9)

16 mergesort
16 mergesort16 mergesort
16 mergesort
 
Discrete math mathematics for computer science 2
Discrete math   mathematics for computer science 2Discrete math   mathematics for computer science 2
Discrete math mathematics for computer science 2
 
Osi model
Osi modelOsi model
Osi model
 
Mobility in network
Mobility in networkMobility in network
Mobility in network
 
Train name index
Train name indexTrain name index
Train name index
 
Red black-trees-4
Red black-trees-4Red black-trees-4
Red black-trees-4
 
Lec14
Lec14Lec14
Lec14
 
Software engineering
Software engineeringSoftware engineering
Software engineering
 
Red Black Trees
Red Black TreesRed Black Trees
Red Black Trees
 

Similar to Binary searchtrees

Lecture 7-BinarySearchTrees.ppt
Lecture 7-BinarySearchTrees.pptLecture 7-BinarySearchTrees.ppt
Lecture 7-BinarySearchTrees.ppt
DrBashirMSaad
 
Unit14_BinarySearchTree.ppt
Unit14_BinarySearchTree.pptUnit14_BinarySearchTree.ppt
Unit14_BinarySearchTree.ppt
plagcheck
 
Binary tree
Binary treeBinary tree
Binary tree
Afaq Mansoor Khan
 
Materi Searching
Materi Searching Materi Searching
Materi Searching
BintangWijaya5
 
L 19 ct1120
L 19 ct1120L 19 ct1120
L 19 ct1120
Zia Ush Shamszaman
 
Write a C++ program that implements a binary search tree (BST) to man.pdf
Write a C++ program that implements a binary search tree (BST) to man.pdfWrite a C++ program that implements a binary search tree (BST) to man.pdf
Write a C++ program that implements a binary search tree (BST) to man.pdf
hardjasonoco14599
 
Trees in data structure
Trees in data structureTrees in data structure
Trees in data structure
Anusruti Mitra
 
(a) There are three ways to traverse a binary tree pre-order, in-or.docx
(a) There are three ways to traverse a binary tree pre-order, in-or.docx(a) There are three ways to traverse a binary tree pre-order, in-or.docx
(a) There are three ways to traverse a binary tree pre-order, in-or.docx
ajoy21
 
Binary Search Tree
Binary Search TreeBinary Search Tree
Binary Search Tree
sagar yadav
 
Topic19BinarySearchTrees.ppt
Topic19BinarySearchTrees.pptTopic19BinarySearchTrees.ppt
Topic19BinarySearchTrees.ppt
plagcheck
 
Binary search trees (1)
Binary search trees (1)Binary search trees (1)
Binary search trees (1)
Himadri Sen Gupta
 
Binary Search Tree
Binary Search TreeBinary Search Tree
Binary Search Tree
VijayaLakshmi506
 
Biary search Tree.docx
Biary search Tree.docxBiary search Tree.docx
Biary search Tree.docx
sowmya koneru
 
unit06-3-Trees.pdf
unit06-3-Trees.pdfunit06-3-Trees.pdf
unit06-3-Trees.pdf
AnmolYadav509681
 
Binary tree
Binary treeBinary tree
Binary tree
Maria Saleem
 
Tree and binary tree
Tree and binary treeTree and binary tree
Tree and binary tree
Zaid Shabbir
 
Lecture_10 - Revised.pptx
Lecture_10 - Revised.pptxLecture_10 - Revised.pptx
Lecture_10 - Revised.pptx
RedHeart11
 
Given a newly created Binary Search Tree with the following numerica.pdf
Given a newly created Binary Search Tree with the following numerica.pdfGiven a newly created Binary Search Tree with the following numerica.pdf
Given a newly created Binary Search Tree with the following numerica.pdf
hadpadrrajeshh
 

Similar to Binary searchtrees (20)

Lecture 7-BinarySearchTrees.ppt
Lecture 7-BinarySearchTrees.pptLecture 7-BinarySearchTrees.ppt
Lecture 7-BinarySearchTrees.ppt
 
Unit14_BinarySearchTree.ppt
Unit14_BinarySearchTree.pptUnit14_BinarySearchTree.ppt
Unit14_BinarySearchTree.ppt
 
Binary tree
Binary treeBinary tree
Binary tree
 
Materi Searching
Materi Searching Materi Searching
Materi Searching
 
L 19 ct1120
L 19 ct1120L 19 ct1120
L 19 ct1120
 
Write a C++ program that implements a binary search tree (BST) to man.pdf
Write a C++ program that implements a binary search tree (BST) to man.pdfWrite a C++ program that implements a binary search tree (BST) to man.pdf
Write a C++ program that implements a binary search tree (BST) to man.pdf
 
Trees in data structure
Trees in data structureTrees in data structure
Trees in data structure
 
(a) There are three ways to traverse a binary tree pre-order, in-or.docx
(a) There are three ways to traverse a binary tree pre-order, in-or.docx(a) There are three ways to traverse a binary tree pre-order, in-or.docx
(a) There are three ways to traverse a binary tree pre-order, in-or.docx
 
Binary Search Tree
Binary Search TreeBinary Search Tree
Binary Search Tree
 
Topic19BinarySearchTrees.ppt
Topic19BinarySearchTrees.pptTopic19BinarySearchTrees.ppt
Topic19BinarySearchTrees.ppt
 
Binary search trees (1)
Binary search trees (1)Binary search trees (1)
Binary search trees (1)
 
08 binarysearchtrees 1
08 binarysearchtrees 108 binarysearchtrees 1
08 binarysearchtrees 1
 
Binary Search Tree
Binary Search TreeBinary Search Tree
Binary Search Tree
 
611+tutorial
611+tutorial611+tutorial
611+tutorial
 
Biary search Tree.docx
Biary search Tree.docxBiary search Tree.docx
Biary search Tree.docx
 
unit06-3-Trees.pdf
unit06-3-Trees.pdfunit06-3-Trees.pdf
unit06-3-Trees.pdf
 
Binary tree
Binary treeBinary tree
Binary tree
 
Tree and binary tree
Tree and binary treeTree and binary tree
Tree and binary tree
 
Lecture_10 - Revised.pptx
Lecture_10 - Revised.pptxLecture_10 - Revised.pptx
Lecture_10 - Revised.pptx
 
Given a newly created Binary Search Tree with the following numerica.pdf
Given a newly created Binary Search Tree with the following numerica.pdfGiven a newly created Binary Search Tree with the following numerica.pdf
Given a newly created Binary Search Tree with the following numerica.pdf
 

Binary searchtrees

  • 1. CSCE 3110 Data Structures & Algorithm Analysis Rada Mihalcea http://www.cs.unt.edu/~rada/CSCE3110 Binary Search Trees Reading: Chap. 4 (4.3) Weiss
  • 2. A Taxonomy of Trees General Trees – any number of children / node Binary Trees – max 2 children / node Heaps – parent < (>) children Binary Search Trees
  • 3. Binary Trees Binary search tree Every element has a unique key. The keys in a nonempty left subtree (right subtree) are smaller (larger) than the key in the root of subtree. The left and right subtrees are also binary search trees.
  • 4. Binary Search Trees Binary Search Trees (BST) are a type of Binary Trees with a special organization of data. This data organization leads to O(log n) complexity for searches, insertions and deletions in certain types of the BST (balanced trees). O(h) in general
  • 5. Binary Search Algorithm Binary Search algorithm of an array of sorted items reduces the search space by one half after each comparison 34 41 56 63 72 89 95 0 1 2 3 4 5 6 34 41 56 72 89 95 0 1 2 4 5 6 72 95 34 56 4 6 0 2
  • 6. Organization Rule for BST • the values in all nodes in the left subtree of a node are less than the node value • the values in all nodes in the right subtree of a node are greater 63 than the node values 41 89 72 95 34 56
  • 7. Binary Tree typedef struct tnode *ptnode; typedef struct node { short int key; ptnode right, left; };
  • 8. BST Operations: Search Searching in the BST method search(key) • implements the binary search based on comparison of the items in the tree • the items in the BST must be comparable (e.g integers, string, etc.) The search starts at the root. It probes down, comparing the values in each node with the target, till it finds the first item equal to the target. Returns this item or null if there is none.
  • 9. Search in BST - Pseudocode if the tree is empty return NULL else if the item in the node equals the target return the node value else if the item in the node is greater than the target return the result of searching the left subtree else if the item in the node is smaller than the target return the result of searching the right subtree
  • 10. Search in a BST: C code Ptnode search(ptnode root, int key) { /* return a pointer to the node that contains key. If there is no such node, return NULL */ if (!root) return NULL; if (key == root->key) return root; if (key < root->key) return search(root->left,key); return search(root->right,key); }
  • 11. BST Operations: Insertion method insert(key)  places a new item near the frontier of the BST while retaining its organization of data: starting at the root it probes down the tree till it finds a node whose left or right pointer is empty and is a logical place for the new value uses a binary search to locate the insertion point is based on comparisons of the new item and values of nodes in the BST Elements in nodes must be comparable!
  • 12. Case 1: The Tree is Empty Set the root to a new node containing the item Case 2: The Tree is Not Empty Call a recursive helper method to insert the item 10 > 7 10 7 5 9 10 > 9 4 6 8 10
  • 13. Insertion in BST - Pseudocode if tree is empty create a root node with the new key else compare key with the top node if key = node key replace the node with the new value else if key > node key compare key with the right subtree: if subtree is empty create a leaf node else add key in right subtree else key < node key compare key with the left subtree: if the subtree is empty create a leaf node else add key to the left subtree
  • 14. Insertion into a BST: C code void insert (ptnode *node, int key) { ptnode ptr, temp = search(*node, key); if (temp || !(*node)) { ptr = (ptnode) malloc(sizeof(tnode)); if (IS_FULL(ptr)) { fprintf(stderr, “The memory is fulln”); exit(1); } ptr->key = key; ptr->left = ptr->right = NULL; if (*node) if (key<temp->key) temp->left=ptr; else temp->right = ptr; else *node = ptr; } }
  • 15. BST Shapes  The order of supplying the data determines where it is placed in the BST , which determines the shape of the BST  Create BSTs from the same set of data presented each time in a different order: a) 17 4 14 19 15 7 9 3 16 10 b) 9 10 17 4 3 7 14 16 15 19 c) 19 17 16 15 14 10 9 7 4 3 can you guess this shape?
  • 16. BST Operations: Removal  removes a specified item from the BST and adjusts the tree  uses a binary search to locate the target item:  starting at the root it probes down the tree till it finds the target or reaches a leaf node (target not in the tree) removal of a node must not leave a ‘gap’ in the tree,
  • 17. Removal in BST - Pseudocode method remove (key) I if the tree is empty return false II Attempt to locate the node containing the target using the binary search algorithm if the target is not found return false else the target is found, so remove its node: Case 1: if the node has 2 empty subtrees replace the link in the parent with null Case 2: if the node has a left and a right subtree - replace the node's value with the max value in the left subtree - delete the max node in the left subtree
  • 18. Removal in BST - Pseudocode Case 3: if the node has no left child - link the parent of the node - to the right (non-empty) subtree Case 4: if the node has no right child - link the parent of the target - to the left (non-empty) subtree
  • 19. Removal in BST: Example Case 1: removing a node with 2 EMPTY SUBTREES parent 7 cursor 5 9 4 6 8 10 7 Removing 4 replace the link in the 5 9 parent with null 6 8 10
  • 20. Removal in BST: Example Case 2: removing a node with 2 SUBTREES - replace the node's value with the max value in the left subtree - delete the max node in the left subtree What other element Removing 7 can be used as cursor replacement? cursor 7 6 5 9 5 9 4 6 8 10 4 8 10
  • 21. Removal in BST: Example Case 3: removing a node with 1 EMPTY SUBTREE the node has no left child: link the parent of the node to the right (non-empty) subtree parent parent 7 7 cursor 5 9 cursor 5 9 6 8 10 6 8 10
  • 22. Removal in BST: Example Case 4: removing a node with 1 EMPTY SUBTREE the node has no right child: link the parent of the node to the left (non-empty) subtree Removing 5 parent parent 7 cursor 7 cursor 5 9 5 9 4 8 10 4 8 10
  • 23. Analysis of BST Operations The complexity of operations get, insert and remove in BST is O(h) , where h is the height. O(log n) when the tree is balanced. The updating operations cause the tree to become unbalanced. The tree can degenerate to a linear shape and the operations will become O (n)
  • 24. Best Case BST tree = new BST(); tree.insert ("E"); tree.insert ("C"); tree.insert ("D"); tree.insert ("A"); tree.insert ("H"); tree.insert ("F"); tree.insert ("K"); >>>> Items in advantageous order: Output: K H F E D C A
  • 25. Worst Case BST tree = new BST(); for (int i = 1; i <= 8; i++) tree.insert (i); Output: >>>> Items in worst order: 8 7 6 5 4 3 2 1
  • 26. Random Case tree = new BST (); for (int i = 1; i <= 8; i++) tree.insert(random()); Output: >>>> Items in random order: X U P O H F B
  • 27. Applications for BST Sorting with binary search trees Input: unsorted array Output: sorted array Algorithm ? Running time ?
  • 28. Better Search Trees Prevent the degeneration of the BST : A BST can be set up to maintain balance during updating operations (insertions and removals) Types of ST which maintain the optimal performance: splay trees AVL trees 2-4 Trees Red-Black trees B-trees