Computer Science 2: Trees

Slides:



Advertisements
Similar presentations
Info 3.3. Chapter 3.3 Recursive Data Structures Part 2 : Binary Trees.
Advertisements

Comp 122, Spring 2004 Binary Search Trees. btrees - 2 Comp 122, Spring 2004 Binary Trees  Recursive definition 1.An empty tree is a binary tree 2.A node.
Trees Types and Operations
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.
Computer Science C++ High School Level By Guillermo Moreno.
CS 206 Introduction to Computer Science II 09 / 24 / 2008 Instructor: Michael Eckmann.
Binary Trees, Binary Search Trees COMP171 Fall 2006.
CS 171: Introduction to Computer Science II
Recursion practice. Problem 0 Using recursion (and no arrays), write the code to read in a series of numbers (until EOF) and then print them backwards.
Data Structures: Trees i206 Fall 2010 John Chuang Some slides adapted from Marti Hearst, Brian Hayes, or Glenn Brookshear.
CS 104 Introduction to Computer Science and Graphics Problems Data Structure & Algorithms (4) Data Structures 11/18/2008 Yang Song.
1 Section 9.2 Tree Applications. 2 Binary Search Trees Goal is implementation of an efficient searching algorithm Binary Search Tree: –binary tree in.
CSCE 3110 Data Structures & Algorithm Analysis Binary Search Trees Reading: Chap. 4 (4.3) Weiss.
Review Binary Tree Binary Tree Representation Array Representation Link List Representation Operations on Binary Trees Traversing Binary Trees Pre-Order.
Advanced Algorithms Analysis and Design Lecture 8 (Continue Lecture 7…..) Elementry Data Structures By Engr Huma Ayub Vine.
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,
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 Search Tree Traversal Methods. How are they different from Binary Trees?  In computer science, a binary tree is a tree data structure in which.
CS 206 Introduction to Computer Science II 10 / 05 / 2009 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 02 / 13 / 2009 Instructor: Michael Eckmann.
Pascal Programming Pointers and Dynamic Variables.
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.
Trees 3 The Binary Search Tree Section 4.3. Binary Search Tree Also known as Totally Ordered Tree Definition: A binary tree B is called a binary search.
1 Binary Trees and Binary Search Trees Based on Dale & Co: Object-Oriented Data Structures using C++ (graphics)
Binary Search Trees (BST)
CIS 068 Welcome to CIS 068 ! Lesson 12: Data Structures 3 Trees.
Copyright © 2012 Pearson Education, Inc. Chapter 20: Binary Trees.
2014-T2 Lecture 27 School of Engineering and Computer Science, Victoria University of Wellington  Lindsay Groves, Marcus Frean, Peter Andreae, and Thomas.
Stacks Queues Introduction to Trees. Stacks An Everyday Example Your boss keeps bringing you important items to deal with and keeps saying: “Put that.
Question 4 Tutorial 8. Part A Insert 20, 10, 15, 5,7, 30, 25, 18, 37, 12 and 40 in sequence into an empty binary tree
(c) University of Washington20-1 CSC 143 Java Trees.
Binary Search Trees What is a binary search tree?
CSCE 3110 Data Structures & Algorithm Analysis
CSCE 3110 Data Structures & Algorithm Analysis
Recursive Objects (Part 4)
CISC220 Fall 2009 James Atlas Lecture 13: Binary Trees.
Week 6 - Wednesday CS221.
Binary Search Tree (BST)
Binary Search Tree Chapter 10.
Trees.
COMP 103 Binary Search Trees.
Tonga Institute of Higher Education
ITEC 2620M Introduction to Data Structures
Chapter 20: Binary Trees.
Chapter 21: Binary Trees.
Computer Science Dynamics.
Lesson Objectives Aims
Computer Science 2 Outline/Learning Objectives for the day:
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
Forests D. J. Foreman.
Computer Science Dynamics.
Binary Trees, Binary Search Trees
Non-Linear Structures
Binary Search Trees Chapter 9 2/24/2019 B.Ramamurthy.
Deleting from a binary tree
CSC 143 Java Trees.
Computer Science 2 More Trees.
Deleting from a binary tree
Chapter 20: Binary Trees.
2-3 Trees Extended tree. Tree in which all empty subtrees are replaced by new nodes that are called external nodes. Original nodes are called internal.
More Trees 5/9/2-017 Be able to dry run a program that uses trees
Trees.
Computer Science 2 Queue.
Time to finish first stack program
Computer Science 2 Queue Day 2.
Binary Search Trees Comp 122, Spring 2004.
Computer Science
Binary Trees, Binary Search Trees
Data Structures Using C++ 2E
Presentation transcript:

Computer Science 2: Trees

Goals/Learning Objectives/Learning Targets/… Complete a dry run that includes, dynamics, recursion, and multiple pointers! Use notes to help capture today’s thoughts. Understand the definition of a Binary Tree. Be able to demonstrate adding to a tree. Develop pseudo-code and code for adding to a tree. Develop pseudo-code and code for showing from a tree.

procedure b(top:ptrtype); begin if top^.left<> nil then program DryRunTree1; type ptrtype = ^nodetype; nodetype = record score:integer; left, right:ptrtype; end; var top:ptrtype; num:integer; procedure a(var top:ptrtype;count:integer); begin if top = nil then new(top); top^.score:=count; top^.left:=nil; top^.right:=nil; end else if count MOD 2 = 0 then a(top^.right, 2*count) else a(top^.left, 2*count); procedure b(top:ptrtype); begin if top^.left<> nil then b(top^.left); writeln(top^.score); if top^.right<> nil then b(top^.right); end; top:=nil; for num:= 1 to 5 do a(top,num); b(top); end.

Binary Trees What is it? Benefits In computer science, a binary tree is a tree data structure in which each node has at most two children. Typically the child nodes are called left and right. One common use of binary trees is binary search trees. Benefits Dynamic size Fast access (Compared to an insertion linked list) Sorts data as it is entered. …

Node 1,2,3 are children of root Definitions root Nodes 1-6 are descendants of node 0 Node 0 Node 1,2,3 are children of root Node 1 Node 2 Node 3 Node 1 is parent of Nodes 4,5 Node 4 Node 5 Node 6 leaves Node 4 and 5 are siblings

Binary Search Trees A binary tree where every node's left sub tree has values less than the node's value, and every right sub tree has values greater. 76 39 80 32 47 79

Binary Search Trees Applet: animated BST How to add to a binary search tree ? Applet: animated BST

Add Pseudo Code If top is nill Practice Adding: 50, 8, 90, 45, 12, 63, 20, 14, 71 90, 80, 70, 60, 50,40 Pseudo Code If top is nill Put it in the tree Else if bigger than or equal to top’s value Add value to top’s right Else Add value to top’s left

Developing the code for adding procedure add(var top:ptrtype;c:integer); var temp:ptrtype; begin if top=nil then new(temp); top:=temp; temp^.num:=c; temp^.left:=nil; temp^.right:=nil; end else if c>= top^.num then add(top^.right, c) else add(top^.left, c); end; Pseudo Code If top is nill Put it in the tree Else if >= top’s value Add value to top’s right Else Add value to top’s left Test it with… 30, 50, 20, 75, 15, 65 3, 6, 8, 30, 27, 90, 100

Take 3 minutes to summarize your information about Binary Trees. Definitions Parts Adding algorithm

Showing the Information What is the first number that should be shown? (Low to high) How can you get there? How about the second value? Third…? How can you do this? 76 39 80 32 47 79

Developing code for show PseudoCode Show in order (Infix) If you can go left Go left Show If you can go right Go right procedure show(top:ptrtype); begin if top^.left<> nil then show(top^.left); writeln(top^.num); if top^.right<> nil then show(top^.right); end;

Tree 1 Menu Push: Show all (High to low using the same tree) Add a name Show All (In order: Low to high) Push: Show all (High to low using the same tree) Push: Add a procedure to check is someone is in the tree. (If they are in the tree it says they are, if they are not, it says that are not.) Push: Make this a boolean function that returns true if they are in the tree and false if they are not. Push: Delete someone from the tree. Push: On the Show from a tree, have it show the information like a tree.