binary search trees -...

28
Binary Search Trees 1 Sorting Numbers using a Tree a sorting algorithm using a tree of integer numbers 2 Header Files defining a node struct defining a tree class 3 Definition of Methods selectors, methods insert() and to_string() the depth of a tree and a membership function MCS 360 Lecture 24 Introduction to Data Structures Jan Verschelde, 9 March 2020 Introduction to Data Structures (MCS 360) Binary Search Trees L-24 9 March 2020 1 / 28

Upload: others

Post on 19-Jun-2020

9 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Binary Search Trees - homepages.math.uic.eduhomepages.math.uic.edu/~jan/mcs360/binary_search_trees.pdf · Binary Search Trees 1 Sorting Numbers using a Tree a sorting algorithm using

Binary Search Trees

1 Sorting Numbers using a Treea sorting algorithmusing a tree of integer numbers

2 Header Filesdefining a node structdefining a tree class

3 Definition of Methodsselectors, methods insert() and to_string()the depth of a tree and a membership function

MCS 360 Lecture 24Introduction to Data StructuresJan Verschelde, 9 March 2020

Introduction to Data Structures (MCS 360) Binary Search Trees L-24 9 March 2020 1 / 28

Page 2: Binary Search Trees - homepages.math.uic.eduhomepages.math.uic.edu/~jan/mcs360/binary_search_trees.pdf · Binary Search Trees 1 Sorting Numbers using a Tree a sorting algorithm using

Binary Search Trees

1 Sorting Numbers using a Treea sorting algorithmusing a tree of integer numbers

2 Header Filesdefining a node structdefining a tree class

3 Definition of Methodsselectors, methods insert() and to_string()the depth of a tree and a membership function

Introduction to Data Structures (MCS 360) Binary Search Trees L-24 9 March 2020 2 / 28

Page 3: Binary Search Trees - homepages.math.uic.eduhomepages.math.uic.edu/~jan/mcs360/binary_search_trees.pdf · Binary Search Trees 1 Sorting Numbers using a Tree a sorting algorithm using

Sorting Numbers using a TreeConsider the sequence 4, 5, 2, 3, 8, 1, 7

Insert the numbers in a tree:

4PPP

5���

2HH

3

HH

8

��

1�

7

Rules to insert x at node N:if N is empty, then put x in Nif x < N, insert x to the left of Nif x ≥ N, insert x to the right of N

Recursive printing: left, node, right sorts the sequence.

Introduction to Data Structures (MCS 360) Binary Search Trees L-24 9 March 2020 3 / 28

Page 4: Binary Search Trees - homepages.math.uic.eduhomepages.math.uic.edu/~jan/mcs360/binary_search_trees.pdf · Binary Search Trees 1 Sorting Numbers using a Tree a sorting algorithm using

running the program

We generate a sequence of random numbers.

$ /tmp/inttreeGive n : 8inserting 36inserting 471inserting 297inserting 453inserting 142inserting 917inserting 259inserting 123tree inorder string :36 123 142 259 297 453 471 917

$

Introduction to Data Structures (MCS 360) Binary Search Trees L-24 9 March 2020 4 / 28

Page 5: Binary Search Trees - homepages.math.uic.eduhomepages.math.uic.edu/~jan/mcs360/binary_search_trees.pdf · Binary Search Trees 1 Sorting Numbers using a Tree a sorting algorithm using

Binary Search Trees

1 Sorting Numbers using a Treea sorting algorithmusing a tree of integer numbers

2 Header Filesdefining a node structdefining a tree class

3 Definition of Methodsselectors, methods insert() and to_string()the depth of a tree and a membership function

Introduction to Data Structures (MCS 360) Binary Search Trees L-24 9 March 2020 5 / 28

Page 6: Binary Search Trees - homepages.math.uic.eduhomepages.math.uic.edu/~jan/mcs360/binary_search_trees.pdf · Binary Search Trees 1 Sorting Numbers using a Tree a sorting algorithm using

the main program#include "mcs360_integer_tree.h"using namespace mcs360_integer_tree;

int main(){

cout << "Give n : ";int n; cin >> n;srand(time(0));Tree T;for(int i=0; i<n; i++){

int r = rand() % 1000;cout << "inserting " << r << endl;T.insert(r);

}cout << "tree inorder string : " << endl;cout << T.to_string() << endl;return 0;

}

Introduction to Data Structures (MCS 360) Binary Search Trees L-24 9 March 2020 6 / 28

Page 7: Binary Search Trees - homepages.math.uic.eduhomepages.math.uic.edu/~jan/mcs360/binary_search_trees.pdf · Binary Search Trees 1 Sorting Numbers using a Tree a sorting algorithm using

Binary Tree of Nodes

A binary tree consists of nodes, a node has a data field and twopointers, to left and right child.

Node

data

left s -

right s -

If both left and right point to NULL, then the node is a leaf.

The root of the tree is a pointer to a node.

In a binary search tree, for every node: all elements in the left subtreeare smaller than the data element in the node and larger elements arestored in the right subtree.

Introduction to Data Structures (MCS 360) Binary Search Trees L-24 9 March 2020 7 / 28

Page 8: Binary Search Trees - homepages.math.uic.eduhomepages.math.uic.edu/~jan/mcs360/binary_search_trees.pdf · Binary Search Trees 1 Sorting Numbers using a Tree a sorting algorithm using

binary tree ADTabstract <typename T> binary_tree;/* A binary tree is either empty or it has a data element

and at most two subtrees, a left and a right subtree. */

abstract bool empty ( binary_tree t );postcondition: empty(t)

== true if t is empty,== false if t is not empty;

abstract T data_element ( binary_tree t );precondition: not empty(t);postcondition: data_element(t) is the data element of t;

abstract binary_tree left_subtree ( binary_tree t );precondition: not empty(t);postcondition: left_subtree(t) is the left subtree of t;

abstract binary_tree right_tree ( binary_tree t );precondition: not empty(t);postcondition: right_subtree(t) is the right subtree of t;

Introduction to Data Structures (MCS 360) Binary Search Trees L-24 9 March 2020 8 / 28

Page 9: Binary Search Trees - homepages.math.uic.eduhomepages.math.uic.edu/~jan/mcs360/binary_search_trees.pdf · Binary Search Trees 1 Sorting Numbers using a Tree a sorting algorithm using

binary search tree ADT

A binary search tree stores items of a typefor which the comparison operation < is defined.

A binary search tree is a binary tree with an insert operation.

abstract <typename T> binary_search_tree;/* A binary search tree is a binary tree where everything

less than the data element is in the left subtreeand everyting else is in the right subtree. */

abstract void insert ( binary_search_tree t, T item );postcondition: if empty(t), then after insert(t, item)we have item = data_element(t), it not empty(t), then:if item < data_element(t), item is in left_subtree(t)if item >= data_element(t), item is in right_subtree(t);

abstract bool member ( binary_search_tree t, T item );postcondition: true if the item belongs to t,false otherwise.

Introduction to Data Structures (MCS 360) Binary Search Trees L-24 9 March 2020 9 / 28

Page 10: Binary Search Trees - homepages.math.uic.eduhomepages.math.uic.edu/~jan/mcs360/binary_search_trees.pdf · Binary Search Trees 1 Sorting Numbers using a Tree a sorting algorithm using

Binary Search Trees

1 Sorting Numbers using a Treea sorting algorithmusing a tree of integer numbers

2 Header Filesdefining a node structdefining a tree class

3 Definition of Methodsselectors, methods insert() and to_string()the depth of a tree and a membership function

Introduction to Data Structures (MCS 360) Binary Search Trees L-24 9 March 2020 10 / 28

Page 11: Binary Search Trees - homepages.math.uic.eduhomepages.math.uic.edu/~jan/mcs360/binary_search_trees.pdf · Binary Search Trees 1 Sorting Numbers using a Tree a sorting algorithm using

a node struct

The file mcs360_integer_tree_node.h contains

#ifndef __TREE_NODE_H__#define __TREE_NODE_H__#include <sstream>

struct Node{

int data; // numbers stored at node in treeNode *left; // pointer to left branch of treeNode *right; // pointer to right branch of tree

Node(const int& item, Node* left_ptr = NULL,Node* right_ptr = NULL) :

data(item),left(left_ptr), right(right_ptr) {}

Introduction to Data Structures (MCS 360) Binary Search Trees L-24 9 March 2020 11 / 28

Page 12: Binary Search Trees - homepages.math.uic.eduhomepages.math.uic.edu/~jan/mcs360/binary_search_trees.pdf · Binary Search Trees 1 Sorting Numbers using a Tree a sorting algorithm using

virtual methods

virtual ~Node() {}

virtual std::string to_string() const{

std::ostringstream os;os << data;return os.str();

}};

#endif

By the virtual we can override later.

Introduction to Data Structures (MCS 360) Binary Search Trees L-24 9 March 2020 12 / 28

Page 13: Binary Search Trees - homepages.math.uic.eduhomepages.math.uic.edu/~jan/mcs360/binary_search_trees.pdf · Binary Search Trees 1 Sorting Numbers using a Tree a sorting algorithm using

Binary Search Trees

1 Sorting Numbers using a Treea sorting algorithmusing a tree of integer numbers

2 Header Filesdefining a node structdefining a tree class

3 Definition of Methodsselectors, methods insert() and to_string()the depth of a tree and a membership function

Introduction to Data Structures (MCS 360) Binary Search Trees L-24 9 March 2020 13 / 28

Page 14: Binary Search Trees - homepages.math.uic.eduhomepages.math.uic.edu/~jan/mcs360/binary_search_trees.pdf · Binary Search Trees 1 Sorting Numbers using a Tree a sorting algorithm using

defining a Tree class

The file mcs360_integer_tree.h contains

#ifndef __MCS360_INTEGER_TREE_H__#define __MCS360_INTEGER_TREE_H__#include "mcs360_integer_tree_node.h"#include <string>

namespace mcs360_integer_tree{

class Tree{

private:

Node *root; // data member

// construct tree from a nodeTree(Node *r) : root(r) {}

Introduction to Data Structures (MCS 360) Binary Search Trees L-24 9 March 2020 14 / 28

Page 15: Binary Search Trees - homepages.math.uic.eduhomepages.math.uic.edu/~jan/mcs360/binary_search_trees.pdf · Binary Search Trees 1 Sorting Numbers using a Tree a sorting algorithm using

public methods

public:

Tree() : root(NULL) {}

Tree(const int& item,const Tree& left = Tree(),const Tree& right = Tree() ) :

root(new Node(item,left.root,right.root)) {}

Tree get_left() const;Tree get_right() const;

void insert(int item);std::string to_string();

Introduction to Data Structures (MCS 360) Binary Search Trees L-24 9 March 2020 15 / 28

Page 16: Binary Search Trees - homepages.math.uic.eduhomepages.math.uic.edu/~jan/mcs360/binary_search_trees.pdf · Binary Search Trees 1 Sorting Numbers using a Tree a sorting algorithm using

Binary Search Trees

1 Sorting Numbers using a Treea sorting algorithmusing a tree of integer numbers

2 Header Filesdefining a node structdefining a tree class

3 Definition of Methodsselectors, methods insert() and to_string()the depth of a tree and a membership function

Introduction to Data Structures (MCS 360) Binary Search Trees L-24 9 March 2020 16 / 28

Page 17: Binary Search Trees - homepages.math.uic.eduhomepages.math.uic.edu/~jan/mcs360/binary_search_trees.pdf · Binary Search Trees 1 Sorting Numbers using a Tree a sorting algorithm using

get_left() and get_right()

The file mcs360_integer_tree.cpp contains

#include "mcs360_integer_tree.h"

namespace mcs360_integer_tree{

Tree Tree::get_left() const{

return Tree(root->left);}

Tree Tree::get_right() const{

return Tree(root->right);}

Introduction to Data Structures (MCS 360) Binary Search Trees L-24 9 March 2020 17 / 28

Page 18: Binary Search Trees - homepages.math.uic.eduhomepages.math.uic.edu/~jan/mcs360/binary_search_trees.pdf · Binary Search Trees 1 Sorting Numbers using a Tree a sorting algorithm using

inserting a number

void Tree::insert(int item){

if(root == NULL)root = new Node(item);

else if(item < root->data){

Tree L = this->get_left();L.insert(item);root->left = L.root;

}else{

Tree R = this->get_right();R.insert(item);root->right = R.root;

}}

Introduction to Data Structures (MCS 360) Binary Search Trees L-24 9 March 2020 18 / 28

Page 19: Binary Search Trees - homepages.math.uic.eduhomepages.math.uic.edu/~jan/mcs360/binary_search_trees.pdf · Binary Search Trees 1 Sorting Numbers using a Tree a sorting algorithm using

writing the tree inorder

std::string Tree::to_string(){

using std::string;if(root == NULL)

return "";else{

string L = this->get_left().to_string();string d = root->to_string();string R = this->get_right().to_string();return L + " " + d + " " + R;

}}

Introduction to Data Structures (MCS 360) Binary Search Trees L-24 9 March 2020 19 / 28

Page 20: Binary Search Trees - homepages.math.uic.eduhomepages.math.uic.edu/~jan/mcs360/binary_search_trees.pdf · Binary Search Trees 1 Sorting Numbers using a Tree a sorting algorithm using

tree traversals

Three kinds of tree traversals:

preorder: visit node, left tree, and right tree

inorder: visit left tree, node, and right tree

postorder: visit left tree, right tree, and node

Introduction to Data Structures (MCS 360) Binary Search Trees L-24 9 March 2020 20 / 28

Page 21: Binary Search Trees - homepages.math.uic.eduhomepages.math.uic.edu/~jan/mcs360/binary_search_trees.pdf · Binary Search Trees 1 Sorting Numbers using a Tree a sorting algorithm using

Binary Search Trees

1 Sorting Numbers using a Treea sorting algorithmusing a tree of integer numbers

2 Header Filesdefining a node structdefining a tree class

3 Definition of Methodsselectors, methods insert() and to_string()the depth of a tree and a membership function

Introduction to Data Structures (MCS 360) Binary Search Trees L-24 9 March 2020 21 / 28

Page 22: Binary Search Trees - homepages.math.uic.eduhomepages.math.uic.edu/~jan/mcs360/binary_search_trees.pdf · Binary Search Trees 1 Sorting Numbers using a Tree a sorting algorithm using

the Depth of a TreeThe depth of a node is zero is it is the root,otherwise it is 1 + the depth of its parent.

Need two new selector methods:bool Tree::is_left_null() const{

return (root->left == NULL);}bool Tree::is_right_null() const{

return (root->right == NULL);}

Need extra method to get data field:int Tree::get_data() const{

return root->data;}

Introduction to Data Structures (MCS 360) Binary Search Trees L-24 9 March 2020 22 / 28

Page 23: Binary Search Trees - homepages.math.uic.eduhomepages.math.uic.edu/~jan/mcs360/binary_search_trees.pdf · Binary Search Trees 1 Sorting Numbers using a Tree a sorting algorithm using

a function depth()

int depth ( Tree t ){

int L = 0;if(!t.is_left_null())

L = 1 + depth(t.get_left());

int R = 0;if(!t.is_right_null())

R = 1 + depth(t.get_right());

return (L > R) ? L : R;}

Note: not a member of the class.

Introduction to Data Structures (MCS 360) Binary Search Trees L-24 9 March 2020 23 / 28

Page 24: Binary Search Trees - homepages.math.uic.eduhomepages.math.uic.edu/~jan/mcs360/binary_search_trees.pdf · Binary Search Trees 1 Sorting Numbers using a Tree a sorting algorithm using

visualizing the depthInserting 9 numbers:

49 919 366 307 332 665 955 614 525tree inorder string :49 307 332 366 525 614 665 919 955

depth of tree : 549919366307332

665614525

955

Insert 2 spaces for every level.

Introduction to Data Structures (MCS 360) Binary Search Trees L-24 9 March 2020 24 / 28

Page 25: Binary Search Trees - homepages.math.uic.eduhomepages.math.uic.edu/~jan/mcs360/binary_search_trees.pdf · Binary Search Trees 1 Sorting Numbers using a Tree a sorting algorithm using

write with depth

void write_with_depth ( int k, Tree t );// writes the tree t keeping track of the depth// with the parameter k, call initially with k = 0

void write_with_depth ( int k, Tree t ){

for(int i=0; i<k; i++) cout << " ";cout << t.get_data() << endl;if(!t.is_left_null())

write_with_depth(k+1,t.get_left());if(!t.is_right_null())

write_with_depth(k+1,t.get_right());}

Introduction to Data Structures (MCS 360) Binary Search Trees L-24 9 March 2020 25 / 28

Page 26: Binary Search Trees - homepages.math.uic.eduhomepages.math.uic.edu/~jan/mcs360/binary_search_trees.pdf · Binary Search Trees 1 Sorting Numbers using a Tree a sorting algorithm using

navigating the tree

Does an integer number belong to the tree?

Recursive algorithm is_in(T,e):if e is in current node, return trueif e < data in current node,then return is_in(T.get_left(),e);else return is_in(T.get_right(),e).

Introduction to Data Structures (MCS 360) Binary Search Trees L-24 9 March 2020 26 / 28

Page 27: Binary Search Trees - homepages.math.uic.eduhomepages.math.uic.edu/~jan/mcs360/binary_search_trees.pdf · Binary Search Trees 1 Sorting Numbers using a Tree a sorting algorithm using

the function is_in()bool is_in ( Tree t, int e ){

if(e == t.get_data())return true;

else if(e < t.get_data()){

if(t.is_left_null())return false;

elsereturn is_in(t.get_left(),e);

}else{

if(t.is_right_null())return false;

elsereturn is_in(t.get_right(),e);

}}

Introduction to Data Structures (MCS 360) Binary Search Trees L-24 9 March 2020 27 / 28

Page 28: Binary Search Trees - homepages.math.uic.eduhomepages.math.uic.edu/~jan/mcs360/binary_search_trees.pdf · Binary Search Trees 1 Sorting Numbers using a Tree a sorting algorithm using

Summary + Exercises

We started with binary search trees covered in §8.4.

Exercises:1 Adjust the get_left() method to throw an exception in case the

node is NULL.2 Change the to_string() method so that the numbers are

printed in decreasing order.3 Modify mcs360_integer_tree.h and .cpp to define a

templated class for any numeric type.4 Use the code of this lecture to sort a vector of integer numbers.

Write a function that takes on input an STL vector<int> andthat returns the sorted vector.

Introduction to Data Structures (MCS 360) Binary Search Trees L-24 9 March 2020 28 / 28