COMP9024 ♢ Week 02b ♢Search Tree Data Structures ♢ (22T0)

COMP9024 (22T0) ♢ Week 02b ♢ Search Tree Data Structures ♢ [0/49]
❖ Searching

An extremely common application in computing

Applications:  Google,  databases, .....
COMP9024 (22T0) ♢ Week 02b ♢ Search Tree Data Structures ♢ [1/49]
❖ ... Searching

Since searching is a very important/frequent operation,
many approaches have been developed to do it

Linear structures: arrays, linked lists, files

Arrays = random access. Lists, files = sequential access.

Cost of searching:

Array ListFile
Unsorted O(n)
(linear scan)
O(n)
(linear scan)
O(n)
(linear scan)
Sorted O(log n)
(binary search)
O(n)
(linear scan)
O(log n)
(seek, seek>, …)

Also (cf. COMP9021): hash tables   (O(1), but only under optimal conditions)
COMP9024 (22T0) ♢ Week 02b ♢ Search Tree Data Structures ♢ [2/49]
❖ ... Searching

Maintaining the order in sorted arrays and files is a costly operation.

Search trees are as efficient to search but more efficient to maintain.


Example: the following tree corresponds to the sorted array [2,5,10,12,14,17,20,24,29,30,31,32]:

[Diagram:Pic/bigtree.png]

COMP9024 (22T0) ♢ Week 02b ♢ Search Tree Data Structures ♢ [3/49]
❖ Tree Data Structures

COMP9024 (22T0) ♢ Week 02b ♢ Search Tree Data Structures ♢ [4/49]
❖ Trees

Trees are connected graphs


[Diagram:Pic/tree.png]

COMP9024 (22T0) ♢ Week 02b ♢ Search Tree Data Structures ♢ [5/49]
❖ ... Trees

Trees are used in many contexts, e.g.


[Diagram:Pic/trees.png]

COMP9024 (22T0) ♢ Week 02b ♢ Search Tree Data Structures ♢ [6/49]
❖ ... Trees

Trees can be used as a data structure, but also for illustration.

E.g. showing evaluation of a prefix arithmetic expression

[Diagram:Pic/tree-expr-eval.png]

COMP9024 (22T0) ♢ Week 02b ♢ Search Tree Data Structures ♢ [7/49]
❖ ... Trees

Binary trees (k=2 children per node) can be defined recursively, as follows:

A binary tree is either


[Diagram:Pic/subtrees.png]

COMP9024 (22T0) ♢ Week 02b ♢ Search Tree Data Structures ♢ [8/49]
❖ ... Trees

Other special kinds of tree

COMP9024 (22T0) ♢ Week 02b ♢ Search Tree Data Structures ♢ [9/49]
❖ Search Trees

COMP9024 (22T0) ♢ Week 02b ♢ Search Tree Data Structures ♢ [10/49]
❖ Binary Search Trees

Binary search trees (or BSTs) have the characteristic properties

perfectly balanced trees have the properties

[Diagram:Pic/three-bin-search-trees.png]

COMP9024 (22T0) ♢ Week 02b ♢ Search Tree Data Structures ♢ [11/49]
❖ ... Binary Search Trees

Operations on BSTs:

Notes:
COMP9024 (22T0) ♢ Week 02b ♢ Search Tree Data Structures ♢ [12/49]
❖ ... Binary Search Trees

Examples of binary search trees:

[Diagram:Pic/binary-search-trees.png]

Shape of tree is determined by order of insertion.

COMP9024 (22T0) ♢ Week 02b ♢ Search Tree Data Structures ♢ [13/49]
❖ ... Binary Search Trees

Level of node = path length from root to node

Height (or: depth) of tree = max path length from root to leaf

[Diagram:Pic/tree-depth.png]

Height-balanced tree:  ∀ nodes: height(left subtree) = height(right subtree)

Time complexity of tree algorithms is typically O(height)

COMP9024 (22T0) ♢ Week 02b ♢ Search Tree Data Structures ♢ [14/49]
❖ Exercise : Insertion into BSTs

For each of the sequences below


(a)   4   2   6   5   1   7   3

(b)   6   5   2   3   4   7   1

(c)   1   2   3   4   5   6   7


Assume new values are always inserted as new leaf nodes

COMP9024 (22T0) ♢ Week 02b ♢ Search Tree Data Structures ♢ [15/49]

(a) the balanced tree from 3 slides ago (height = 2)

(b) the non-balanced tree from 3 slides ago (height = 4)

(c) a fully degenerate tree of height 6

COMP9024 (22T0) ♢ Week 02b ♢ Search Tree Data Structures ♢ [16/49]
❖ Representing BSTs

Binary trees are typically represented by node structures

Most tree algorithms move down the tree.
If upward movement needed, add a pointer to parent.

[Diagram:Pic/tree-rep.png]

COMP9024 (22T0) ♢ Week 02b ♢ Search Tree Data Structures ♢ [17/49]
❖ ... Representing BSTs

Typical data structures for trees …

// a Tree is represented by a pointer to its root node
typedef struct Node *Tree;

// a Node contains its data, plus left and right subtrees
typedef struct Node {
   int  data;
   Tree left, right;
} Node;

// some macros that we will use frequently
#define data(tree)  ((tree)->data)
#define left(tree)  ((tree)->left)
#define right(tree) ((tree)->right)

We ignore items   ⇒ data in Node is just a key

COMP9024 (22T0) ♢ Week 02b ♢ Search Tree Data Structures ♢ [18/49]
❖ ... Representing BSTs

Abstract data vs concrete data …

[Diagram:Pic/concrete.png]

COMP9024 (22T0) ♢ Week 02b ♢ Search Tree Data Structures ♢ [19/49]
❖ Tree Algorithms

COMP9024 (22T0) ♢ Week 02b ♢ Search Tree Data Structures ♢ [20/49]
❖ Searching in BSTs

Most tree algorithms are best described recursively

TreeSearch(tree,item):
|  Input  tree, item
|  Output true if item found in tree, false otherwise
|
|  if tree is empty then
|     return false
|  else if item < data(tree) then
|     return TreeSearch(left(tree),item)
|  else if item > data(tree) then
|     return TreeSearch(right(tree),item)
|  else         // found
|    return true
|  end if

COMP9024 (22T0) ♢ Week 02b ♢ Search Tree Data Structures ♢ [21/49]
❖ Insertion into BSTs

Insert an item into appropriate subtree

insertAtLeaf(tree,item):
|  Input  tree, item
|  Output tree with item inserted
|
|  if tree is empty then
|     return new node containing item
|  else if item < data(tree) then
|     return insertAtLeaf(left(tree),item)
|  else if item > data(tree) then
|     return insertAtLeaf(right(tree),item)
|  else
|     return tree    // avoid duplicates
|  end if

COMP9024 (22T0) ♢ Week 02b ♢ Search Tree Data Structures ♢ [22/49]
❖ Tree Traversal

Iteration (traversal) on …

For binary Trees, several well-defined visiting orders exist:
COMP9024 (22T0) ♢ Week 02b ♢ Search Tree Data Structures ♢ [23/49]
❖ ... Tree Traversal

Consider "visiting" an expression tree like:

[Diagram:Pic/tree1.png]

NLR:  + * 1 3 - * 5 7 9    (prefix-order: useful for building tree)
LNR:  1 * 3 + 5 * 7 - 9    (infix-order: "natural" order)
LRN:  1 3 * 5 7 * 9 - +    (postfix-order: useful for evaluation)
Level:  + * - 1 3 * 9 5 7    (level-order: useful for printing tree)

COMP9024 (22T0) ♢ Week 02b ♢ Search Tree Data Structures ♢ [24/49]
❖ Exercise : Tree Traversal

Show NLR, LNR, LRN traversals for the tree

[Diagram:Pic/bigtree.png]

COMP9024 (22T0) ♢ Week 02b ♢ Search Tree Data Structures ♢ [25/49]
NLR (preorder):   20   10   5   2   14   12   17   30   24   29   32   31

LNR (inorder):   2   5   10   12   14   17   20   24   29   30   31   32

LRN (postorder):   2   5   12   17   14   10   29   24   31   32   30   20

COMP9024 (22T0) ♢ Week 02b ♢ Search Tree Data Structures ♢ [26/49]
❖ Exercise : Non-recursive traversals

Write a non-recursive preorder traversal algorithm.

Assume that you have a stack ADT available.

COMP9024 (22T0) ♢ Week 02b ♢ Search Tree Data Structures ♢ [27/49]

showBSTreePreorder(t):
|  Input tree t
|
|  push t onto new stack S
|  while stack is not empty do
|  |  t=pop(S)
|  |  print data(t)
|  |  if right(t) is not empty then
|  |     push right(t) onto S
|  |  end if
|  |  if left(t) is not empty then
|  |     push left(t) onto S
|  |  end if
|  end while

COMP9024 (22T0) ♢ Week 02b ♢ Search Tree Data Structures ♢ [28/49]
❖ Joining Two Trees

An auxiliary tree operation …

Tree operations so far have involved just one tree.

An operation on two trees:   t = joinTrees(t1,t2)

COMP9024 (22T0) ♢ Week 02b ♢ Search Tree Data Structures ♢ [29/49]
❖ ... Joining Two Trees

Method for performing tree-join:

Advantage: doesn't increase height of tree significantly

x ≤ height(t) ≤ x+1, where x = max(height(t1),height(t2))

Variation: choose deeper subtree; take root from there.

COMP9024 (22T0) ♢ Week 02b ♢ Search Tree Data Structures ♢ [30/49]
❖ ... Joining Two Trees

Joining two trees:

[Diagram:Pic/join-op.png]

 

Note: t2' may be less deep than t2

COMP9024 (22T0) ♢ Week 02b ♢ Search Tree Data Structures ♢ [31/49]
❖ ... Joining Two Trees

Implementation of tree-join

joinTrees(t1,t2):
|  Input  trees t1,t2
|  Output t1 and t2 joined together
|
|  if t1 is empty then return t2
|  else if t2 is empty then return t1
|  else
|  |  curr=t2, parent=NULL
|  |  while left(curr) is not empty do     // find min element in t2
|  |     parent=curr
|  |     curr=left(curr)
|  |  end while
|  |  if parent≠NULL then
|  |     left(parent)=right(curr)  // unlink min element from parent
|  |     right(curr)=t2
|  |  end if
|  |  left(curr)=t1
|  |  return curr                  // curr is new root
|  end if

COMP9024 (22T0) ♢ Week 02b ♢ Search Tree Data Structures ♢ [32/49]
❖ Exercise : Joining Two Trees

Join the trees

[Diagram:Pic/joinTrees.png]

COMP9024 (22T0) ♢ Week 02b ♢ Search Tree Data Structures ♢ [33/49]

[Diagram:Pic/joinTreesSoln.png]

COMP9024 (22T0) ♢ Week 02b ♢ Search Tree Data Structures ♢ [34/49]
❖ Deletion from BSTs

Insertion into a binary search tree is easy.

Deletion from a binary search tree is harder.

Four cases to consider …

COMP9024 (22T0) ♢ Week 02b ♢ Search Tree Data Structures ♢ [35/49]
❖ ... Deletion from BSTs

Case 2: item to be deleted is a leaf (zero subtrees)

[Diagram:Pic/del-k.png]

Just delete the item

COMP9024 (22T0) ♢ Week 02b ♢ Search Tree Data Structures ♢ [36/49]
❖ ... Deletion from BSTs

Case 2: item to be deleted is a leaf (zero subtrees)

[Diagram:Pic/del1-k.png]

COMP9024 (22T0) ♢ Week 02b ♢ Search Tree Data Structures ♢ [37/49]
❖ ... Deletion from BSTs

Case 3: item to be deleted has one subtree

[Diagram:Pic/del-p.png]

Replace the item by its only subtree

COMP9024 (22T0) ♢ Week 02b ♢ Search Tree Data Structures ♢ [38/49]
❖ ... Deletion from BSTs

Case 3: item to be deleted has one subtree

[Diagram:Pic/del1-p.png]

COMP9024 (22T0) ♢ Week 02b ♢ Search Tree Data Structures ♢ [39/49]
❖ ... Deletion from BSTs

Case 4: item to be deleted has two subtrees

[Diagram:Pic/del0-m.png]

Version 1: right child becomes new root, attach left subtree to min element of right subtree

COMP9024 (22T0) ♢ Week 02b ♢ Search Tree Data Structures ♢ [40/49]
❖ ... Deletion from BSTs

Case 4: item to be deleted has two subtrees

[Diagram:Pic/del1-m.png]

COMP9024 (22T0) ♢ Week 02b ♢ Search Tree Data Structures ♢ [41/49]
❖ ... Deletion from BSTs

Case 4: item to be deleted has two subtrees

[Diagram:Pic/del-m.png]

Version 2: join left and right subtree

COMP9024 (22T0) ♢ Week 02b ♢ Search Tree Data Structures ♢ [42/49]
❖ ... Deletion from BSTs

Case 4: item to be deleted has two subtrees

[Diagram:Pic/del2-m.png]

COMP9024 (22T0) ♢ Week 02b ♢ Search Tree Data Structures ♢ [43/49]
❖ ... Deletion from BSTs

Pseudocode (version 2 for case 4)

TreeDelete(t,item):
|  Input  tree t, item
|  Output t with item deleted
|
|  if t is not empty then          // nothing to do if tree is empty
|  |  if item < data(t) then       // delete item in left subtree
|  |     left(t)=TreeDelete(left(t),item)
|  |  else if item > data(t) then  // delete item in right subtree
|  |     right(t)=TreeDelete(right(t),item)
|  |  else                         // node 't' must be deleted
|  |  |  if left(t) and right(t) are empty then 
|  |  |     new=empty tree                   // 0 children
|  |  |  else if left(t) is empty then
|  |  |     new=right(t)                     // 1 child
|  |  |  else if right(t) is empty then
|  |  |     new=left(t)                      // 1 child
|  |  |  else
|  |  |     new=joinTrees(left(t),right(t))  // 2 children
|  |  |  end if
|  |  |  free memory allocated for t
|  |  |  t=new
|  |  end if
|  end if
|  return t

COMP9024 (22T0) ♢ Week 02b ♢ Search Tree Data Structures ♢ [44/49]
❖ Application of BSTs: Sets

Trees provide efficient search.

Sets require efficient search

Logical to implement a set ADT via BSTree
COMP9024 (22T0) ♢ Week 02b ♢ Search Tree Data Structures ♢ [45/49]
❖ ... Application of BSTs: Sets

Assuming we have Tree implementation

then Set implementation is
COMP9024 (22T0) ♢ Week 02b ♢ Search Tree Data Structures ♢ [46/49]
❖ ... Application of BSTs: Sets

[Diagram:Pic/set-tree.png]

COMP9024 (22T0) ♢ Week 02b ♢ Search Tree Data Structures ♢ [47/49]
❖ ... Application of BSTs: Sets

Concrete representation:

#include <BSTree.h>

typedef struct SetRep {
   int   nelems;
   Tree  root;
} SetRep;

typedef Set *SetRep;

Set newSet() {
   Set S = malloc(sizeof(SetRep));
   assert(S != NULL);
   S->nelems = 0;
   S->root = newTree();
   return S;
}

COMP9024 (22T0) ♢ Week 02b ♢ Search Tree Data Structures ♢ [48/49]
❖ Summary


COMP9024 (22T0) ♢ Week 02b ♢ Search Tree Data Structures ♢ [49/49]


Produced: 20 Dec 2021