Trees 7/14/2009.

Slides:



Advertisements
Similar presentations
CS Fall 2012, Lab 08 Haohan Zhu. Boston University Slideshow Title Goes Here CS Fall 2012, Lab /17/2015 Tree - Data Structure  Basic.
Advertisements

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.
Binary Trees, Binary Search Trees CMPS 2133 Spring 2008.
Binary Trees, Binary Search Trees COMP171 Fall 2006.
CS 171: Introduction to Computer Science II
Lists A list is a finite, ordered sequence of data items. Two Implementations –Arrays –Linked Lists.
© 2006 Pearson Addison-Wesley. All rights reserved11 A-1 Chapter 11 Trees.
CSC 2300 Data Structures & Algorithms February 6, 2007 Chapter 4. Trees.
By : Budi Arifitama Pertemuan ke Objectives Upon completion you will be able to: Create and implement binary search trees Understand the operation.
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.
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.
CS 61B Data Structures and Programming Methodology July 15, 2008 David Sun.
Lecture 10 Trees –Definiton of trees –Uses of trees –Operations on a tree.
CISC220 Fall 2009 James Atlas Lecture 13: Trees. Skip Lists.
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.
Computer Science: A Structured Programming Approach Using C Trees Trees are used extensively in computer science to represent algebraic formulas;
Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy Walters, and Godfrey Muganda Modified for use at Midwestern State University Chapter.
Computer Science 112 Fundamentals of Programming II Introduction to Trees.
Binary Search Trees Data Structures Ananda Gunawardena
Min Chen School of Computer Science and Engineering Seoul National University Data Structure: Chapter 6.
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.
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.
M180: Data Structures & Algorithms in Java Trees & Binary Trees Arab Open University 1.
Binary Tree. Some Terminologies Short review on binary tree Tree traversals Binary Search Tree (BST)‏ Questions.
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.
Data Structures Lakshmish Ramaswamy. Tree Hierarchical data structure Several real-world systems have hierarchical concepts –Physical and biological systems.
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.
TREES General trees Binary trees Binary search trees AVL trees Balanced and Threaded 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.
Trees By JJ Shepherd. Introduction Last time we discussed searching and sorting in a more efficient way Divide and Conquer – Binary Search – Merge Sort.
Chapter 11. Chapter Summary  Introduction to trees (11.1)  Application of trees (11.2)  Tree traversal (11.3)  Spanning trees (11.4)
Tree Representation and Terminology Binary Trees Binary Search Trees Pointer-Based Representation of a Binary Tree Array-Based Representation of a Binary.
TREES From root to leaf. Trees  A tree is a non-linear collection  The elements are in a hierarchical arrangement  The elements are not accessible.
Binary Search Trees Chapter 7 Objectives
Trees Saurav Karmakar
Chapter 10 Trees © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved.
Data Structures and Design in Java © Rick Mercer
CSCE 210 Data Structures and Algorithms
Chapter 25 Binary Search Trees
Week 6 - Wednesday CS221.
Binary Search Tree (BST)
Tree.
Binary Trees "A tree may grow a thousand feet tall, but its leaves will return to its roots." -Chinese Proverb.
CMSC 341 Introduction to Trees.
Section 8.1 Trees.
Data Structures & Algorithm Design
Lecture 18. Basics and types of Trees
ITEC 2620M Introduction to Data Structures
i206: Lecture 13: Recursion, continued Trees
Binary Trees, Binary Search Trees
Binary Tree and General Tree
Data Structures and Database Applications Binary Trees in C#
TREES General trees Binary trees Binary search trees AVL trees
CS223 Advanced Data Structures and Algorithms
7/23/2009 Many thanks to David Sun for some of the included slides!
Trees.
Topic 18 Binary Trees "A tree may grow a thousand feet tall, but its leaves will return to its roots." -Chinese Proverb.
Trees CMSC 202, Version 5/02.
CMSC 202 Trees.
Binary Search Trees 7/16/2009.
Binary Trees, Binary Search Trees
Trees.
Chapter 20: Binary Trees.
Binary Trees.
Binary Trees, Binary Search Trees
Data Structures Using C++ 2E
Presentation transcript:

Trees 7/14/2009

Important Dates Project 2 due Midterm Review Midterm 2 DESIGN, DESIGN, DESIGN!!! Friday 7/24/2009 – 10pm Midterm Review Saturday 7/26/2009 – 1-4pm in 306 Soda Midterm 2 Tuesday 7/28/2009 – 5-6pm in 10 Evans Important Dates

Don’t let this happen to you! DESIGN FAIL Don’t let this happen to you!

(souped-up LLs) Trees

VOCAB Tree Node Path Edge Root Rooted Tree Parent Child Leaf Sibling Ancestor Descendent Path Length Depth Height Subtree VOCAB

Tree Definitions: Overview A tree consists of a set of nodes and a set of edges that connect pairs of nodes. There is exactly one path between any two nodes of the tree. A path is a connected sequence of zero or more edges.

Tree Definitions: Rooted Tree In a rooted tree, one distinguished node is called the root. Every node c, except the root, has exactly one parent node p, which is the first node traversed on the path from c to the root. c is p's child. The root has no parent. A node can have any number of children.

Tree Definitions: More A leaf is a node with no children. Siblings are nodes with the same parent. The ancestors of a node d are the nodes on the path from d to the root. Technically, the ancestors of d also include d itself. The root is an ancestor of every node in the tree. If a is an ancestor of d, then d is a descendant of a.

Definitions The length of a path is the number of edges in the path. The depth of a node d is the length of the path from d to the root. The depth of the root is zero. The height of a node d is the length of the path from d to its deepest descendant. The height of a leaf node is zero. The height of a tree = height of the root. The subtree rooted at node d is the tree formed by d and its descendants.

Announcements

Announcements (Text) Please recycle your paper – there is a bin in the printer room Please throw away your trash Please use the power strips. NEVER use the floorboards!!!!

One Amoeba Object Amoeba Amoeba myParent String myName ArrayList<Amoeba> myKids “Pete”

An Amoeba Subtree “Pete” Amoeba myParent String myName ArrayList<Amoeba> myKids “Pete”

Design 1 (How we think about it)

Design 2 (Amoeba)

Design 3 – LinkedList of Children

Binary-Trees

Binary Tree A binary tree is a tree in which no node has more than two children, and every child is either a left child or a right child, even if it's the only child its parent has.

Traversal Preorder: visit node, traverse its children. Postorder: traverse children, visit node. Inorder: traverse first child, visit node, traverse second child (binary trees only).

Preorder: visit node, traverse its children. As you pass LEFT 2 → 7 → 12 → 6 → 5 → 11 → 15 → 9 → 4 “PRE” => start with “ME”

Postorder: traverse children, visit node As you pass RIGHT 12 → 5 → 11 → 6 → 7 → 4 → 9 → 15 → 2 “POST” => be a good “HOST”

Inorder: traverse first child, visit node, traverse second child (binary trees only). As you pass BELOW 12 → 7 → 5 → 6 → 11 → 2 → 15 → 4 → 9

Level Order: Breadth First One depth at a time 2 → 7 → 15 → 12 → 6 → 9 → 5 → 11 → 4

Level-Order (Breadth-First) Traversal Traverse all nodes at depth 0, then depth 1… Unlike the other traversals, this algorithm is not naturally recursive Use a queue, which initially contains only the root. Then repeat the following steps: Dequeue a node from the front of the list. Visit it. Enqueue its children (in order from left to right). Continue until the queue is empty.

Binary Search Tree BST

Binary Search Tree Binary Search Tree is a binary tree. Generally, each node of the Binary Search Tree contains an <Key, Value> pair called an Entry. The key can be the same as the value. Binary Search Tree satisfies the following property, called the binary search tree invariant: For any node X, every key in the left subtree of X is less than or equal to X's key, and every key in the right subtree of X is greater than or equal to X's key. A key equal to the parent's key can go into either subtree. Example.

Inserting insert() starts by following the same path through the tree as find(). When it reaches a null reference, replace the null with a reference to a new node <Key, Value> . Duplicate keys are allowed. If insert() finds a node that already has the key, it puts it the new entry in the left subtree of the older one. We could just as easily choose the right subtree; it doesn't matter.

Debugging Your TA will help you LEARN to use the debugger! Your TA will not debug your code! SORRY! Your FRIENDS will help you learn to use the debugger AND help you debug your code!!! AWESOME!