Data Structures: Binary Trees CMSC 11500 Introduction to Computer Programming October 28, 2002.

Slides:



Advertisements
Similar presentations
Interval Heaps Complete binary tree. Each node (except possibly last one) has 2 elements. Last node has 1 or 2 elements. Let a and b be the elements in.
Advertisements

Info 3.3. Chapter 3.3 Recursive Data Structures Part 2 : Binary Trees.
Multi-way Trees CS 5010 Program Design Paradigms “Bootcamp” Lesson 6.6 © Mitchell Wand, This work is licensed under a Creative Commons Attribution-NonCommercial.
Heapsort Idea: two phases: 1. Construction of the heap 2. Output of the heap For ordering number in an ascending sequence: use a Heap with reverse.
6.001 SICP SICP – October Trees Trevor Darrell 32-D512 Office Hour: W web page:
Department of Computer Science University of Maryland, College Park
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 47 Red Black Trees.
CS 104 Introduction to Computer Science and Graphics Problems Data Structure & Algorithms (4) Data Structures 11/18/2008 Yang Song.
6.001 SICP SICP – October Introduction Trevor Darrell 32-D512 Office Hour: W web page:
Razdan CST230http://dcst2.east.asu.edu/~razdan/cst230/ Razdan with contribution from others 1 Chapter 9 Trees Anshuman Razdan Div of Computing Studies.
Trees. 2 Definition of a tree A tree is a node with a value and zero or more children Depending on the needs of the program, the children may or may not.
Quiz: Box and Pointer fun! (cons (cons (cons ‘hey (cons ‘there nil)) nil) (cons ‘wow nil)) (list ‘boo (append (list ‘hoo ‘hoo) (cons ‘see ‘me)))
Chapter 9 contd. Binary Search Trees Anshuman Razdan Div of Computing Studies
1 Section 9.2 Tree Applications. 2 Binary Search Trees Goal is implementation of an efficient searching algorithm Binary Search Tree: –binary tree in.
Advanced Topics in Algorithms and Data Structures 1 An example.
From Templates to Folds CS 5010 Program Design Paradigms “Bootcamp” Lesson © Mitchell Wand, This work is licensed under a Creative Commons.
PPL Pairs, lists and data abstraction. Data Abstraction? An interface: separate implementation from usage Think of the Map interface in Java: we know.
Solving Your Problem by Generalization CS 5010 Program Design Paradigms “Bootcamp” Lesson 7.1 © Mitchell Wand, This work is licensed under a.
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.
Min Chen School of Computer Science and Engineering Seoul National University Data Structure: Chapter 7.
Arbitrarily Long Data Structures: Lists and Recursion CMSC Introduction to Computer Programming October 4, 2002.
Scheme: Lists Cont’d Chapter 9-10 of HTDP Ms. Knudtzon September 26.
Lecture Objectives  To learn how to use a tree to represent a hierarchical organization of information  To learn how to use recursion to process trees.
Recursion: Linear and Tree Recursive Processes and Iteration CMSC Introduction to Computer Programming October 7, 2002.
Assignment: Changing Data CMSC Introduction to Computer Programming November 1, 2002.
Binary Search Trees Binary Search Trees (BST)  the tree from the previous slide is a special kind of binary tree called a binary.
Mutual Recursion: Web pages CMSC Introduction to Computer Programming November 25, 2002.
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.
Abstraction: Procedures as Parameters CMSC Introduction to Computer Programming October 14, 2002.
More examples of invariants CS 5010 Program Design Paradigms “Bootcamp” Lesson © Mitchell Wand, This work is licensed under a Creative.
Data Abstraction: Sets Binary Search Trees CMSC Introduction to Computer Programming October 30, 2002.
Computer Science 112 Fundamentals of Programming II Introduction to Trees.
Solving Your Problem by Generalization CS 5010 Program Design Paradigms “Bootcamp” Lesson 7.1 © Mitchell Wand, This work is licensed under a.
Rudiments of Trees a Joshua presentation DATA STRUCTURES.
Trees By P.Naga Srinivasu M.tech,(MBA). Basic Tree Concepts A tree consists of finite set of elements, called nodes, and a finite set of directed lines.
ADT Binary Search Tree Ellen Walker CPSC 201 Data Structures Hiram College.
PPL CPS. Moed A 2007 Solution (define scale-tree (λ (tree factor) (map (λ (sub-tree) (if (list? sub-tree) (scale-tree sub-tree factor) (* sub-tree.
Meta-Circular Evaluation CMSC Introduction to Computer Programming November 20, 2002.
Data Abstraction: Sets
Chapter 47 Red Black Trees
Chapter 48 Red Black Trees
CS 5010 Program Design Paradigms “Bootcamp” Lesson 5.2
Fundamentals of Programming II Introduction to Trees
Recursive Objects (Part 4)
Representing Sets (2.3.3) Huffman Encoding Trees (2.3.4)
Binary search tree. Removing a node
Binary Search Tree (BST)
Source Code for Data Structures and Algorithm Analysis in C (Second Edition) – by Weiss
CS 5010 Program Design Paradigms “Bootcamp” Lesson 5.1
From Templates to Folds
Objective: Understand Concepts related to trees.
Section 8.1 Trees.
Data Structures & Algorithm Design
Trees.
Trees.
Trees.
Chapter 43 Red Black Trees
Binary Trees CS-2851 Dr. Mark L. Hornick.
CMSC 341 K-D Trees.
Lecture 36 Section 12.2 Mon, Apr 23, 2007
Binary Search Trees Chapter 9 2/22/2019 B.Ramamurthy.
CMSC 341 K-D Trees.
CS 5010 Program Design Paradigms “Bootcamp” Lesson 6.5
CMSC 341 K-D Trees.
Trees.
Developing Programs for Family Trees
Tree and its terminologies
Changing Data: (Continued)
list data list 만들기 list 사용하기 nil : list link :  * list -> list
Bringing it all Together: Family Trees
Presentation transcript:

Data Structures: Binary Trees CMSC Introduction to Computer Programming October 28, 2002

Roadmap Recap: Family trees Generalizing: Binary trees –Data definition –Template Functions on Binary Trees –Contains? –Map-tree –Fringe Summary

Family Trees Data definition: –(define-stuct ft (name eye-color mother father)) Where name, eye-color: symbols, mother, father are… –Family-tree is 1) ‘unknown, or 2) (make-ft name eye-color mother father) (define nana (make-ft ‘alice ‘blue ‘unknown ‘unknown)) (define ma (make-ft ‘anna ‘brown nana pap)) Functions: –Blue-eyed ancestor, all-blue-eyed-ancestors, how-deep

Binary Trees More general form –Nodes with two self-references –Family trees -> self-refs= mother, father Data definition: –(define-struct bt (val left right)) Where val:number, left, right are binary-tree –Binary-tree is: #f, or (make-bt val left right)

Binary Tree Examples Vocabulary: –If bt-left & bt-right both #f, “leaf” node –If not bt-left or bt-right of other node, “root” (define leaf1 (make-bt 1 #f #f)) (define leaf2 (make-bt 2 #f #f)) (define leaf3 (make-bt 3 #f #f)) (define mid12 (make-bt 12 leaf1 leaf2)) (define root (make-bt 123 mid12 leaf3))

Binary Tree Graphic

Binary Tree Template Questions: How many conditions?, How many parts?, How many & what self-references? (define (fn-for-btree abt) –(cond ((eq? abt #f) ….) ((bt? abt) (cond ((eq? (bt-val abt) …) ….) …. (fn-for-btree (bt-left abt)) … …. (fn-for-btree (bt-right abt))….))))))

Functions on Binary Trees Is x in the tree? –Contains? Do something to every element in the tree –map-tree Find the nodes with no successors –fringe

Contains? Contract: binary-tree -> boolean Purpose: To determine if a given value is in the tree

Contains? (define (contains? testnum abt) (cond ((eq? abt #f) #f) ((bt? abt) (cond ((eq? (bt-val abt) testnum) #t) (else (or (contains? testnum (bt-left abt)) (contains? testnum (bt-right abt))))))))

Map-tree Analogous to map for flat lists Applies some function to every element Contract:map-tree –(number->number) binary-tree -> binary-tree Purpose: –To apply function to every element of tree

Map-tree (define (map-tree func abt) (cond ((eq? abt #f) #f) ((bt? abt) (make-bt (func (bt-val abt)) (map-tree func (bt-left abt)) (map-tree func (bt-right abt)))))

Using Map-tree Square-tree: –Contract: square-tree: binary-tree -> binary-tree –Purpose: Square all numbers in tree Double-tree: –Contract: double-tree: binary-tree -> binary-tree –Purpose: Double all numbers in tree

Using Map-tree (define (square-tree abt) (map-tree square abt)) (define (double-tree abt) (map-tree (lambda (x) (+ x x)) abt))

Next Time Binary Search Trees –Invariants –Efficient set implementation