Binary Trees and Binary Search Trees

Similar documents
Binary Trees, Binary Search Trees

TREES. Trees - Introduction

Trees, Binary Trees, and Binary Search Trees

Section 5.5. Left subtree The left subtree of a vertex V on a binary tree is the graph formed by the left child L of V, the descendents

Lec 17 April 8. Topics: binary Trees expression trees. (Chapter 5 of text)

A set of nodes (or vertices) with a single starting point

Computational Optimization ISE 407. Lecture 16. Dr. Ted Ralphs

Binary Tree. Preview. Binary Tree. Binary Tree. Binary Search Tree 10/2/2017. Binary Tree

Advanced Java Concepts Unit 5: Trees. Notes and Exercises

Chapter 20: Binary Trees

Trees. (Trees) Data Structures and Programming Spring / 28

Algorithms in Systems Engineering ISE 172. Lecture 16. Dr. Ted Ralphs

Tree: non-recursive definition. Trees, Binary Search Trees, and Heaps. Tree: recursive definition. Tree: example.

Associate Professor Dr. Raed Ibraheem Hamed

Data Structures. Trees. By Dr. Mohammad Ali H. Eljinini. M.A. Eljinini, PhD

Discussion 2C Notes (Week 8, February 25) TA: Brian Choi Section Webpage:

Binary Trees. Examples:

March 20/2003 Jayakanth Srinivasan,

Lecture Notes 16 - Trees CSS 501 Data Structures and Object-Oriented Programming Professor Clark F. Olson

Topic Binary Trees (Non-Linear Data Structures)

Programming II (CS300)

CS 171: Introduction to Computer Science II. Binary Search Trees

Data Structures and Algorithms

CSCI-401 Examlet #5. Name: Class: Date: True/False Indicate whether the sentence or statement is true or false.

a graph is a data structure made up of nodes in graph theory the links are normally called edges

Chapter 5. Binary Trees

Binary Search Trees. Contents. Steven J. Zeil. July 11, Definition: Binary Search Trees The Binary Search Tree ADT...

8. Binary Search Tree

Binary Trees. Height 1

Binary Trees

CS102 Binary Search Trees

Computer Science 210 Data Structures Siena College Fall Topic Notes: Trees

Trees. Introduction & Terminology. February 05, 2018 Cinda Heeren / Geoffrey Tien 1

Binary Tree. Binary tree terminology. Binary tree terminology Definition and Applications of Binary Trees

Garbage Collection: recycling unused memory

Search Trees. Data and File Structures Laboratory. DFS Lab (ISI) Search Trees 1 / 17

Binary Trees. BSTs. For example: Jargon: Data Structures & Algorithms. root node. level: internal node. edge.

Trees! Ellen Walker! CPSC 201 Data Structures! Hiram College!

Binary Trees. Directed, Rooted Tree. Terminology. Trees. Binary Trees. Possible Implementation 4/18/2013

Upcoming ACM Events Linux Crash Course Date: Time: Location: Weekly Crack the Coding Interview Date:

Design and Analysis of Algorithms Lecture- 9: Binary Search Trees

7.1 Introduction. A (free) tree T is A simple graph such that for every pair of vertices v and w there is a unique path from v to w

Trees. T.U. Cluj-Napoca -DSA Lecture 2 - M. Joldos 1

An Introduction to Trees

DATA STRUCTURES AND ALGORITHMS. Hierarchical data structures: AVL tree, Bayer tree, Heap

Trees. A tree is a directed graph with the property

Analysis of Algorithms

Binary Trees. College of Computing & Information Technology King Abdulaziz University. CPCS-204 Data Structures I

Sorted Arrays. Operation Access Search Selection Predecessor Successor Output (print) Insert Delete Extract-Min

Data Structure. Chapter 5 Trees (Part II) Angela Chih-Wei Tang. National Central University Jhongli, Taiwan

Outline. Preliminaries. Binary Trees Binary Search Trees. What is Tree? Implementation of Trees using C++ Tree traversals and applications

CMSC 341. Binary Search Trees CMSC 341 BST

Binary Search Trees Treesort

Bioinformatics Programming. EE, NCKU Tien-Hao Chang (Darby Chang)

Lecture Notes on Binary Search Trees

SCJ2013 Data Structure & Algorithms. Binary Search Tree. Nor Bahiah Hj Ahmad

Advanced Java Concepts Unit 5: Trees. Notes and Exercises

CE 221 Data Structures and Algorithms

IX. Binary Trees (Chapter 10)

Stacks, Queues and Hierarchical Collections

Data Structure - Binary Tree 1 -

Priority Queues and Heaps. Heaps of fun, for everyone!

Uses for Trees About Trees Binary Trees. Trees. Seth Long. January 31, 2010

Data Structures and Algorithms

! Tree: set of nodes and directed edges. ! Parent: source node of directed edge. ! Child: terminal node of directed edge

Operations on Heap Tree The major operations required to be performed on a heap tree are Insertion, Deletion, and Merging.

Friday Four Square! 4:15PM, Outside Gates

INF2220: algorithms and data structures Series 1

Data Structures and Algorithms for Engineers

Tree Structures. Definitions: o A tree is a connected acyclic graph. o A disconnected acyclic graph is called a forest

MID TERM MEGA FILE SOLVED BY VU HELPER Which one of the following statement is NOT correct.

Algorithms. AVL Tree

CS24 Week 8 Lecture 1

1 Binary trees. 1 Binary search trees. 1 Traversal. 1 Insertion. 1 An empty structure is an empty tree.

Binary Search Trees. See Section 11.1 of the text.

CSCI2100B Data Structures Trees

Cpt S 122 Data Structures. Data Structures Trees

Tree Data Structures CSC 221

Stacks, Queues and Hierarchical Collections. 2501ICT Logan

Introduction to Computers and Programming. Concept Question

! Tree: set of nodes and directed edges. ! Parent: source node of directed edge. ! Child: terminal node of directed edge

Topic 14. The BinaryTree ADT

Section Summary. Introduction to Trees Rooted Trees Trees as Models Properties of Trees

CS302 - Data Structures using C++

CSCI Trees. Mark Redekopp David Kempe

Priority Queues, Binary Heaps, and Heapsort

STUDENT LESSON AB30 Binary Search Trees

BBM 201 Data structures

Search Trees: BSTs and B-Trees

Algorithms and Data Structures (INF1) Lecture 8/15 Hua Lu

Unit III - Tree TREES

CSI33 Data Structures

H.O.#12 Fall 2015 Gary Chan. Binary Tree (N:12)

Module 4: Index Structures Lecture 13: Index structure. The Lecture Contains: Index structure. Binary search tree (BST) B-tree. B+-tree.

CSE 373 OCTOBER 11 TH TRAVERSALS AND AVL

MULTIMEDIA COLLEGE JALAN GURNEY KIRI KUALA LUMPUR

Cpt S 122 Data Structures. Course Review Midterm Exam # 1

Why Do We Need Trees?

Principles of Computer Science

Fall, 2015 Prof. Jungkeun Park

Transcription:

Binary Trees and Binary Search Trees

Learning Goals After this unit, you should be able to... Determine if a given tree is an instance of a particular type (e.g. binary, and later heap, etc.) Describe and use pre-, in- and post-order traversal algorithms Describe the properties of binary trees, binary search trees, and more general trees; Implement iterative and recursive algorithms for navigating them in C++ Compare and contrast ordered versus unordered trees in terms of complexity and scope of application Insert and delete elements from a binary tree

Binary Trees and Binary Search Trees C++ Implementations In a sorted array, we can search for an element using binary search an O(lg n) operation. We can now generalize this concept to binary trees. Definition: A binary tree is a data structure that is either empty or consists of a node called a root and two binary trees called the left subtree and right subtree, one or both of which may be empty. Note that this definition is recursive we define a binary tree as a structure that consists of two other (sub)trees.

Domain: A set of nodes containing some data and two pointers to other nodes. Structure: There is a unique root node (that has no parent node) having zero, one, or two child nodes; every other node has exactly one parent node and either zero, one, or two children. Operations: insertleft - insert new node to left of given node insertright - insert new node to right of given node find - find node containing given item ( search key ) findparent - find parent of given node deleteitem - remove node containing given item print - print data in the tree etc.

Nodes: A binary tree node is a <key, value> or <key, data> pair. A key is any piece of (usually unique) information that can be used to identify a data entry (i.e. the value/data). It is the key that is used when searching. For example, a useful key for storing student information might be your student number. Student number: 12345678 Student name: Alan Daniel Thomas Year: 3rd year Program: Computer Science Key value Data

Some Terminology: The path from node N 1 to node N k is a sequence of nodes N 1, N 2,, N k where N i is the parent of N i+1. The length of the path is the number of edges in the path. The depth or level of a node N is the length of the path from the root to N. The level of the root is 0. The height of a node N is the length of the longest path from N to a leaf node (a node with no child). The height of a leaf node is 0.

Some Terminology: The height of a tree is the height of its root node. The height of the empty tree is 1. The root appears at level 0. The number of nodes in a tree of height h is at least h+1 and no more than 2 h+1-1.

E H C B D J I F G Height of tree: Depth of node containing B: Height of node containing B: # of nodes in this tree: # of leaves (i.e., external nodes): # of non-leaf (i.e., internal) nodes: A

More Definitions: Full Binary Tree: Each node has exactly 0 or 2 children. Each internal node has exactly 2 children; each leaf has 0 children. Examples: Complete Binary Tree: A full binary tree where all leaves have the same depth. Examples: Nearly Complete Binary Tree: All leaves on the last level are together on the far left side, and all other levels are completely filled. Examples:

Implementation of a Binary Tree in C++ We will assume that a typedef statement has defined Item_type to be the type of data to be stored in the tree. Each node contains an item, a pointer to the left subtree and a pointer to the right subtree: typedef string Item_type; struct BNode { Item_type item; BNode* left; BNode* right; }; We will now look at the implementation of some of the operations listed earlier. To begin, we will write a makenode function to create a new BNode as needed. Note the default assignments in the argument list:

Bnode * makenode(const Item_type& item, Bnode * leftchild, Bnode * rightchild) /* PRE: item is valid, leftchild points to a node or is NULL, rightchild points to a node or is NULL POST: a new node is created and its address is returned */ { } Bnode * temp; temp = new Bnode; temp->item = item; temp->left = leftchild; temp->right = rightchild; return temp;

Bnode * makenode(const Item_type& item, Bnode * leftchild=null, Bnode * rightchild=null) /* PRE: item is valid, leftchild points to a node or is NULL, rightchild points to a node or is NULL POST: a new node is created and its address is returned */ { } Bnode * temp; temp = new Bnode; temp->item = item; temp->left = leftchild; temp->right = rightchild; return temp;

Two sample calling statements: makenode( someitem, Bnode* leftchild, Bnode* rightchild) makenode( someitem ) Notes: Recall, when leaving a function in C++, we: (a) Pass back the appropriate return value to the caller. In the calling statement, the return value actually replaces the whole function call, but only the function call (since there may be more things to do). (b) Destroy all temporary variables in the function, but retain the dynamically allocated ones. (c) Return to the caller, and continue execution with the rest of the calling statement, if anything in the calling statement remains.

Consider the insertleft function. This function is used to insert an item in a node to the left of a current node (or to create a new binary tree). We assume that current does not already have a left child; so we are inserting into an empty spot in the tree: void insertleft( BNode*& current, const Item_type& item ) // PRE: current points to a node in a binary tree with // no left child, or is NULL // POST: if current is not null, the left child of // current is a new node containing item; else, // current points to a root node containing item { if (current) // same as: if (current!= NULL) } else current->left = makenode(item); current = makenode(item);

Now let s implement/complete the find function. This function will be used to assist in the process of deleting an item from the tree. In order to delete an item, we need to find the node that contains it. BNode* find( BNode* root, const Item_type& item ) // PRE: root points to the root of a binary (sub)tree // POST: if item is in the tree, the address of the // node containing item is returned; otherwise, // NULL is returned { Bnode * temp; if (root == NULL root->item == item) temp = return root;

We will now consider the deletenode function. The task of removing a node from a binary tree is quite complicated. We will break the task down. bool deletenode( BNode*& root, const Item_type& item ) // PRE: root points to the root of a binary tree or is // NULL // POST: if item is in tree, first instance of node // containing item has been deleted and true is // returned; else, false is returned (only) { Bnode *temp, *parent; temp = find(root, item); if (!temp) // same as: if (temp == NULL) return false; parent = findparent(root, temp);

Now that we have a reference to the pointer that points to the node to be deleted, we proceed according to one of 4 cases: Case 1: node to be deleted is a leaf if (isleaf(temp)) { if (parent) // if (parent!= NULL) if (parent->left == temp) parent->left = NULL; else parent->right = NULL; else root = NULL; } delete temp; return true; // avoid memory leak // see pp. 120-121 in Nyhoff

Case 2: node to be deleted has both a left and right child This is the tricky case. There is no obvious way to remove a node having two children and re-connect the tree. Instead, we will choose not to delete the node but rather copy data from a leaf node (which is easy to remove) into the current node. We will arbitrarily choose to copy data from the leftmost leaf of the node to be deleted (if the nodes keys are not in order). You ll work on this in the lab...

Case 3: node to be deleted has only a left child if ( hasleftchild(temp) ) { if (parent) if (parent->left == temp) parent->left = temp->left; else parent->right = temp->left; else root = temp->left; delete temp return true; } Case 4: node to be deleted has only a right child (similar to #3)

Tree Traversals There are three common types of binary tree traversals: Preorder (prefix): process the current node, then recursively visit its left subtree, then recursively visit its right subtree 5 8 3 2 4 0 7 9 1 6 Data printed using preorder traversal:

Inorder (infix): visit the left subtree, then process the current node, then visit the right subtree 5 8 3 2 4 0 7 9 1 6 Data printed using inorder traversal:

Postorder (postfix): visit the left subtree, then visit the right subtree, then process the current node 5 8 3 2 4 0 7 9 1 Data printed using postorder traversal: 6

Let s consider how we would implement a function that traverses a binary tree using inorder traversal and applies a function to each node visited: void inorder( BNode* root, void (*process)(bnode*) ) // PRE: root points to a binary tree or is NULL // POST: function process() has been applied to each node // in the tree, using inorder traversal { The implementation of the functions to perform preorder or postorder traversal is left as an exercise.

An Application: Binary Expression Trees Arithmetic expressions can be represented using binary trees. We will build a binary tree representing the expression: ( 3 + 2 ) * 5 1 We start by identifying the operator with the highest precedence and build a binary tree having the operator at the root, the left operand as the left sub-tree and the right operand as the right subtree. We continue in this fashion until all operators have been represented: (3 + 2 ) (3 + 2) * 5

(3 + 2) * 5 1 Now let s print this expression tree using postorder traversal: What we now have is the arithmetic expression written using Reverse Polish Notation. It turns out to be much easier to write an algorithm to evaluate an expression written in this form than using the standard notation at the top of this page.

Binary Search Trees (Review, and C++ Implementation): A binary search tree (BST) is a binary tree such that for every node v in the tree: (a) all of the keys (elements) in v s left subtree are v s key, and (b) all of the keys in v s right subtree are v s key. 6 3 7 2 5 9 6 5 7 2 3 9 a binary search tree not a binary search tree

Common Operations on BST s Searching for a Key (called a Search Key) in a BST Algorithm: If tree is empty then search key is not present, and we re done If search key = root s key, we ve found it, and we re done If search key < root s key then search left subtree else search right subtree 6 3 7 2 5 9

In the following implementations of the Search function, we assume that the nodes of the binary search tree are represented as follows: template<typename Key, typename Value> struct BNode { Key key; Value value; Bnode * left; Bnode * right; };

Recursive Implementation of Search Function: If key is found, return true and the value associated with the key; else return false. template<typename Key, typename Value> bool search(bnode<key, Value>* root, const Key& key, Value& value) { if (root == NULL) return false; if (root->key == key) { value = root->value; return true; } if (key < root->key) // search left subtree return search(root->left, key, value); else // search right subtree return search(root->right, key, value); }

Alternatively, if we want to return a pointer to the node containing the found search key (or NULL if no such node exists), we can use the following code (and this time, we ll use an iterative version): template<typename Key, typename Value> BNode<Key, Value>* search( BNode<Key, Value>* root, const Key& key ) { while (root && root->key!= key) } if (key > root->key) else return root; root = root->right; // right subtree root = root->left;

Inserting an Item into a BST: Algorithm: If tree is empty then insert item as root If key = root s key then replace root s value with new value If key < root s key then insert into root s left subtree else insert into root s right subtree Here is a possible prototype for this function: template<typename Key, typename Value> void insert( BNode< Key, Value >*& root, const Key& key, const Value& value );

Exercise: Insert the following keys (we won t worry about the corresponding values) into an empty binary search tree in the order given. Note that in both cases, the data is the same but the order in which we do the insertion is different. Case 1: 7, 4, 3, 6, 9, 8 Case 2: 3, 4, 6, 7, 8, 9

Note that in Case 1, we end up with a bushy tree ; in fact, it is a nearly complete binary tree. If we search for an item in a complete (or nearly complete) binary tree, it takes O( ) worst-case time. In Case 2, our tree is rather unbalanced or one-sided. Searching for an item in this tree would take O( ) worst-case time. Our insertion algorithm makes no attempt to balance the tree it maintains only an ordering property, not a shape property. In this sense, a binary search tree is very different from a binary heap. (We ll study heaps, later.) It can be shown that, on average, with random insertion, the depth of a binary search tree is O( ).

Removing an Item from a BST Algorithm: Search for the item to find the node containing the item. If the item was not found, we re done. If the node is a leaf, delete the node. If node s left subtree is empty, replace node with right child else if node s right subtree is empty, replace node with left child else replace node with its logical predecessor (rightmost node of its left sub-tree) Delete 5 from tree: 6 3 7 2 5 9

Delete 7 from tree: 6 3 7 2 5 9 Delete 6 from tree: 6 3 7 2 5 9 Note that by replacing the node with its logical predecessor, we maintain the ordering property of the binary search tree. 4

Finding the Smallest Key in a BST, Recursively Assuming the tree is reasonably balanced, we can access the smallest (or largest) item in O( ) time because the smallest and largest items in a BST are stored in the extreme left and extreme right nodes of the tree. template<typename Key, typename Value> void findsmallest( BNode< Key, Value >* root, Key& key, Value& value ) //Pre: BST root is not NULL //Post: return key & value of smallest key {

A BST implementation of a map is also beneficial if we want the data sorted by key value. Suppose we want to print the data to the screen, sorted by key. This would be a(n) traversal. template<class Key, class Value> void printdata( BNode< Key, Value >* root ) { } This is a Θ( ) operation.

Learning Goals After this unit, you should be able to... Determine if a given tree is an instance of a particular type (e.g. binary, and later heap, etc.) Describe and use pre-, in- and post-order traversal algorithms Describe the properties of binary trees, binary search trees, and more general trees; Implement iterative and recursive algorithms for navigating them in C++ Compare and contrast ordered versus unordered trees in terms of complexity and scope of application Insert and delete elements from a binary tree