Trees Lecture 12 CS2110 – Fall 2018.

Slides:



Advertisements
Similar presentations
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.
Advertisements

Binary Trees, Binary Search Trees CMPS 2133 Spring 2008.
CS 206 Introduction to Computer Science II 09 / 24 / 2008 Instructor: Michael Eckmann.
Binary Trees, Binary Search Trees COMP171 Fall 2006.
CS 146: Data Structures and Algorithms June 18 Class Meeting Department of Computer Science San Jose State University Summer 2015 Instructor: Ron Mak
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.
Tree. Basic characteristic Top node = root Left and right subtree Node 1 is a parent of node 2,5,6. –Node 2 is a parent of node.
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.
TREES Lecture 12 CS2110 – Fall Announcements  Prelim #1 is tonight!  Olin 155  A-L  5:30  M-Z  5:30  A4 will be posted today  Mid-semester.
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.
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.
Week 10 - Friday.  What did we talk about last time?  Graph representations  Adjacency matrix  Adjacency lists  Depth first search.
Binary Tree. Some Terminologies Short review on binary tree Tree traversals Binary Search Tree (BST)‏ Questions.
1/14/20161 BST Operations Data Structures Ananda Gunawardena.
1 Binary Trees and Binary Search Trees Based on Dale & Co: Object-Oriented Data Structures using C++ (graphics)
Binary Search Trees (BST)
TREES K. Birman’s and G. Bebis’s Slides. Tree Overview 2  Tree: recursive data structure (similar to list)  Each cell may have zero or more successors.
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.
BINARY TREES Objectives Define trees as data structures Define the terms associated with trees Discuss tree traversal algorithms Discuss a binary.
CS Prelim Review – 10/15/09  First prelim is TOMORROW at 7:30 PM  Review session – Tonight 7:30 PM, Phillips 101  Things you should do:  Review every.
TREES Lecture 11 CS2110 – Spring Readings and Homework  Textbook, Chapter 23, 24  Homework: A thought problem (draw pictures!)  Suppose you use.
(c) University of Washington20-1 CSC 143 Java Trees.
Non Linear Data Structure
Data Structures and Design in Java © Rick Mercer
COMP 53 – Week Fourteen Trees.
CMSC 341 Introduction to Trees 8/3/2007 CMSC 341 Tree Intro.
Binary Trees and Binary Search Trees
Trees Lecture 12 CS2110 – Spring 2017.
Week 6 - Wednesday CS221.
Trees Lecture 12 CS2110 – Fall 2017.
Lecture 22 Binary Search Trees Chapter 10 of textbook
Trees Lecture 12 CS2110 – Fall 2016.
Trees.
Week 11 - Friday CS221.
Hashing Exercises.
abstract containers sequence/linear (1 to 1) hierarchical (1 to many)
ITEC 2620M Introduction to Data Structures
i206: Lecture 13: Recursion, continued Trees
Binary Search Trees Why this is a useful data structure. Terminology
Binary Trees, Binary Search Trees
Chapter 22 : Binary Trees, AVL Trees, and Priority Queues
Data Structures and Database Applications Binary Trees in C#
Map interface Empty() - return true if the map is empty; else return false Size() - return the number of elements in the map Find(key) - if there is an.
Trees Lecture 12 CS2110 – Spring 2018.
CMSC 341 Lecture 10 Binary Search Trees
CMSC 341 Lecture 10 B-Trees Based on slides from Dr. Katherine Gibson.
Binary Trees.
General Trees & Binary Trees
Find in a linked list? first last 7  4  3  8 NULL
Trees.
Binary Trees.
B- Trees D. Frey with apologies to Tom Anastasio
Trees Lecture 9 CS2110 – Fall 2009.
Lecture 12 CS203 1.
General Trees & Binary Trees
Binary Trees, Binary Search Trees
CMSC 341 Introduction to Trees CMSC 341 Tree Intro.
Announcements Prelim 1 on Tuesday! A4 will be posted today
CSC 143 Java Trees.
CSC 143 Binary Search Trees.
Binary SearchTrees [CLRS] – Chap 12.
Yan Shi CS/SE 2630 Lecture Notes
Tree.
Trees.
Binary Trees, Binary Search Trees
Data Structures Using C++ 2E
Trees Lecture 10 CS2110 – Spring 2013.
Tree (new ADT) Terminology: A tree is a collection of elements (nodes)
Presentation transcript:

Trees Lecture 12 CS2110 – Fall 2018

Prelim Updates Regrades are live until next Thursday @ 11:59PM A few rubric changes are happening Recursion question: -0pts if you continued to print Exception handling “write the output of execution of that statement” – rubrics change in place

Data Structures There are different ways of storing data, called data structures Each data structure has operations that it is good at and operations that it is bad at For any application, you want to choose a data structure that is good at the things you do often

Example Data Structures add(val v) get(int i) Array Linked List contains(val v) 𝑂(𝑛) 𝑂(1) 𝑂(𝑛) 2 1 3 𝑂(1) 𝑂(𝑛) 𝑂(𝑛) 2 1 3 add(v): append v to this list get(i): return element at position i in this list contains(v): return true if this list contains v AKA add, lookup, search

Tree Singly linked list: Today: trees! 2 1 4 1 2 Node object pointer Node object pointer int value 4 1 2 Today: trees!

Tree Overview A tree or not a tree? Tree: data structure with nodes, similar to linked list Each node may have zero or more successors (children) Each node has exactly one predecessor (parent) except the root, which has none All nodes are reachable from root A tree or not a tree? 5 4 2 7 8 9 5 4 2 7 8 9 A tree Not a tree If you draw a bunch of circles and arrows, and you want to know whether you have drawn a tree… 5 4 7 8 5 6 7 Not a tree A tree

Tree Terminology (1) the root of the tree (no parents) child of M W P J D N H B S child of M child of M the leaves of the tree (no children)

Tree Terminology (2) ancestors of B descendants of W M G W P J D N H B

Tree Terminology (3) M G W P J D N H B S subtree of M

Tree Terminology (4) A node’s depth is the length of the path to the root. A tree’s (or subtree’s) height is the length of the longest path from the root to a leaf. M G W P J D N H B S depth 1 height 2 depth 3 height 0

Tree Terminology (5) Multiple trees: a forest G W P J D N H B S

Class for general tree nodes class GTreeNode<T> { private T value; private List<GTreeNode<T>> children; //appropriate constructors, getters, //setters, etc. } 5 4 2 9 8 7 3 1 <T> means user picks a type when they create one (later lecture) Parent contains a list of its children General tree

Class for general tree nodes class GTreeNode<T> { private T value; private List<GTreeNode<T>> children; //appropriate constructors, getters, //setters, etc. } 5 4 2 9 8 7 3 1 Java.util.List is an interface! It defines the methods that all implementations must implement. Whoever writes this class gets to decide what implementation to use — ArrayList? LinkedList? Etc.? General tree

Binary Trees A binary tree is a particularly important kind of tree in which every node as at most two children. In a binary tree, the two children are called the left and right children. 5 4 2 7 8 9 5 4 2 7 9 Not a binary tree (a general tree) Binary tree

Binary trees were in A1! You have seen a binary tree in A1. A PhD object has one or two advisors. (Note: the advisors are the “children”.) David Gries Friedrich Bauer Georg Aumann Fritz Bopp Fritz Sauter Erwin Fues Heinrich Tietze Constantin Carathodory

Useful facts about binary trees 2 depth 2 9 8 3 5 1 5 Height 2, minimum number of nodes 2 Height 2, maximum number of nodes Max # of nodes at depth d: 2d If height of tree is h: min # of nodes: h + 1 max #of nodes: 20 + … + 2h = 2h+1 – 1 Complete binary tree Every level, except last, is completely filled, nodes on bottom level as far left as possible. No holes.

Class for binary tree node class TreeNode<T> { private T datum; private TreeNode<T> left, right; /** Constructor: one-node tree with datum d */ public TreeNode (T d) {datum= d; left= null; right= null;} /** Constr: Tree with root datum d, left tree l, right tree r */ public TreeNode (T d, TreeNode<T> l, TreeNode<T> r) { datum= d; left= l; right= r; } // more methods: getValue, setValue, getLeft, setLeft, etc. Either might be null if the subtree is empty. Binary trees are a special case of general trees, but they are *so common* that we will sometimes just say “tree” when we really mean “binary tree.”

Binary versus general tree In a binary tree, each node has up to two pointers: to the left subtree and to the right subtree: One or both could be null, meaning the subtree is empty (remember, a tree is a set of nodes) In a general tree, a node can have any number of child nodes (and they need not be ordered) Very useful in some situations ... ... one of which may be in an assignment!

A Tree is a Recursive Thing A binary tree is either null or an object consisting of a value, a left binary tree, and a right binary tree.

Looking at trees recursively Binary Tree 2 9 8 3 5 7 Right subtree (also a binary tree) Left subtree, which is also a binary tree

Looking at trees recursively a binary tree

Looking at trees recursively value right subtree left subtree

Looking at trees recursively value

A Recipe for Recursive Functions Base case: If the input is “easy,” just solve the problem directly. Recursive case: Get a smaller part of the input (or several parts). Call the function on the smaller value(s). Use the recursive result to build a solution for the full input.

A Recipe for Recursive Functions on Binary Trees an empty tree (null), or possibly a leaf Base case: If the input is “easy,” just solve the problem directly. Recursive case: Get a smaller part of the input (or several parts). Call the function on the smaller value(s). Use the recursive result to build a solution for the full input. each subtree

Searching in a Binary Tree /** Return true iff x is the datum in a node of tree t*/ public static boolean treeSearch(T x, TreeNode<T> t) { if (t == null) return false; if (x.equals(t.datum)) return true; return treeSearch(x, t.left) || treeSearch(x, t.right); } Analog of linear search in lists: given tree and an object, find out if object is stored in tree Easy to write recursively, harder to write iteratively 2 9 8 3 5 7 We sometimes talk of the root of the tree, t. But we also use t to denote the whole tree.

Comparing Data Structures add(val v) get(int i) Array Linked List contains(val v) 𝑂(𝑛) 𝑂(1) 𝑂(𝑛) 2 1 3 Binary Tree 𝑂(1) 𝑂(𝑛) 𝑂(𝑛) 2 1 3 2 1 3 𝑂(1) 𝑂(𝑛) 𝑂(𝑛) Node you seek could be anywhere in the tree; have to search the whole thing. Index set by pre-determined traversal order (see slide 36); have to go through the whole tree (no short cut like array indexing)

Binary Search Tree (BST) A binary search tree is a binary tree that is ordered and has no duplicate values. In other words, for every node: All nodes in the left subtree have values that are less than the value in that node, and All values in the right subtree are greater. 5 2 8 3 7 9 <5 >5 A BST is the key to making search way faster.

Building a BST To insert a new item: Pretend to look for the item Put the new node in the place where you fall off the tree

Building a BST insert: January Note: Inserting them chronologically, (January, then February…) but the BST places them alphabetically (Feb comes before Jan, etc.)

Building a BST insert: February january

Building a BST insert: March january february

Building a BST insert: April january february march

Building a BST january february march april june may august july september december october november

Printing contents of BST /** Print BST t in alpha order */ private static void print(TreeNode<T> t) { if (t == null) return; print(t.left); System.out.print(t.value); print(t.right); } show code Because of ordering rules for BST, easy to print alphabetically Recursively print left subtree Print the root Recursively print right subtree

Tree traversals Other standard kinds of traversals preorder traversal Process root Process left subtree Process right subtree postorder traversal level-order traversal Not recursive: uses a queue (we’ll cover this later) “Walking” over the whole tree is a tree traversal Done often enough that there are standard names Previous example: in-order traversal Process left subtree Process root Process right subtree Note: Can do other processing besides printing

Binary Search Tree (BST) 5 2 8 3 7 9 Compare binary tree to binary search tree: boolean searchBT(n, v): if n == null, return false if n.v == v, return true return searchBT(n.left, v) || searchBT(n.right, v) boolean searchBST(n, v): if n == null, return false if n.v == v, return true if v < n.v return searchBST(n.left, v) else return searchBST(n.right, v) Here's the thing: if the tree is perfectly balanced, so there's the same amount of stuff on either side of any node, then this is *asymptotically* very fast. The height of the tree is log_2 n. And you only have to traverse *at most* the height of the tree! So searching is O(log n) if the tree is balanced. 2 recursive calls 1 recursive call

Comparing Data Structures add(val x) get(int i) Array Linked List contains(val x) 𝑂(𝑛) 𝑂(1) 𝑂(𝑛) 2 1 3 𝑂(1) Binary Tree BST 𝑂(𝑛) 𝑂(𝑛) 2 1 3 1 2 3 𝑂(1) 𝑂(𝑛) 𝑂(𝑛) 2 1 3 𝑂(𝑑𝑒𝑝𝑡ℎ) 𝑂(𝑑𝑒𝑝𝑡ℎ) 𝑂(𝑑𝑒𝑝𝑡ℎ)

Inserting in Alphabetical Order april

Inserting in Alphabetical Order april august

Inserting in Alphabetical Order april august december february january

Insertion Order Matters A balanced binary tree is one where the two subtrees of any node are about the same size. Searching a binary search tree takes O(h) time, where h is the height of the tree. In a balanced binary search tree, this is O(log n). But if you insert data in sorted order, the tree becomes imbalanced, so searching is O(n).

Things to think about What if we want to delete data from a BST? A BST works great as long as it’s balanced. There are kinds of trees that can automatically keep themselves balanced as things are inserted! jan feb mar apr jun may jul