Docsity

Introduction to Mergesort and Binary Search Trees in CPSC 223, Slides of Data Structures and Algorithms

Partial preview of the text

Download Introduction to Mergesort and Binary Search Trees in CPSC 223 and more Slides Data Structures and Algorithms in PDF only on Docsity!

CPSC 223

Algorithms & Data Abstract Structures

Lecture 13:

Mergesort exercise

Binary Search Trees (intro)

Today …

• Homework

  • Homework 5 due today
  • Homework 6

• Sorting Exercises (Mergesort)

• Start Binary Search Trees

  • Ch 10: pp. 499-516, 519- CPSC 223 -­‐-­‐ Fall 2010 2

Mergesort (based on textbook)

void Mergesort (Entry theArray[], int n, int first, int last) { if(first < last) { int mid = (first + last) / 2; Mergesort (theArray, n, first, mid); Mergesort (theArray, n, mid + 1, last); Merge (theArray, n, first, mid, last); } } CPSC 223 -­‐-­‐ Fall 2010 3

Mergesort (based on textbook)

void Merge (Entry theArray[], int n, int first, int mid, int last) { Entry tmpArray[n]; int first1 = first, first2 = mid + 1, i = first1; for(; first1 <= mid && first2 <= last; i++) { if(theArray[first1] < theArray[first2]) tmpArray[i] = theArray[first1++]; else tmpArray[i] = theArray[first2++]; } for(; first1 <= mid; i++, first1++) // copy remaining from first half tmpArray[i] = theArray[first1]; for(; first2 <= last; i++, first2++) // copy remaining from sec. half tmpArray[i] = theArray[first2]; for(int j = first; j <= last; j++) // copy tmpArray to the array theArray[j] = tmpArray[j]; } CPSC 223 -­‐-­‐ Fall 2010 4

Tree Structures

Trees are used to represent different kinds of relationships

CPSC 223 -­‐-­‐ Fall 2010 7 B R V S C For example, inheritance hierarchies (taxonomies, ontologies, …) M Vehicle Bicycle Mountain Bike Road Bike Car Sports Car ÷ 2

5 Algebraic expressions (evaluaVon order) × 3 4

Tree Structures

CPSC 223 -­‐-­‐ Fall 2010 8 E Y C G N NesVng (including “has a” relaVonships) N Company Employee Name #Years Name Gonzaga J John 15 Sort order A B C D E F G

Trees are used to represent different kinds of relationships

Trees (Terminology)

In general, a tree forms a hierarchy consisting of

  • Zero or more nodes
  • A distinguished “ root ” node (no parents)
  • Structures of multiple trees are called “ forests ” CPSC 223 -­‐-­‐ Fall 2010 9 the root A B C D E F G

Trees (Terminology)

Nodes are arranged in parent-child relationships

  • The parent of a node N is directly above N
  • The child of a node N is directly below N CPSC 223 -­‐-­‐ Fall 2010 10 a child of node D A B C D E F G a parent of node B

Trees (Terminology)

Parent-child relationships induce ancestor-descendent

relationships

  • An ancestor of a node N is either the parent of N or the parent of an ancestor of N
  • The root is the ancestor of every node in the tree
  • A descendent of a node N is either a child of N or a child of a descendent of N CPSC 223 -­‐-­‐ Fall 2010 13 a descendent of D (^) A B C D E F G an ancestor of A A descendent of a node lies on a path from the node to a leaf

Trees (Terminology)

Nodes can have zero or more “ siblings ”

  • The siblings of N are the nodes with the same parent as N CPSC 223 -­‐-­‐ Fall 2010 14 A B C D E F G sibling of F sibling of B

Trees (Terminology)

A “ subtree ” is a tree rooted at a descendent of the root

  • A subtree includes all children and descendents of the subtree “root” N
  • We often say a subtree is rooted at N CPSC 223 -­‐-­‐ Fall 2010 15 A B C D E F G subtree rooted at B subtree rooted at G

Trees (Terminology)

In a “ binary ” tree, every node has at most 2 children

More formally, T is a binary tree if

  • T has no nodes (is empty), or
  • T has the form
  • where r is a node, and TL and TR are binary trees
  • TL is the left subtree of r and TR is the right subtree of r … note that this definition employs recursion! (this will be helpful as we compute over trees) CPSC 223 -­‐-­‐ Fall 2010 16 r TL TR

Trees (Terminology)

A “ full ” binary tree has a height h with no missing nodes

  • i.e., every internal node has exactly 2 children CPSC 223 -­‐-­‐ Fall 2010 19 A B C D E F G this is a full binary tree

Trees (Terminology)

A “ complete ” binary tree of height h is

  • a full binary tree at height h – 1, and
  • the nodes at height h are filled in from left to right … sometimes we say “ level ” h to mean the nodes at height h CPSC 223 -­‐-­‐ Fall 2010 20 A B C D E this is a complete binary tree F

Trees (Terminology)

A “ balanced ” binary tree has for every node

  • left and right subtrees that differ in height by at most 1 CPSC 223 -­‐-­‐ Fall 2010 21 A B C D E F this is not a balanced binary tree this is a balanced binary tree A B C D E F E

Binary Tree versus Binary Search Tree (BST)

  • A Binary Search Tree stipulates where nodes are

placed in a Binary Tree …

  • In such a way as to maintain the items in sorted

order (based on a sort key)

CPSC 223 -­‐-­‐ Fall 2010 22

Searching for nodes in a BST

Given a key (i.e., a word) to search for …

  • Start at the root node
    • Is the item at the root node equal to search key? If Yes … Found match!
    • Is the item at the root node greater than the search key? If Yes … Search the left subtree of the root
    • Is the item at the root node less than the search key? If Yes … Search the right subtree of the root CPSC 223 -­‐-­‐ Fall 2010 25

Inserting Nodes

Basic idea

  • Find where the node should be ( search )
  • And then insert the node at that location
  • Will always insert into a leaf node

Order of insertions determines the shape of the tree!

CPSC 223 -­‐-­‐ Fall 2010 26 D B G A C E H F

Inserting Nodes

Insert:

1. D

2. B

3. G

4. A

5. C

6. E

7. F

8. H

CPSC 223 -­‐-­‐ Fall 2010 27 D B G A C E H F

Inserting Nodes

  • Insert:
    1. A
    2. B
    3. C
    4. D
    5. E
    6. F
    7. G

What happens?

CPSC 223 -­‐-­‐ Fall 2010 28 A B C D E F G