INTRODUCTION TO BINARY TREES P. 651 - 659. SORTING  Review of Linear Search: –again, begin with first element and search through list until finding element,

Slides:



Advertisements
Similar presentations
Senem Kumova Metin Spring2009 BINARY TREES && TREE TRAVERSALS Chapter 10 in A Book on C.
Advertisements

Searching Kruse and Ryba Ch and 9.6. Problem: Search We are given a list of records. Each record has an associated key. Give efficient algorithm.
They’re not just binary anymore!
Main Index Contents 11 Main Index Contents Tree StructuresTree Structures (3 slides) Tree Structures Tree Node Level and Path Len. Tree Node Level and.
BST Data Structure A BST node contains: A BST contains
Data Structures Data Structures Topic #8. Today’s Agenda Continue Discussing Table Abstractions But, this time, let’s talk about them in terms of new.
Starting Out with C++, 3 rd Edition 1 Chapter 20 Binary Trees.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 12 – Data Structures Outline 12.1Introduction.
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.
Data Structures Using C++ 2E Chapter 11 Binary Trees and B-Trees.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Sixth Edition Chapter 19: Binary Trees.
10. Binary Trees A. Introduction: Searching a linked list.
Binary Search Trees Chapter 6.
1 Joe Meehean.  Important and common problem  Given a collection, determine whether value v is a member  Common variation given a collection of unique.
Version TCSS 342, Winter 2006 Lecture Notes Trees Binary Trees Binary Search Trees.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Data Structures Trees.
B+ Tree What is a B+ Tree Searching Insertion Deletion.
CSCE 3110 Data Structures & Algorithm Analysis Binary Search Trees Reading: Chap. 4 (4.3) Weiss.
INTRODUCTION TO AVL TREES P. 839 – 854. INTRO  Review of Binary Trees: –Binary Trees are useful for quick retrieval of items stored in the tree –order.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Templatized Tree.
Searching: Binary Trees and Hash Tables CHAPTER 12 6/4/15 Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education,
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.
Binary Trees Chapter 10. Introduction Previous chapter considered linked lists –nodes connected by two or more links We seek to organize data in a linked.
AVL Trees CSCI 2720 Fall 2005 Kraemer. Binary Tree Issue  One major problem with the binary trees we have discussed thus far: they can become extremely.
Prof. Amr Goneid, AUC1 Analysis & Design of Algorithms (CSCE 321) Prof. Amr Goneid Department of Computer Science, AUC Part R2. Binary Search Trees.
BINARY SEARCH TREE. Binary Trees A binary tree is a tree in which no node can have more than two children. In this case we can keep direct links to the.
Chapter 19: Binary Trees Java Programming: Program Design Including Data Structures Program Design Including Data Structures.
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.
© M. Gross, ETH Zürich, 2014 Informatik I für D-MAVT (FS 2014) Exercise 12 – Data Structures – Trees Sorting Algorithms.
Starting at Binary Trees
1 10. Binary Trees Read Sec A. Introduction: Searching a linked list. 1. Linear Search /* Linear search a list for a particular item */ 1. Set.
Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy Walters, and Godfrey Muganda Modified for use at Midwestern State University Chapter.
Topics Definition and Application of Binary Trees Binary Search Tree Operations.
Binary Trees Chapter 10. Introduction Previous chapter considered linked lists –nodes connected by two or more links We seek to organize data in a linked.
© Copyright 2012 by Pearson Education, Inc. All Rights Reserved. 1 Chapter 19 Binary Search Trees.
CMSC 341 Introduction to Trees. 2/21/20062 Tree ADT Tree definition –A tree is a set of nodes which may be empty –If not empty, then there is a distinguished.
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.
Prof. Amr Goneid, AUC1 CSCE 210 Data Structures and Algorithms Prof. Amr Goneid AUC Part 6. Dictionaries(3): Binary Search Trees.
Binary Search Trees (BST)
Tree Data Structures. Heaps for searching Search in a heap? Search in a heap? Would have to look at root Would have to look at root If search item smaller.
Copyright © 2012 Pearson Education, Inc. Chapter 20: Binary Trees.
Data Structures Using C++ 2E Chapter 11 Binary Trees.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 20: Binary Trees.
CMSC 202, Version 5/02 1 Trees. CMSC 202, Version 5/02 2 Tree Basics 1.A tree is a set of nodes. 2.A tree may be empty (i.e., contain no nodes). 3.If.
Search: Binary Search Trees Dr. Yingwu Zhu. Review: Linear Search Collection of data items to be searched is organized in a list x 1, x 2, … x n – Assume.
@ Zhigang Zhu, CSC212 Data Structure - Section FG Lecture 17 B-Trees and the Set Class Instructor: Zhigang Zhu Department of Computer Science.
CS 240Chapter 10 – TreesPage Chapter 10 Trees The tree abstract data type provides a hierarchical to the representation of certain types of relationships.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 20: Binary Trees.
1 Binary Search Trees. 2 Binary Search Trees Binary Search Trees The Binary Search Tree (BST) Search, Insertion and Traversal of BST Removal of nodes.
1 Trees 3: The Binary Search Tree Reading: Sections 4.3 and 4.6.
Chapter 12 – Data Structures
COMP 53 – Week Fourteen Trees.
CSCE 3110 Data Structures & Algorithm Analysis
CSC212 Data Structure - Section AB
Tree data structure.
Binary Tree Applications
Binary Trees Lecture 36 Wed, Apr 21, /21/2018 Binary Trees.
Chapter 20: Binary Trees.
Map interface Empty() - return true if the map is empty; else return false Size() - return the number of elements in the map Find(key) - if there is an.
Trees 3: The Binary Search Tree
Chapter 21: Binary Trees.
Tree data structure.
Tree A tree is a data structure in which each node is comprised of some data as well as node pointers to child nodes
Search Sorted Array: Binary Search Linked List: Linear Search
Chapter 10 1 – Binary Trees Tree Structures (3 slides)
Pointers & Dynamic Data Structures
CSC 143 Java Trees.
Mark Redekopp David Kempe
Data Structures Using C++ 2E
Presentation transcript:

INTRODUCTION TO BINARY TREES P

SORTING  Review of Linear Search: –again, begin with first element and search through list until finding element, returning that index. –Otherwise, -1 is returned worst case: O(n) –occurs if item not in list; then, all items must be compared – if list is ordered, runs a little faster on average, since whole list may not have to be searched; worst still O(n), if item is greater than all list items –can modify linear search to represent a linked list:

Linear Search with Linked List  int LinkedList :: Search(const Item &myItem) { int pos_num = 0; Node *position; position = head; while (position != 0) { if (myItem == position->data) return pos_num; else { position = position->link; pos_num++; } return -1; }

Review of Binary Search - start with midde item, compare that; if match, return middle - otherwise, if item < middle, repeat BS on first half - otherwise, repeat BS on second half - eventually list split down to 1 element, and if that doesn't match, return -1 - runs in O(log n) time, due to number of splits made based on size of list -if size 4, 2 splits; 8 gives 3, 16 gives 4, etc; this is a log function) - binary search does not work with linked lists, because finding the middle element is too cumbersome - can use a different structure to store the contents of the ordered list to make it easy to apply algorithms like binary search

Binary Search Review (cont.) - how? -make the initial middle point the root -then make all subsequent middles elements branching off the root -example: List: middle: 39 -middle of first half: 4 -middle of second half: 70 -can express as a diagram -this structure is called a binary tree or binary search tree

BINARY TREE DEFINITIONS Binary Tree: non-linear linked list where each node may point to up to two other nodes Empty Tree: a tree with no nodes arcs: connectors between nodes subtree: an entire branch of a tree, from one particular node down root node (or root): the first node of the tree; only one in each tree children: nodes that are branched off from a particular node

B-TREES (Definitions cont.) parent: - node that has children (nodes branching off it); all nodes can have only 1 parent left child: represents all nodes to left of that parent right child: all the values greater than that parent leaf node: a node with no children siblings: all nodes on the same level with same parent

B-TREE EXAMPLES Example of a valid B- Tree: Why Valid? one root at most 2 children from any node values of left children less than parent values of right children greater than parent Data: 15 Data: 1 Data: 22 NULL

B-TREE EXAMPLES (cont.) EXAMPLE OF AN INVALID B-TREE : WHY INVALID? -child 12 has a left child greater than it -child 72 has a right child less than it Data: 25 Data: 12Data: 72 NULL Data: 17Data: 22Data: 49 NULL

IMPLEMENTATION OF BINARY SEARCH TREE will use structure similar to linked list, but with some modifications to account for two links typedef int Item; struct TreeNode { Item Value; //data TreeNode *left; //pointer to left child TreeNode *right; //pointer to right child };

BINARY TREE IMPLEMENTATION (CONT.) then we can define a new class called a Binary Tree: class BinaryTree { public: BinaryTree(); ~BinaryTree(); void Display(); void Insert(const Item &anItem); bool Search(const Item &anItem); bool Delete(const Item &anItem); private: TreeNode *root; TreeNode *InsertTree(TreeNode *root, TreeNode *node); void DisplayTree(TreeNode root); bool SearchTree(TreeNode root, const Item &anItem); void DeleteCompleteTree(); bool DeleteTreeValue(TreeNode &root, const Item &anItem); };

B-TREE MEMBER FUNCTIONS - Constructor -sets root to null, indicating the tree is empty BinaryTree::BinaryTree() { root = 0; } - Insertion - use the divide-and-conquer approach to solving this: -using recursion, decide to work on either right or left of current node -reduces the problem in half - how does insertion work? -find the location where the new item goes (start at root and recursively move through nodes) -user should ONLY know about how to use the function, not the internals of the tree structure -that is why we need two functions: the public insert (called by the user) and the private InsertTree (called internally, that handles the recursive calls)

B-TREE: Insert Function //the public function void BinaryTree::Insert(const Item &anItem) { node = new TreeNode; node -> left = null; node -> right = null; node->value = anItem; root = InsertTree(root, node); } TreeNode BinaryTree::InsertTree(TreeNode *root,TreeNode *node) { if (root == 0) { //tree is empty, or we have reached a leaf node root = node; return root; } //handle case if this item is a duplicate if (node -> value == root->Value) ` return root; //search the left side if (node->value Value) root -> left = InsertTree(root->left, node); else //search the right side root->right = InsertTree(root->right, node); }

B-TREES – Display Function Display Function same as insert; two functions, one public (called display) and one private (called DisplayTree) //public function void BinaryTree::Display() { DisplayTree(root); cout << endl; } //private function void BinaryTree::DisplayTree(TreeNode root) { if (root != 0) { DisplayTree(root->left); cout Value << " "; DisplayTree(root->right); }

B-TREES – Search Function - Search is a lot like Display; has both a public and private function - traverses the tree checking to see if the value matches what we are looking for //the public function bool BinaryTree::Search(const Item &anItem) { return (SearchTree(root, anItem)); } //the private function bool BinaryTree::SearchTree(TreeNode root, const Item &anItem) { if (root == 0) return false; if (root->Value == anItem) return true; if (anItem Value) return(SearchTree(root->left, anItem)); else return(SearchTree(root->right, anItem)); }

SAMPLE PROGRAM void main() { BinaryTree TheTree; TheTree.Insert(5); TheTree.Insert(20); TheTree.Insert(1); TheTree.Insert(17); TheTree.Display(); if (TheTree.Search(100)) cout << "Found 100" << endl; if (TheTree.Search(1)) cout << "Found 1" << endl; cout << endl; }

B-TREES (other functions) The Deconstructor - traverses the left and right nodes, and upon returning, deletes them from memory //the public function void BinaryTree::~BinaryTree() { DeleteCompleteTree(root); } //the private function void BinaryTree::DeleteCompleteTree(TreeNode root) { if (root != 0) { DeleteCompleteTree(root->left); DeleteCompleteTree(root->right); delete root; }

QUESTIONS? Food for Thought: “How would you write a function that just deletes a single node from the tree?” NEXT TOPIC: AVL Trees (Ch. 15 – P )