Trees Lecture 12 CS2110 – Spring 2018.

Slides:



Advertisements
Similar presentations
Chapter 7. Binary Search Trees
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.
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.
Trees Isaac Sheff. Nodes Building blocks of trees “Parent” node may have “Child” nodes Can be both parent and child Can’t be its own ancestor Can’t have.
Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy Walters, and Godfrey Muganda Modified for use at Midwestern State University Chapter.
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.
Binary Tree. Some Terminologies Short review on binary tree Tree traversals Binary Search Tree (BST)‏ Questions.
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.
TREES Lecture 11 CS2110 – Spring Readings and Homework  Textbook, Chapter 23, 24  Homework: A thought problem (draw pictures!)  Suppose you use.
Binary Trees.
Non Linear Data Structure
Data Structures and Design in Java © Rick Mercer
COMP 53 – Week Fourteen Trees.
Binary Trees.
CSE 373 Binary search trees; tree height and balance
Binary Trees and Binary Search Trees
Trees Lecture 12 CS2110 – Spring 2017.
UNIT III TREES.
CISC220 Fall 2009 James Atlas Lecture 13: Binary Trees.
Week 6 - Wednesday CS221.
Problems with Linked List (as we’ve seen so far…)
Trees.
Trees Lecture 12 CS2110 – Fall 2017.
Lecture 22 Binary Search Trees Chapter 10 of textbook
Trees Lecture 12 CS2110 – Fall 2016.
abstract containers sequence/linear (1 to 1) hierarchical (1 to many)
COMP 103 Binary Search Trees.
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
Binary Search Trees.
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.
CMSC 341 Lecture 10 B-Trees Based on slides from Dr. Katherine Gibson.
Binary Trees.
CSE373: Data Structures & Algorithms Lecture 6: Binary Search Trees
CSE373: Data Structures & Algorithms Lecture 6: Binary Search Trees
Find in a linked list? first last 7  4  3  8 NULL
Search Sorted Array: Binary Search Linked List: Linear Search
Trees.
B- Trees D. Frey with apologies to Tom Anastasio
Binary Trees.
B- Trees D. Frey with apologies to Tom Anastasio
Trees Lecture 9 CS2110 – Fall 2009.
Trees Lecture 12 CS2110 – Fall 2018.
A Robust Data Structure
CMSC 202 Trees.
Lecture 36 Section 12.2 Mon, Apr 23, 2007
B- Trees D. Frey with apologies to Tom Anastasio
Binary Trees, Binary Search Trees
Non-Linear Structures
Binary Trees.
Announcements Prelim 1 on Tuesday! A4 will be posted today
CSC 143 Java Trees.
CSC 143 Binary Search Trees.
Trees.
Search Sorted Array: Binary Search Linked List: Linear Search
Binary Trees, Binary Search Trees
Data Structures Using C++ 2E
8.2 Tree Traversals Chapter 8 - Trees.
Trees Lecture 10 CS2110 – Spring 2013.
Tree (new ADT) Terminology: A tree is a collection of elements (nodes)
Presentation transcript:

Trees Lecture 12 CS2110 – Spring 2018

Important Announcements A4 is out now and due two weeks from today. Have fun, and start early! There are slots available for lunch with the professors: http://signup.com/go/myeCehh link from Piazza Recitation 6: Iterator and Iterable Watch the tutorials! Quiz due on Monday (yes, that's the day before the prelim, sorry)

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 x) lookup(int i) Array Linked List 𝑂(𝑛) 𝑂(1) 2 1 3 𝑂(1) 𝑂(𝑛) 2 1 3

The Problem of Search Search is the problem of finding an element in a data structure when you don't know where it is stored example applications: DB indexing, process scheduling (CFS Linux), set/map/dictionary (C++)

Example Data Structures add(val x) lookup(int i) Array Linked List search(val x) 𝑂(𝑛) 𝑂(1) 𝑂(𝑛) 2 1 3 𝑂(1) 𝑂(𝑛) 𝑂(𝑛) 2 1 3

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

Tree Overview Tree: data structure with nodes, similar to linked list 5 4 7 8 9 2 5 4 7 8 9 2 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 Not a tree 5 4 7 8 5 6 8 If you draw a bunch of circles and arrows, and you want to know whether you have drawn a tree… Not a tree A tree

Tree Terminology 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 M G W P J D N H B S ancestors of B descendants of W

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

Tree Terminology A node’s depth is the length of the path to the root. A tree’s (or subtree’s) height is he 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 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 Parent contains a list of its children General tree 7 8 9 7 8 3 1

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

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

Class for binary tree node class TreeNode<T> { private T value; private TreeNode<T> left, right; /** Constructor: one-node tree with datum x */ public TreeNode (T v) { value= v; left= null; right= null;} /** Constr: Tree with root value x, left tree l, right tree r */ public TreeNode (T v, TreeNode<T> l, TreeNode<T> r) { value= v; left= l; right= r; } 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.” more methods: getValue, setValue, getLeft, setLeft, etc.

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 Right subtree (also a binary tree) 9 8 3 5 7 Left subtree, which is a binary tree too

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.

Recursive Functions on Binary Trees Base case: empty tree (null) or, possibly, a leaf Recursive case: Call the function on each subtree. Use the recursive result to build a solution for the full input.

Comparing Data Structures add(val x) lookup(int i) Array Linked List search(val x) 𝑂(𝑛) 𝑂(1) 𝑂(𝑛) 2 1 3 𝑂(1) 𝑂(𝑛) 𝑂(𝑛) Binary Tree 2 1 3 2 1 3 𝑂(1) 𝑂(𝑛) 𝑂(𝑛)

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. 2 3 7 9 5 8 < 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 january

Building a BST january

Building a BST january february

Building a BST january february

Building a BST january february

Building a BST january march february

Building a BST january february march

Building a BST january april february march

Building a BST january april february march

Building a BST january february march april

Building a BST january february march april

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); } Because of ordering rules for a BST, it’s easy to print the items in alphabetical order Recursively print left subtree Print the node Recursively print right subtree show code

Binary Search Tree (BST) 2 3 7 9 5 8 Compare binary tree to binary search tree: boolean searchBT(n, v): if n==null, return false if n.v == v, return true return searchBST(n.left, v) || searchBST(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) lookup(int i) Array Linked List search(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

Inserting in Alphabetical Order april august

Inserting in Alphabetical Order april august

Inserting in Alphabetical Order april august december

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 you insert things! jan feb mar apr jun may jul