Binary Trees: Practice Problems

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

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

Binary Trees, Binary Search Trees

Consider the search operation FindKey (): find an element of a particular key value in a binary tree. This operation takes O(n) time in a binary

Chapter 20: Binary Trees

Programming II (CS300)

We have the pointers reference the next node in an inorder traversal; called threads

INF2220: algorithms and data structures Series 1

CS350: Data Structures Binary Search Trees

BST Implementation. Data Structures. Lecture 4 Binary search trees (BST) Dr. Mahmoud Attia Sakr University of Ain Shams

Introduction to Trees. D. Thiebaut CSC212 Fall 2014

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

Advanced Java Concepts Unit 5: Trees. Notes and Exercises

TREES. Trees - Introduction

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

Programming Languages and Techniques (CIS120)

CS 350 : Data Structures Binary Search Trees

CS350: Data Structures Tree Traversal

Some Search Structures. Balanced Search Trees. Binary Search Trees. A Binary Search Tree. Review Binary Search Trees

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

Multi-Way Search Tree

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

Title Description Participants Textbook

Binary Trees. Examples:

CS 151. Binary Search Trees. Wednesday, October 10, 12

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

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

CS 151. Binary Trees. Friday, October 5, 12

Data Structures and Algorithms

examines every node of a list until a matching node is found, or until all nodes have been examined and no match is found.

Friday Four Square! 4:15PM, Outside Gates

Algorithms. Deleting from Red-Black Trees B-Trees

CS 231 Data Structures and Algorithms Fall Binary Search Trees Lecture 23 October 29, Prof. Zadia Codabux

Binary Search Trees. See Section 11.1 of the text.

: Advanced Programming Final Exam Summer 2008 June 27, 2008

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

Binary Trees

CS24 Week 8 Lecture 1

Algorithms and Data Structures CS-CO-412

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

Data Structures in Java

Binary Tree Applications

Trees. Truong Tuan Anh CSE-HCMUT

Data Structure Lecture#10: Binary Trees (Chapter 5) U Kang Seoul National University

CSI33 Data Structures

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

The Problem with Linked Lists. Topic 18. Attendance Question 1. Binary Search Trees. -Monty Python and The Holy Grail

Tree Structures. A hierarchical data structure whose point of entry is the root node

Tree traversals and binary trees

Outline. Computer Science 331. Insertion: An Example. A Recursive Insertion Algorithm. Binary Search Trees Insertion and Deletion.

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

Also, recursive methods are usually declared private, and require a public non-recursive method to initiate them.

CS 206 Introduction to Computer Science II

Motivation Computer Information Systems Storage Retrieval Updates. Binary Search Trees. OrderedStructures. Binary Search Tree

Trees, Binary Trees, and Binary Search Trees

Why Trees? Alternatives. Want: Ordered arrays. Linked lists. A data structure that has quick insertion/deletion, as well as fast search

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

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

MIDTERM WEEK - 9. Question 1 : Implement a MyQueue class which implements a queue using two stacks.

CS 231 Data Structures and Algorithms Fall Recursion and Binary Trees Lecture 21 October 24, Prof. Zadia Codabux

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

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

CE 221 Data Structures and Algorithms

BINARY SEARCH TREES cs2420 Introduction to Algorithms and Data Structures Spring 2015

MULTIMEDIA COLLEGE JALAN GURNEY KIRI KUALA LUMPUR

B-Trees. Based on materials by D. Frey and T. Anastasio

CSI33 Data Structures

Balanced Search Trees. CS 3110 Fall 2010

Data Structures. Giri Narasimhan Office: ECS 254A Phone: x-3748

8. Binary Search Tree

Advanced Java Concepts Unit 5: Trees. Notes and Exercises

March 20/2003 Jayakanth Srinivasan,

Spring 2018 Mentoring 8: March 14, Binary Trees

IX. Binary Trees (Chapter 10)

CS 112 Introduction to Computing II. Wayne Snyder Computer Science Department Boston University

STUDENT LESSON AB30 Binary Search Trees

binary tree empty root subtrees Node Children Edge Parent Ancestor Descendant Path Depth Height Level Leaf Node Internal Node Subtree

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

Lecture 27. Binary Search Trees. Binary Search Trees

Algorithms. AVL Tree

CSE 373 Midterm 2 2/27/06 Sample Solution. Question 1. (6 points) (a) What is the load factor of a hash table? (Give a definition.

I2206 Data Structures TS 6: Binary Trees - Binary Search Trees

Binary Search Tree 1.0. Generated by Doxygen Mon Jun :12:39

* Due 11:59pm on Sunday 10/4 for Monday lab and Tuesday 10/6 Wednesday Lab

(2,4) Trees. 2/22/2006 (2,4) Trees 1

Building Java Programs

CISC 235 Topic 3. General Trees, Binary Trees, Binary Search Trees

Algorithms and Data Structures

CS 206 Introduction to Computer Science II

Cpt S 122 Data Structures. Data Structures Trees

Why Do We Need Trees?


ECE 242. Data Structures

Data Structures. Binary Trees. Root Level = 0. number of leaves:?? leaves Depth (Maximum level of the tree) leaves or nodes. Level=1.

Trees 11/15/16. Chapter 11. Terminology. Terminology. Terminology. Terminology. Terminology

CSI33 Data Structures

Data Structures Brett Bernstein

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

void insert( Type const & ) void push_front( Type const & )

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

Transcription:

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

Warmup Problem 1: Searching for a node public boolean recursivesearch(int data) { return(recursivesearch(root, data)); private boolean recursivesearch(intbstnode p, int data) { if (p == null) { // meaning, there is no node! return(false); if (data == p.data) { return(true); else if (data < p.data) { return(recursivesearch(p.left, data)); else { return(recursivesearch(p.right, data)); Binary Trees: Practice Problems page 2

Warmup Problem 2: Searching for a node in an arbitrary tree Not a BST Doesn t have the ordering property public boolean searchtree(int data) { return(searchtree(root, data)); private boolean searchtree(intbstnode p, int data) { if (p == null) { // meaning, there is no node! return(false); if (data == p.data) { return(true); return (searchtree(p.left, data) searchtree(p.right, data)); Binary Trees: Practice Problems page 3

Warmup Problem 3: Summing the values of nodes in a tree public int sumnodes() { return(sumnodes(root)); private int sumnodes(intbstnode p) { if (p == null) return(0); else { return p.data + sumnodes(p.left) + sumnodes(p.right); Binary Trees: Practice Problems page 4

Count Nodes: Write a method that counts (and returns) the number of nodes in a binary tree public int countnodes() { return(countnodes(root)); private int countnodes(intbstnode p) { if (p == null) return(0); else { return 1 + countnodes(p.left) + countnodes(p.right); Binary Trees: Practice Problems page 5

Count Leaf Nodes: Write a method that counts (and returns) the number of leaf nodes in a binary tree public int numleaves () { return(numleaves(root)); public int numleaves(intbstnode p) { if (p!= NULL) { if (p.left == NULL && p.right == NULL) return 1; else return numleaves(p.left) + numleaves(p.right); else return 0; Binary Trees: Practice Problems page 6

Print Even Nodes: Write a method that prints out all even nodes in a binary search tree private void printeven(intbstnode p) { if (p!= null) { if (p.data % 2 == 0) System.out.print(p.data + ); printeven(p.left); printeven(p.right); This is basically just a traversal Except we added a condition (IF) statement before the print statement Binary Trees: Practice Problems page 7

Print Odd Nodes (in ascending order): Write a function that prints out all odd nodes, in a binary search tree, in ascending order private void printoddasc(intbstnode p) { if (p!= null) { printoddasc(p.left); if (p.data % 2 == 1) System.out.print(p.data + ); printoddasc(p.right); The question requested ascending order This requires an inorder traversal So we simply changed the order of the statements Binary Trees: Practice Problems page 8

Brief Interlude: FAIL Picture Binary Trees: Practice Problems page 9

Compute Height: Write a recursive method to compute the height of a tree Defined as the length of the longest path from the root to a leaf node For the purposes of this problem, a tree with only one node has height 1 and an empty tree has height 0 Binary Trees: Practice Problems page 10

Compute Height: private int height(intbstnode root) { int leftheight, rightheight; if (root == null) return 0; leftheight = height(root.left); rightheight = height(root.right); if (leftheight > rightheight) return leftheight + 1; return rightheight + 1; Binary Trees: Practice Problems page 11

Find Largest: Write a recursive method that returns a pointer to the node containing the largest element in a BST This one should be easy: This is a BST, meaning it has the ordering property So where is the largest node located either the root or the greatest node in the right subtree Binary Trees: Practice Problems page 12

Find Largest: private intbstnode largest(intbstnode B) { // if B is NULL, there is no node if (B == null) return null; // If B s right is NULL, that means B is the largest else if (B.right == null) return B; // SO if B s right was NOT equal to NULL, // There is a right subtree of B. // Which means that the largest value is in this // subtree. So recursively call B s right. else return largest(b.right); Binary Trees: Practice Problems page 13

Number of Single Children: In a binary tree, each node can have zero, one, or two children Write a recursive method that returns the number of nodes with a single child Binary Trees: Practice Problems page 14

Number of Single Children: public int one(intbstnode p) { if (p!= NULL) { if (p.left == null) if (p.right!= null) return 1 + one(p.right); else if (p.right == null) if (p.left!= null) return 1 + one(p.left); else return one(p.left) + one(p.right); Binary Trees: Practice Problems page 15

WASN T THAT SPICY! Binary Trees: Practice Problems page 16

Daily Demotivator Binary Trees: Practice Problems page 17

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