Data Structures - Binary Trees and Binary Search Trees - Assignment 8 | CSCI 1200, Assignments of Data Structures and Algorithms

Material Type: Assignment; Class: DATA STRUCTURES; Subject: Computer Science; University: Rensselaer Polytechnic Institute; Term: Spring 2006;

Typology: Assignments

Pre 2010

Uploaded on 08/09/2009

koofers-user-jln
koofers-user-jln šŸ‡ŗšŸ‡ø

10 documents

1 / 6

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
CSCI-1200 Computer Science II — Spring 2006
Lecture 21 — Trees, Part I
COURSE EVALUATIONS: Tuesday April 25th. Bring a #2 pencil.
Review from Lectures 20 & 21 — Linked Lists
•Singly-linked lists, doubly-linked lists, standard list operations
•Discuss cs2list::erase.
Today’s Lecture — Binary Trees and Binary Search Trees
•Definition
•Basic operations
•Implementation of cs2set class using binary search trees
•Lots more tree stuff in CSCI 2300 Data Structures & Algorithms (DSA)!
22.1 Lists vs. Trees vs. Graphs
•Trees create a hierarchical organization of data, rather than the linear organization in linked lists (and arrays
and vectors).
•Binary search trees are the mechanism underlying maps & sets (and multimaps & multisets).
•In HW8 you are building a graph with bi-directional edges to represent trains between cities. A tree is a special
graph that has no cycles. The edges that connect nodes in trees and graphs may be directed or undirected.
22.2 Definition: Binary Trees
•A binary tree (strictly speaking, a ā€œrooted binary treeā€) is either empty or is a node that has pointers to two
binary trees.
•Here’s a picture of a binary tree storing integer values. In this figure, each large box indicates a tree node, with
the top rectangle representing the value stored and the two lower boxes representing pointers. Pointers that
are null are shown with an empty box.
12
77
āˆ’98
198
99 14
52 33
53235
pf3
pf4
pf5

Partial preview of the text

Download Data Structures - Binary Trees and Binary Search Trees - Assignment 8 | CSCI 1200 and more Assignments Data Structures and Algorithms in PDF only on Docsity!

CSCI-1200 Computer Science II — Spring 2006

Lecture 21 — Trees, Part I

COURSE EVALUATIONS: Tuesday April 25th. Bring a #2 pencil.

Review from Lectures 20 & 21 — Linked Lists

  • Singly-linked lists, doubly-linked lists, standard list operations
  • Discuss cs2list::erase.

Today’s Lecture — Binary Trees and Binary Search Trees

  • Definition
  • Basic operations
  • Implementation of cs2set class using binary search trees
  • Lots more tree stuff in CSCI 2300 Data Structures & Algorithms (DSA)!

22.1 Lists vs. Trees vs. Graphs

  • Trees create a hierarchical organization of data, rather than the linear organization in linked lists (and arrays and vectors).
  • Binary search trees are the mechanism underlying maps & sets (and multimaps & multisets).
  • In HW8 you are building a graph with bi-directional edges to represent trains between cities. A tree is a special graph that has no cycles. The edges that connect nodes in trees and graphs may be directed or undirected.

22.2 Definition: Binary Trees

  • A binary tree (strictly speaking, a ā€œrooted binary treeā€) is either empty or is a node that has pointers to two binary trees.
  • Here’s a picture of a binary tree storing integer values. In this figure, each large box indicates a tree node, with the top rectangle representing the value stored and the two lower boxes representing pointers. Pointers that are null are shown with an empty box.

77

  • The topmost node in the tree is called the root.
  • The pointers from each node are called left and right. The nodes they point to are referred to as that node’s (left and right) children.
  • The (sub)trees pointed to by the left and right pointers at any node are called the left subtree and right subtree of that node.
  • A node where both children pointers are null is called a leaf node.
  • A node’s parent is the unique node that points to it. Only the root has no parent.

22.3 Definition: Binary Search Trees

  • A binary search tree is a binary tree where at each node of the tree, the value stored at the node is
    • greater than or equal to all values stored in the left subtree, and
    • less than or equal to all values stored in the right subtree.
  • Below is a picture of a binary search tree storing string values.

ant zebra

cat

dog

goat

horse

lion

mouse

mule

tiger

22.4 Exercise

Consider the following values:

4.5, 9.8, 3.5, 13.6, 19.2, 7.4, 11.

  1. Draw a binary tree with these values that is NOT a binary search tree.
  2. Draw two different binary search trees with these values. Important note: This shows that the binary search tree structure for a given set of values is not unique!

22.8 cs2set and Binary Search Tree Implementation

  • A partial implementation of a set using a binary search tree is in the code attached. We will continue to study this implementation in the next lecture & lab.
  • The increment and decrement operations for iterators have been omitted from this implementation. Next lecture we will discuss a couple strategies for adding these operations, but will skip the implementation (due to time limitations).
  • We will use this as the basis both for understanding an initial selection of tree algorithms and for thinking about how standard library sets really work.

22.9 cs2set: Class Overview

  • The classes are templated.
  • There is an auxiliary TreeNode class
  • The only member variables of the cs2set class are the root and the size (number of tree nodes).
  • The iterator class is declared internally, and is effectively a wrapper on the TreeNode pointers.
    • Note that operator* returns a const reference because the keys can’t change.
    • As just discussed the increment and decrement operators are missing.
  • The main public member functions just call a private (and often recursive) member function (passing the root node) that does all of the work.
  • Because the class stores and manages dynamically allocated memory, a copy constructor, operator=, and destructor must be provided.

22.10 Exercises

  1. Provide the implementation of the member function cs2set::begin. This is essentially the problem of finding the node in the tree that has stores the smallest value.
  2. Write a recursive version of the function find.

// Partial implementation of binary-tree based set class similar to std::set. // The iterator increment & decrement operations have been omitted. #ifndef cs2set_h_ #define cs2set_h_ #include #include

// ------------------------------------------------------------------- // TREE NODE CLASS template class TreeNode { public: TreeNode() : left(NULL), right(NULL) {} TreeNode(const T& init) : value(init), left(NULL), right(NULL) {} T value; TreeNode* left; TreeNode* right; };

template class cs2set;

// ------------------------------------------------------------------- // TREE NODE ITERATOR CLASS template class tree_iterator { public: tree_iterator() : ptr_(NULL) {} tree_iterator(TreeNode* p) : ptr_(p) {} tree_iterator(const tree_iterator& old) : ptr_(old.ptr_) {} ~tree_iterator() {} tree_iterator& operator=(const tree_iterator& old) { ptr_ = old.ptr_; return *this; }

// operator* gives constant access to the value at the pointer const T& operator*() const { return ptr_->value; } // comparions operators are straightforward friend bool operator==(const tree_iterator& l, const tree_iterator& r) { return l.ptr_ == r.ptr_; } friend bool operator!=(const tree_iterator& l, const tree_iterator& r) { return l.ptr_ != r.ptr_; }

private: // representation TreeNode* ptr_; };

// ------------------------------------------------------------------- // CS2 SET CLASS template class cs2set { public: cs2set() : root_(NULL), size_(0) {} cs2set(const cs2set& old) : size_(old.size_) { root_ = this->copy_tree(old.root_); } ~cs2set() { this->destroy_tree(root_); root_ = NULL; } cs2set& operator=(const cs2set& old) { if (old != *this) { this->destroy_tree(root_); root_ = this->copy_tree(old.root_); size_ = old.size_; } return *this; }

typedef tree_iterator iterator;

int size() const { return size_; } bool operator==(const cs2set& old) const { return (old.root_ == this->root_); }