Trees, Binary Search Trees, Recursion, Project 2 Bryce Boe 2013/08/01 CS24, Summer 2013 C.

Slides:



Advertisements
Similar presentations
Trees Types and Operations
Advertisements

S. Sudarshan Based partly on material from Fawzi Emad & Chau-Wen Tseng
Tree Data Structures &Binary Search Tree 1. Trees Data Structures Tree  Nodes  Each node can have 0 or more children  A node can have at most one parent.
IKI 10100: Data Structures & Algorithms Ruli Manurung (acknowledgments to Denny & Ade Azurat) 1 Fasilkom UI Ruli Manurung (Fasilkom UI)IKI10100: Lecture20.
IKI 10100I: Data Structures & Algorithms Ruli Manurung (acknowledgments to Denny & Ade Azurat) 1 Fasilkom UI Ruli Manurung (Fasilkom UI)IKI10100I: Data.
Binary Trees, Binary Search Trees CMPS 2133 Spring 2008.
Binary Trees, Binary Search Trees COMP171 Fall 2006.
Data Structures: Trees i206 Fall 2010 John Chuang Some slides adapted from Marti Hearst, Brian Hayes, or Glenn Brookshear.
Trees, Binary Trees, and Binary Search Trees COMP171.
1 Trees. 2 Outline –Tree Structures –Tree Node Level and Path Length –Binary Tree Definition –Binary Tree Nodes –Binary Search Trees.
CSE 326: Data Structures Binary Search Trees Ben Lerner Summer 2007.
CS 104 Introduction to Computer Science and Graphics Problems Data Structure & Algorithms (4) Data Structures 11/18/2008 Yang Song.
Lec 15 April 9 Topics: l binary Trees l expression trees Binary Search Trees (Chapter 5 of text)
Dr. Andrew Wallace PhD BEng(hons) EurIng
1 BST Trees A binary search tree is a binary tree in which every node satisfies the following: the key of every node in the left subtree is.
Week 7 - Wednesday.  What did we talk about last time?  Recursive running time  Master Theorem  Introduction to trees.
Binary Trees Chapter 6.
By : Budi Arifitama Pertemuan ke Objectives Upon completion you will be able to: Create and implement binary search trees Understand the operation.
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.
Binary Search Trees. BST Properties Have all properties of binary tree Items in left subtree are smaller than items in any node Items in right subtree.
Priority Queues and Heaps Bryce Boe 2013/11/20 CS24, Fall 2013.
Recursion Bryce Boe 2013/11/18 CS24, Fall Outline Wednesday Recap Lab 7 Iterative Solution Recursion Binary Tree Traversals Lab 7 Recursive Solution.
Lecture 17 Non-Linear data structures Richard Gesick.
CISC220 Fall 2009 James Atlas Lecture 13: Trees. Skip Lists.
1 Trees A tree is a data structure used to represent different kinds of data and help solve a number of algorithmic problems Game trees (i.e., chess ),
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.
Tree Data Structures.
Binary Search Trees Binary Search Trees (BST)  the tree from the previous slide is a special kind of binary tree called a binary.
Trees, Binary Trees, and Binary Search Trees COMP171.
Starting at Binary Trees
Binary Search Trees (10.1) CSE 2011 Winter November 2015.
Binary Search Tree Traversal Methods. How are they different from Binary Trees?  In computer science, a binary tree is a tree data structure in which.
 Trees Data Structures Trees Data Structures  Trees Trees  Binary Search Trees Binary Search Trees  Binary Tree Implementation Binary Tree Implementation.
Lec 15 Oct 18 Binary Search Trees (Chapter 5 of text)
Preview  Graph  Tree Binary Tree Binary Search Tree Binary Search Tree Property Binary Search Tree functions  In-order walk  Pre-order walk  Post-order.
1 Joe Meehean.  We wanted a data structure that gave us... the smallest item then the next smallest then the next and so on…  This ADT is called a priority.
Data Structures Chapter 6. Data Structure A data structure is a representation of data and the operations allowed on that data. Examples: 1.Array 2.Record.
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 Search Trees (BSTs) 18 February Binary Search Tree (BST) An important special kind of binary tree is the BST Each node stores some information.
Binary Search Trees (BST)
Rooted Tree a b d ef i j g h c k root parent node (self) child descendent leaf (no children) e, i, k, g, h are leaves internal node (not a leaf) sibling.
1 Trees General Trees  Nonrecursive definition: a tree consists of a set of nodes and a set of directed edges that connect pairs of nodes.
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.
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.
More Binary Search Trees, Project 2 Bryce Boe 2013/08/06 CS24, Summer 2013 C.
1 AVL Trees II Implementation. 2 AVL Tree ADT A binary search tree in which the balance factor of each node is 0, 1, of -1. Basic Operations Construction,
CS261 Data Structures Binary Search Trees Concepts.
(c) University of Washington20c-1 CSC 143 Binary Search Trees.
A Binary Search Tree Implementation Chapter 25 © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Data Structures and Abstractions.
BSTs, AVL Trees and Heaps Ezgi Shenqi Bran. What to know about Trees? Height of a tree Length of the longest path from root to a leaf Height of an empty.
Binary Search Trees Chapter 7 Objectives
Chapter 12 – Data Structures
BST Trees
CISC220 Fall 2009 James Atlas Lecture 13: Binary Trees.
Binary Search Tree (BST)
Problems with Linked List (as we’ve seen so far…)
Lecture 22 Binary Search Trees Chapter 10 of textbook
ITEC 2620M Introduction to Data Structures
Binary Trees, Binary Search Trees
Binary Search 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.
Lec 12 March 9, 11 Mid-term # 1 (March 21?)
CMSC 202 Trees.
CS6045: Advanced Algorithms
Binary Trees, Binary Search Trees
CSC 143 Binary Search Trees.
Non-Linear data structures
Binary Search Trees CS 580U Fall 17.
Binary Trees, Binary Search Trees
Presentation transcript:

Trees, Binary Search Trees, Recursion, Project 2 Bryce Boe 2013/08/01 CS24, Summer 2013 C

Outline Stack/Queue Review Trees Recursion Binary Search Trees Project 2

Stack / Queue Review Stack operations – push – pop Queue operations – enqueue – dequeue

TREES

Tree Explained Data structure composed of nodes (like a linked list) Each node in a tree can have one or more children (binary tree has at most two children)

Binary Tree

Tree Properties The root is the top-most node of the tree (has no parent) A node’s parent is the node immediately preceding it (closer to the root) A node can have at most two children or child nodes A leaf is a node with no children

More Properties A node’s ancestors are all nodes preceding it A node’s descendants all all nodes succeeding it A subtree is the complete tree starting with a given node and including its descendants

Tree properties

More Properties The depth of a node is how far it is away from the root (the root is at depth 0) The height of a node is the maximum distance to one of its descendent leaf nodes (a leaf node is at height 0) The height of a tree is the height of the root node

What is the depth of G? 3 3

What is the depth of D? 2 2

What is the height of C? 2 2

What is the height of B? 1 1

What is the height of the tree? 3 3

What nodes make up A’s right subtree?

RECURSION

What is recursion? The process of solving a problem by dividing it into similar subproblems Examples – Factorial: 5! = 5 * 4! = 5 * 4 * 3! – Fibonacci Numbers: F(N) = F(n-1) + F(n-2) – Length of linked list: L(node) = 1 + L(node->next)

Factorial Base Case: – F(1) = 1 General Case – F(n) = n * F(n-1)

Factorial int factorial(n) { if (n < 1) throw 1; // Error condition else if (n == 1) // Base Case return 1; else // General Case return n * factorial(n – 1); }

Fibonacci Numbers Base Cases: – F(0) = 0 – F(1) = 1 General Case: – F(n) = F(n-1) + F(n-2)

Linked List Length Base Case: – Length(last node) = 1 General Case: – Length(node) = 1 + Length(node->next);

Linked List Length (option 1) int length(Node *n) { if (n == NULL) // Base Case return 0; else // General Case return 1 + length(n->next); }

Linked List Length (option 2) int length(Node *n) { if (n == NULL) throw 1; // Error condition else if (n->next == NULL) // Base Case return 1; else // General Case return 1 + length(n->next); }

Recursion and the Stack Segment main calls Factorial(3) main Factorial(3) Factorial(2) Factorial(1)

C++ Examples See – week6/recursion.cpp – week6/trees.cpp

BINARY SEARCH TREES

Binary Search Trees A tree with the property that the value of all descendants of a node’s left subtree are smaller, and the value of all descendants of a node’s right subtree are larger

BST Example

BST Operations insert(item) – Add an item to the BST remove(item) – Remove an item from the BST contains(item) – Test whether or not the item is in the tree

BST Running Times All operations are O(n) in the worst case – Why? Assuming a balanced tree (CS132 material) – insert: O(log(n)) – delete: O(log(n)) – contains: O(log(n))

BST Insert If empty insert at the root If smaller than the current node – If no node on left: insert on the left – Otherwise: set the current node to the lhs (repeat) If larger than the current node – If no node on the right: insert on the right – Otherwise: set the current node to the rhs (repeat)

BST Contains Check the current node for a match If the value is smaller, check the left subtree If the value is larger, check the right subtree If the node is a leaf and the value does not match, return False

BST iterative traversal ADT items; items.add(root); // Seed the ADT with the root while(items.has_stuff() { Node *cur = items.random_remove(); do_something(cur); items.add(cur.get_lhs()); // might fail items.add(cur.get_rhs()); // might fail }

BST Remove If the node has no children simply remove it If the node has a single child, update its parent pointer to point to its child and remove the node

Removing a node with two children Replace the value of the node with the largest value in its left-subtree (right-most descendant on the left hand side) Then repeat the remove procedure to remove the node whose value was used in the replacement

Removing a node with two children

Project 2 Add more functionality to the binary search tree – Implement ~Tree() – Implement remove(item) – Implement sorted_output() // Requires recursion – Implement distance(item_a, item_b); – Possibly implement one or two other functions (will be added later)