Binary Search Trees. What is a binary tree? Property 1: each node can have up to two successor nodes.

Slides:



Advertisements
Similar presentations
Chapter 12 Binary Search Trees
Advertisements

Chapter 8 Binary Search Tree 1 Fall Binary Trees 2 A structure with: i) a unique starting node (the root), in which ii) each node has up to two.
Binary Search Trees. John Edgar  Understand tree terminology  Understand and implement tree traversals  Define the binary search tree property  Implement.
1 abstract containers hierarchical (1 to many) graph (many to many) first ith last sequence/linear (1 to 1) set.
Binary Trees, Binary Search Trees CMPS 2133 Spring 2008.
Binary Search Trees CS 308 – Data Structures What is a binary tree? Property1: each node can have up to two successor nodes (children) –The predecessor.
1 Jake’s Pizza Shop Owner Jake Manager Chef Brad Carol Waitress Waiter Cook Helper Joyce Chris Max Len.
Binary Trees, Binary Search Trees COMP171 Fall 2006.
Trees, Binary Trees, and Binary Search Trees COMP171.
1 Chapter 8 Binary Search Trees. 2 Owner Jake Manager Chef Brad Carol Waitress Waiter Cook Helper Joyce Chris Max Len Jake’s Pizza Shop.
BST Data Structure A BST node contains: A BST contains
1 abstract containers hierarchical (1 to many) graph (many to many) first ith last sequence/linear (1 to 1) set.
Starting Out with C++: Early Objects 5/e © 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5 th Edition Chapter 19 Binary.
CHAPTER 12 Trees. 2 Tree Definition A tree is a non-linear structure, consisting of nodes and links Links: The links are represented by ordered pairs.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Sixth Edition Chapter 19: Binary Trees.
1 Joe Meehean.  Important and common problem  Given a collection, determine whether value v is a member  Common variation given a collection of unique.
More Trees COL 106 Amit Kumar and Shweta Agrawal Most slides courtesy : Douglas Wilhelm Harder, MMath, UWaterloo
Properties: -Each node has a value -The left subtree contains only values less than the parent node’s value -The right subtree contains only values greater.
Chapter 8 Binary Search Trees. Owner Jake Manager Chef Brad Carol Waitress Waiter Cook Helper Joyce Chris Max Len Jake’s Pizza Shop.
Trees.ppt1 Introduction Many data structures are linear –unique first component –unique last component –other components have unique predecessor and successor.
Binary Trees Chapter Definition And Application Of Binary Trees Binary tree: a nonlinear linked list in which each node may point to 0, 1, or two.
Tree (new ADT) Terminology:  A tree is a collection of elements (nodes)  Each node may have 0 or more successors (called children)  How many does a.
Binary Trees, Binary Search Trees RIZWAN REHMAN CENTRE FOR COMPUTER STUDIES DIBRUGARH UNIVERSITY.
1 5 Linked Structures Based on Levent Akın’s CmpE160 Lecture Slides 3 Binary Trees.
Binary Search Trees Binary Search Trees (BST)  the tree from the previous slide is a special kind of binary tree called a binary.
1 Chapter 10 Trees. 2 Definition of Tree A tree is a set of linked nodes, such that there is one and only one path from a unique node (called the root.
Trees, Binary Trees, and Binary Search Trees COMP171.
Starting at Binary Trees
 Trees Data Structures Trees Data Structures  Trees Trees  Binary Search Trees Binary Search Trees  Binary Tree Implementation Binary Tree Implementation.
Chapter 8 Binary Search Trees. 2 Goals Define and use the following terminology: binary tree root descendant subtree binary search tree parent level ancestor.
Preview  Graph  Tree Binary Tree Binary Search Tree Binary Search Tree Property Binary Search Tree functions  In-order walk  Pre-order walk  Post-order.
Chapter 8 Binary Search Trees 1. Owner Jake Manager Chef Brad Carol Waitress Waiter Cook Helper Joyce Chris Max Len Jake’s Pizza Shop 2.
Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy Walters, and Godfrey Muganda Modified for use at Midwestern State University Chapter.
Week 7 - Friday.  What did we talk about last time?  Trees in general  Binary search trees.
1 Nell Dale Chapter 8 Binary Search Trees Modified from the slides by Sylvia Sorkin, Community College of Baltimore County - Essex Campus C++ Plus Data.
Introduction to Trees IT12112 Lecture 05 Introduction Tree is one of the most important non-linear data structures in computing. It allows us to implement.
Computer Science and Software Engineering University of Wisconsin - Platteville 10. Binary Search Tree Yan Shi CS/SE 2630 Lecture Notes Partially adopted.
1 Chapter 7 Objectives Upon completion you will be able to: Create and implement binary search trees Understand the operation of the binary search tree.
Binary Tree. Some Terminologies Short review on binary tree Tree traversals Binary Search Tree (BST)‏ Questions.
1 Binary Trees and Binary Search Trees Based on Dale & Co: Object-Oriented Data Structures using C++ (graphics)
Binary Search Tree. Tree  A nonlinear data structure consisting of nodes, each of which contains data and pointers to other nodes.  Each node has only.
ADT Binary Search Tree Ellen Walker CPSC 201 Data Structures Hiram College.
Binary Search Trees (BST)
Binary Search Trees … From
COSC 2007 Data Structures II Chapter 13 Advanced Implementation of Tables I.
CS 302 – Data Structures Sections 3.1, 3.2, 3.4 & 3.5
Copyright © 2012 Pearson Education, Inc. Chapter 20: Binary Trees.
Binary Search Trees.  Understand tree terminology  Understand and implement tree traversals  Define the binary search tree property  Implement binary.
Concepts of Algorithms CSC-244 Unit 19 & 20 Binary Search Tree (BST) Shahid Iqbal Lone Computer College Qassim University K.S.A.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 20: Binary Trees.
1 Joe Meehean. A A B B D D I I C C E E X X A A B B D D I I C C E E X X  Terminology each circle is a node pointers are edges topmost node is the root.
CSE 2331/5331 Topic 8: Binary Search Tree Data structure Operations.
1 Nell Dale Chapter 8 Binary Search Trees Slides by Sylvia Sorkin, Community College of Baltimore County - Essex Campus C++ Plus Data Structures.
Binary Search Trees CS Goals Define and use the following terminology: binary tree root descendant subtree binary search tree parent level ancestor.
Lecture 9 Binary Trees Trees General Definition Terminology
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 20: Binary Trees.
1 Trees. 2 Trees Trees. Binary Trees Tree Traversal.
Binary Trees and Binary Search Trees
Week 6 - Wednesday CS221.
Trees.
Chapter 8 Binary Search Trees.
Binary Trees, Binary Search Trees
Chapter 20: Binary Trees.
Ch. 11 Trees 사실을 많이 아는 것 보다는 이론적 틀이 중요하고, 기억력보다는 생각하는 법이 더 중요하다.
Introduction to Trees IT12112 Lecture 05.
Chapter 21: Binary Trees.
Binary Trees, Binary Search Trees
Chapter 8 Binary Search Tree
Trees.
Binary Trees, Binary Search Trees
Presentation transcript:

Binary Search Trees

What is a binary tree? Property 1: each node can have up to two successor nodes.

What is a binary tree? (cont.) Property 2: a unique path exists from the root to every other node Not a valid binary tree!

Some terminology The successor nodes of a node are called its children The predecessor node of a node is called its parent The "beginning" node is called the root (has no parent) A node without children is called a leaf

Some terminology (cont’d) Nodes are organize in levels (indexed from 0). Level (or depth) of a node: number of edges in the path from the root to that node. Height of a tree h: #levels = L (Warning: some books define h as #levels-1). Full tree: every node has exactly two children and all the leaves are on the same level. not full!

What is the max #nodes at some level l? The max #nodes at level is where l=0,1,2,...,L-1

What is the total #nodes N of a full tree with height h? using the geometric series: l=0l=1l=h-1

What is the height h of a full tree with N nodes?

Why is h important? Tree operations (e.g., insert, delete, retrieve etc.) are typically expressed in terms of h. So, h determines running time!

max heightWhat is the max height of a tree with N nodes? min heightWhat is the min height of a tree with N nodes? N (same as a linked list) log(N+1)

How to search a binary tree? (1) Start at the root (2) Search the tree level by level, until you find the element you are searching for or you reach a leaf. Is this better than searching a linked list? No  O(N)

Search Binary Search Trees (BSTs) Binary Search Tree Property: The value stored at a node is greater than the value stored at its left child and less than the value stored at its right child

In a BST, the value stored at the root of a subtree is greater than any value in its left subtree and less than any value in its right subtree! Search Binary Search Trees (BSTs)

Where is the smallest element? Ans: leftmost element Where is the largest element? Ans: rightmost element Search Binary Search Trees (BSTs)

How to search a binary search tree? (1) Start at the root (2) Compare the value of the item you are searching for with the value stored at the root (3) If the values are equal, then item found; otherwise, if it is a leaf node, then not found

How to search a binary search tree? (4) If it is less than the value stored at the root, then search the left subtree (5) If it is greater than the value stored at the root, then search the right subtree (6) Repeat steps 2-6 for the root of the subtree chosen in the previous step 4 or 5

How to search a binary search tree? Is this better than searching a linked list? Yes !! ---> O(logN)

Tree node structure template struct TreeNode { ItemType info; TreeNode * left; TreeNode * right; };

Binary Search Tree Specification #include struct TreeNode ; enum OrderType {PRE_ORDER, IN_ORDER, POST_ORDER}; template class TreeType { public: TreeType(); ~TreeType(); TreeType(const TreeType &); void operator=(const TreeType &); void MakeEmpty(); bool IsEmpty() const; bool IsFull() const; int NumberOfNodes() const;

Binary Search Tree Specification void RetrieveItem(ItemType&, bool& found); void InsertItem(ItemType); void DeleteItem(ItemType); void ResetTree(OrderType); void GetNextItem(ItemType&, OrderType, bool&); void PrintTree(ofstream&) const; private: TreeNode * root; }; (cont.)

Function NumberOfNodes Recursive implementation #nodes in a tree = #nodes in left subtree + #nodes in right subtree + 1 What is the size factor? Number of nodes in the tree we are examining What is the base case? The tree is empty What is the general case? CountNodes(Left(tree)) + CountNodes(Right(tree)) + 1

Function NumberOfNodes (cont.) template int TreeType ::NumberOfNodes() const { return CountNodes(root); } template int CountNodes(TreeNode * tree) { if (tree == NULL) return 0; else return CountNodes(tree->left) + CountNodes(tree->right) + 1; } O(N) Running Time?

Function RetrieveItem

What is the size of the problem? Number of nodes in the tree we are examining What is the base case(s)? 1)When the key is found 2)The tree is empty (key was not found) What is the general case? Search in the left or right subtrees

Function RetrieveItem (cont.) template void TreeType :: RetrieveItem(ItemType& item, bool& found) { Retrieve(root, item, found); } template void Retrieve(TreeNode * tree, ItemType& item, bool& found) { if (tree == NULL) // base case 2 found = false; else if(item info) Retrieve(tree->left, item, found); else if(item > tree->info) Retrieve(tree->right, item, found); else { // base case 1 item = tree->info; found = true; } O(h) Running Time?

Function InsertItem Use the binary search tree property to insert the new item at the correct place

Function InsertItem (cont.) Implementing insertion resursively e.g., insert 11

Function InsertItem (cont.) What is the size of the problem? Number of nodes in the tree we are examining What is the base case(s)? The tree is empty What is the general case? Choose the left or right subtree

Function InsertItem (cont.) template void TreeType ::InsertItem(ItemType item) { Insert(root, item); } template void Insert(TreeNode *& tree, ItemType item) { if(tree == NULL) { // base case tree = new TreeNode ; tree->right = NULL; tree->left = NULL; tree->info = item; } else if(item info) Insert(tree->left, item); else Insert(tree->right, item); } O(h) Running Time?

Function InsertItem (cont.) Insert 11

Does the order of inserting elements into a tree matter? Yes, certain orders might produce very unbalanced trees!

Does the order of inserting elements into a tree matter? (cont.)

Does the order of inserting elements into a tree matter? (cont’d) Unbalanced trees are not desirable because search time increases! Advanced tree structures, such as red-black trees, guarantee balanced trees.

Function DeleteItem First, find the item; then, delete it Binary search tree property must be preserved!! We need to consider three different cases: (1) Deleting a leaf (2) Deleting a node with only one child (3) Deleting a node with two children

(1) Deleting a leaf

(2) Deleting a node with only one child

(3) Deleting a node with two children

(3) Deleting a node with two children (cont.) Find predecessor (i.e., rightmost node in the left subtree) Replace the data of the node to be deleted with predecessor's data Delete predecessor node

Function DeleteItem (cont.) What is the size of the problem? Number of nodes in the tree we are examining What is the base case(s)? Key to be deleted was found What is the general case? Choose the left or right subtree

Function DeleteItem (cont.) template void TreeType ::DeleteItem(ItemType item) { Delete(root, item); } template void Delete(TreeNode *& tree, ItemType item) { if(item info) Delete(tree->left, item); else if(item > tree->info) Delete(tree->right, item); else DeleteNode(tree); }

Function DeleteItem (cont.) template void DeleteNode(TreeNode *& tree) { ItemType item; TreeNode * tempPtr; tempPtr = tree; if(tree->left == NULL) { // right child tree = tree->right; delete tempPtr; } else if(tree->right == NULL) { // left child tree = tree->left; delete tempPtr; } else { GetPredecessor(tree->left, item); tree->info = item; Delete(tree->left, item); }

Function DeleteItem (cont.) template void GetPredecessor(TreeNode * tree, ItemType& item) { while(tree->right != NULL) tree = tree->right; item = tree->info; }

Function DeleteItem (cont.) template void TreeType ::DeleteItem(ItemType item) { Delete(root, item); } template void Delete(TreeNode *& tree, ItemType item) { if(item info) Delete(tree->left, item); else if(item > tree->info) Delete(tree->right, item); else DeleteNode(tree); } Running Time? O(h)

Function DeleteItem (cont.)

Tree Traversals There are mainly three ways to traverse a tree: 1)Inorder Traversal 2)Postorder Traversal 3)Preorder Traversal

Inorder Traversal: A E H J M T Y 46 ‘J’ ‘E’ ‘A’ ‘H’ ‘T’ ‘M’‘Y’ tree Visit left subtree firstVisit right subtree last Visit second

Inorder Traversal Visit the nodes in the left subtree, then visit the root of the tree, then visit the nodes in the right subtree Inorder(tree) If tree is not NULL Inorder(Left(tree)) Visit Info(tree) Inorder(Right(tree)) Warning: "visit" implies do something with the value at the node (e.g., print, save, update etc.).

Preorder Traversal: J E A H T M Y 48 ‘J’ ‘E’ ‘A’ ‘H’ ‘T’ ‘M’‘Y’ tree Visit left subtree secondVisit right subtree last Visit first

Preorder Traversal Visit the root of the tree first, then visit the nodes in the left subtree, then visit the nodes in the right subtree Preorder(tree) If tree is not NULL Visit Info(tree) Preorder(Left(tree)) Preorder(Right(tree))

50 ‘J’ ‘E’ ‘A’ ‘H’ ‘T’ ‘M’‘Y’ tree Visit left subtree firstVisit right subtree second Visit last Postorder Traversal: A H E M Y T J

Postorder Traversal Visit the nodes in the left subtree first, then visit the nodes in the right subtree, then visit the root of the tree Postorder(tree) If tree is not NULL Postorder(Left(tree)) Postorder(Right(tree)) Visit Info(tree)

Tree Traversals: another example

Function PrintTree We use "inorder" to print out the node values. Keys will be printed out in sorted order. Hint: binary search could be used for sorting! A D J M Q R T

Function PrintTree (cont.) void TreeType::PrintTree(ofstream& outFile) { Print(root, outFile); } template void Print(TreeNode * tree, ofstream& outFile) { if(tree != NULL) { Print(tree->left, outFile); outFile info; // “visit” Print(tree->right, outFile); }

Class Constructor template TreeType ::TreeType() { root = NULL; }

Class Destructor How should we delete the nodes of a tree? Use postorder! Delete the tree in a "bottom-up" fashion

Class Destructor (cont’d) TreeType::~TreeType() { Destroy(root); } void Destroy(TreeNode *& tree) { if(tree != NULL) { Destroy(tree->left); Destroy(tree->right); delete tree; // “visit” } postorder

Copy Constructor How should we create a copy of a tree? Use preorder!

Copy Constructor (cont’d) template TreeType ::TreeType(const TreeType & originalTree) { CopyTree(root, originalTree.root); } template<class ItemType) void CopyTree(TreeNode *& copy, TreeNode * originalTree) { if(originalTree == NULL) copy = NULL; else { copy = new TreeNode ; copy->info = originalTree->info; CopyTree(copy->left, originalTree->left); CopyTree(copy->right, originalTree->right); } preorder // “visit”

Comparing Binary Search Trees to Linear Lists Big-O Comparison Operation Binary Search Tree Array-based List Linked List ConstructorO(1) DestructorO(N)O(1)O(N) IsFullO(1) IsEmptyO(1) RetrieveItemO(logN )* O(logN)O(N) InsertItemO(logN )* O(N) DeleteItemO(logN) * O(N) *assuming h=O(logN)

Exercises

Exercise