Docsity

Binary Search Trees (BST) - CSE 3318 Algorithms and Data Structures, Slides of Data Structures and Algorithms

Partial preview of the text

Download Binary Search Trees (BST) - CSE 3318 Algorithms and Data Structures and more Slides Data Structures and Algorithms in PDF only on Docsity!

Binary Search Trees

(BST)

CSE 3318 – Algorithms and Data Structures

Alexandra Stefan

Includes materials from Dr. Bob Weems

University of Texas at Arlington

3/22/2022 1

Search Trees

  • "search tree" as a term does NOT

refer to a specific implementation.

  • The term refers to a family of

implementations , that may have different properties.

  • We will discuss:
    • Binary search trees (BST).
    • 2 - 3 - 4 trees (a special type of a B-

tree).

  • mention: red-black trees, AVL trees,

splay trees, B-trees and other

variations.

2

  • All search trees support: search , insert and delete.
  • Insertions and deletions can differ among trees, and have important implications on overall performance.
  • The main goal is to have insertions and deletions that: - Are efficient (at most logarithmic

time).

  • Leave the tree balanced , to

support efficient search (at most

logarithmic time).

  • Preserve the tree properties (restore the tree)

Tree Properties - Review

• Full tree

• Complete tree (e.g. heap tree)

• Perfect binary tree

• Alternative definitions: complete (for perfect) and almost complete or nearly

complete (complete).

• Tree – connected graph with no cycles, or connected graph with N- 1

edges (and N vertices).

4

Binary Search Trees

• Definition: a binary search tree is a binary tree where

the item at each node is:

– Greater than or equal to all items on the left subtree.

– Less than all items in the right subtree. (Use ≤ to create

more balanced trees with duplicate values.)

• How do we search?

5

40

23 52

(^15 37 )

typedef struct node * nodePT; struct node { int data; nodePT left; nodePT right; };

Example 1

• What values could the empty leaf have?

• Only value 40

7

40

40 52

(^15) (^40) 44

Example 1

• If you change direction twice, (go to A, left child, and

then go to X, right child) all the nodes in the subtree

rooted X will be in the range [A,B].

8

40

40 52

(^15) (^40) 44

B

A 52

(^15) X 44

A ≤ X ≤ B

Properties

(tree image: Dr. Bob Weems: Notes 11, parts: ‘11.C. Binary Search Trees’ )

10

• Where is the item with the smallest key?

• Where is the item with the largest key?

• What traversal prints the data in increasing order?

  • How about decreasin g order?

Consider the special

cases where the root has:

- No left child

- No right child

Predecessor and Successor

(according to key order)

(tree image: Dr. Bob Weems: Notes 11, parts: ‘11.C. Binary Search Trees’ )

11

Node Predecessor Successor

  • When the node has the child you need.
  • When the node does NOT have he child you need.

Here, in the nodes, the first number is the item/key and the second number is the tree size. Root has key 120 and size 21 (the whole tree has 21 nodes).

  • Min: leftmost node (from the root keep going left)
    • Special case: no left child => root
  • Max: rightmost node (from the root keep going right.
    • Special case: no right child => root
  • Print in order:
    • Increasing: Left, Root, Right (inorder traversal)
    • Decreasing: Right, Root, Left
  • Successor of node x with key k (go right):
    • Smallest node in the right subtree
    • Special case: no right subtree: first parent to the right
  • Predecessor of node x with key k (go left):
    • Largest node in the left subtree
    • Special case: no left subtree: first parent to the left 13

Binary Search Trees - Search

nodePT search(nodePT tree, int s_data) {

if (tree == NULL) return NULL; else if (s_data == tree->data) return tree; else if (s_data < tree->data) return search(tree->left, s_data); else return search(tree->right, s_data); }

14

Runtime

(in terms of ,N, number of nodes in the tree or tree height)

  • Best case:
  • Worst case:

40

23 52

(^15 37 )

typedef struct node * nodePT; struct node { int data; nodePT left; nodePT right; };

Performance of BST

• Are these trees valid BST?

• Give two sequences of nodes s.t. when inserted in an

empty tree will produce the two trees shown here

(each sequence produces a different tree).

16

40

23 52

(^15 37 )

40

23

52

15

37

44

Performance of BST

• Are these trees valid BST?

  • Yes

• Give two sequences of nodes s.t. when inserted in an

empty tree will produce the two trees shown here

(each sequence produces a different tree).

  • 40, 23, 37, 52, 44, 15
  • 15, 23, 37, 40, 44, 52

17

40

23 52

(^15 37 )

40

23

52

15

37

44

Search, Insert and Delete take time linear to the height of the tree (worst).

Ideal: build and keep a balanced tree

  • insertions and deletions should leave

the tree balanced.

  • Min: leftmost node (from the root keep going left)
    • O(___)
  • Max: rightmost node (from the root keep going right).
    • O(___)
  • Print in order:
    • Increasing: Left, Root, Right (inorder traversal)
    • Decreasing: Right, Root, Left
    • O(______) can we give Theta? Θ(_____)
  • Successor of node x with key k (go right):
    • O(___)
  • Predecessor of node x with key k (go left):
    • O(___)
  • Search for a value (and not found)
    • O(___)
  • Build the tree via N repeated insertions:
    • O(___) Best: Θ(_____) Worst: Θ(_____)
  • Space complexity? Θ(_____) (^19)

Time and Space complexity for a tree with N nodes

Predecessor and Successor

(according to key order)

(tree image: Dr. Bob Weems: Notes 11, parts: ‘11.C. Binary Search Trees’ )

20

Node Predecessor Successor

  • When the node has the child you need.
  • When the node does NOT have he child you need.

Here, in the nodes, the first number is the item/key and the second number is the tree size. Root has key 120 and size 21 (the whole tree has 21 nodes).