Binary Trees CS 110: Data Structures and Algorithms First Semester, 2010-2011.

Slides:



Advertisements
Similar presentations
Splay Trees Binary search trees.
Advertisements

Heaps1 Part-D2 Heaps Heaps2 Recall Priority Queue ADT (§ 7.1.3) A priority queue stores a collection of entries Each entry is a pair (key, value)
S. Sudarshan Based partly on material from Fawzi Emad & Chau-Wen Tseng
CS 206 Introduction to Computer Science II 03 / 23 / 2009 Instructor: Michael Eckmann.
Binary Trees Chapter 6. Linked Lists Suck By now you realize that the title to this slide is true… By now you realize that the title to this slide is.
1 Trees Tree nomenclature Implementation strategies Traversals –Depth-first –Breadth-first Implementing binary trees Reading: L&C 9.1 – 9.7.
CS 104 Introduction to Computer Science and Graphics Problems Data Structure & Algorithms (4) Data Structures 11/18/2008 Yang Song.
CS 206 Introduction to Computer Science II 11 / 05 / 2008 Instructor: Michael Eckmann.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved L12 (Chapter 20) Lists, Stacks,
CSE 326: Data Structures Lecture #6 (end of Lists, then) Trees Alon Halevy Spring Quarter 2001.
Source: Muangsin / Weiss1 Priority Queue (Heap) A kind of queue Dequeue gets element with the highest priority Priority is based on a comparable value.
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.
1 Foundations of Software Design Fall 2002 Marti Hearst Lecture 17: Binary Search Trees; Heaps.
Binary Trees Chapter 6.
1 Chapter 18 Trees Objective To learn general trees and recursion binary trees and recursion tree traversal.
Data Structures Arrays both single and multiple dimensions Stacks Queues Trees Linked Lists.
Binary Tree. Binary Trees – An Informal Definition A binary tree is a tree in which no node can have more than two children Each node has 0, 1, or 2 children.
CHAPTER 71 TREE. Binary Tree A binary tree T is a finite set of one or more nodes such that: (a) T is empty or (b) There is a specially designated node.
Trees, Binary Search Trees, Recursion, Project 2 Bryce Boe 2013/08/01 CS24, Summer 2013 C.
Data Structures - CSCI 102 Binary Tree In binary trees, each Node can point to two other Nodes and looks something like this: template class BTNode { public:
1 Trees Tree nomenclature Implementation strategies Traversals –Depth-first –Breadth-first Implementing binary search trees.
12-CRS-0106 REVISED 8 FEB 2013 CSG2A3 ALGORITMA dan STRUKTUR DATA.
Lecture 10 Trees –Definiton of trees –Uses of trees –Operations on a tree.
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.
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 ),
INTRODUCTION TO BINARY TREES P SORTING  Review of Linear Search: –again, begin with first element and search through list until finding element,
For Monday Read Weiss, chapter 7, sections 1-3. Homework –Weiss, chapter 4, exercise 6. Make sure you include parentheses where appropriate.
Chapter 21 Priority Queue: Binary Heap Saurav Karmakar.
Tree Data Structures.
P p Chapter 10 has several programming projects, including a project that uses heaps. p p This presentation shows you what a heap is, and demonstrates.
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 CS 105. L9: Trees Slide 2 Definition The Tree Data Structure stores objects (nodes) hierarchically nodes have parent-child relationships operations.
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.
Advance Data Structure 1 College Of Mathematic & Computer Sciences 1 Computer Sciences Department م. م علي عبد الكريم حبيب.
Lecture - 10 on Data Structures. 6:05:57 PM Prepared by, Jesmin Akhter, Lecturer, IIT,JU.
CPSC 252 Binary Heaps Page 1 Binary Heaps A complete binary tree is a binary tree that satisfies the following properties: - every level, except possibly.
IKI 10100I: Data Structures & Algorithms Ruli Manurung (acknowledgments to Denny & Ade Azurat) 1 Fasilkom UI Ruli Manurung (Fasilkom UI)IKI10100I: Data.
Binary Tree 10/22/081. Tree A nonlinear data structure Contain a distinguished node R, called the root of tree and a set of subtrees. Two nodes n1 and.
Week 10 - Friday.  What did we talk about last time?  Graph representations  Adjacency matrix  Adjacency lists  Depth first search.
Heaps and basic data structures David Kauchak cs161 Summer 2009.
Week 15 – Wednesday.  What did we talk about last time?  Review up to Exam 1.
Copyright © 2012 Pearson Education, Inc. Chapter 20: Binary Trees.
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.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 20: Binary Trees.
Week 7 - Wednesday.  What did we talk about last time?  Recursive running time  Master Theorem  Symbol tables.
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.
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.
Chapter 7 Trees_ Part2 TREES. Depth and Height 2  Let v be a node of a tree T. The depth of v is the number of ancestors of v, excluding v itself. 
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 20: Binary Trees.
18-1 Chapter 18 Binary Trees Data Structures and Design in Java © Rick Mercer.
IKI 10100: Data Structures & Algorithms Ruli Manurung (acknowledgments to Denny & Ade Azurat) 1 Fasilkom UI Ruli Manurung (Fasilkom UI)IKI10100: Lecture13.
Trees Saurav Karmakar
Recursive Objects (Part 4)
CISC220 Fall 2009 James Atlas Lecture 13: Binary Trees.
Chapter 10.1 Binary Search Trees
Cse 373 April 14th – TreEs pt 2.
Heaps © 2010 Goodrich, Tamassia Heaps Heaps
March 31 – Priority Queues and the heap
Splay Trees Binary search trees.
Chapter 20: Binary Trees.
Data Structures and Database Applications Binary Trees in C#
Chapter 21: Binary Trees.
Tree A tree is a data structure in which each node is comprised of some data as well as node pointers to child nodes
Trees.
Trees CMSC 202, Version 5/02.
Binary Tree Traversals
CMSC 202 Trees.
Chapter 20: Binary Trees.
Binary Tree Implementation And Applications
Heaps.
Presentation transcript:

Binary Trees CS 110: Data Structures and Algorithms First Semester,

Binary Tree ► An ordered tree with just two children ► Distinguished between “left” and “right” child ► Has a lot of uses, like heaps, expression trees, etc.

Binary Tree Properties ► Let ► n – number of nodes ► e – external nodes ► i – internal nodes ► h – height ► Properties ► e = i + 1 ► n = 2e – 1 ► h ≤ i ► h ≤ (n-1) / 2 ► e ≤ 2 n ► h ≥ log 2 e ► h ≥ log 2 (n+1) - 1

Binary Tree Implementation ► Array Implementation ► Elements are stored in an array ► Relationships are derived using indices ► Linked List Implementation ► Elements stored in a TreeNode, similar to the Node class used in Stack and Queue ► Each TreeNode has pointers to the element it contains, and then TreeNode references to its children ► Ideas here can be extended to implement other types of trees

Binary Tree Implementation ► Each element is stored in an array ► Root of the array is at array[0] ► Left child of the node at index j is at array[2j+1] ► Right child of the node at index j is at array[2j+2] ► How do you compute for the parent of the node at index j?

Binary Tree Array Example A BC D E H GF ABCDEFGH

Linked List Implementation ► Each element is stored in a TreeNode object ► TreeNode has “left” and “right” TreeNode fields, which correspond to its children ► TreeNode also has a “parent” field

BT Linked List Implementation Ø A Ø ØØØØØØ BC DEG leftright

Tree Search Algorithms ► Given a root of a tree, find a particular element ► Two approaches ► Breadth First Search ► Depth First Search

Breadth First Search ► Search per level ► Root first ► Children of root ► Children of the children of the root ► Etc ► Search left’s children before right’s ► Can be applied to other trees

Breadth First Search A BC D E H GF ► Top to bottom, left to right ► A, B, C, D, E, F, G, H

Breadth First Search ► Implementation requires a Queue data structure ► Put the root in the queue ► Dequeue an element from the queue ► If it’s the element we’re looking for, return ► Otherwise, queue its left and right child ► Repeat until the queue is empty, or until the element is found

Breadth First Search public TreeNode BFS(TreeNode root, Object element) { Queue q = new NodeQueue(); q.enqueue(root); while( !q.isEmpty() ) { TreeNode n = (TreeNode) q.dequeue(); if ( n.getElement().equals(element) ) { return n; } if ( q.getLeft() != null ) q.enqueue( q.getLeft() ); if ( q.getRight() != null ) q.enqueue( q.getRight() ); }

Depth First Search ► Start from the root, search downward ► Explore as far as possible until the goal node is reached, or there are no more children ► Backtrack when there are no more children ► Search the left branch before the right branch

Depth First Search A BC D E H GF ► Drill down, then go back, then right ► A, B, D, E, H, C, F, G

Depth First Search ► Adapt the code from the BFS, except use a Stack instead of a Queue ► Push the right child FIRST ► Better solution is to use recursion ► Check the current node’s element ► If it is not what is being searched ► DFS subtree rooted at the left child ► DFS subtree rooted at the right child

Depth First Search public TreeNode DFS(TreeNode root, Object element) { if ( root.getElement().equals(element) ) { return root; } TreeNode ret = null; if ( root.getLeft() != null ) { ret = DFS( root.getLeft(), element ); } if ( ret != null ) return ret; if ( root.getRight() != null ) { ret = DFS( root.getRight(), element ); } return ret; }