Week 7 - Friday.  What did we talk about last time?  Trees in general  Binary search trees.

Slides:



Advertisements
Similar presentations
Trees Types and Operations
Advertisements

S. Sudarshan Based partly on material from Fawzi Emad & Chau-Wen Tseng
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
CS 240: Data Structures Monday, July 30 th Binary Search Trees.
Binary Search Trees Chapter 7 Objectives
1 BST Trees A binary search tree is a binary tree in which every node satisfies the following: the key of every node in the left subtree is.
Week 7 - Wednesday.  What did we talk about last time?  Recursive running time  Master Theorem  Introduction to trees.
By : Budi Arifitama Pertemuan ke Objectives Upon completion you will be able to: Create and implement binary search trees Understand the operation.
Properties: -Each node has a value -The left subtree contains only values less than the parent node’s value -The right subtree contains only values greater.
Data Structures Arrays both single and multiple dimensions Stacks Queues Trees Linked Lists.
Data Structures - CSCI 102 Binary Tree In binary trees, each Node can point to two other Nodes and looks something like this: template class BTNode { public:
Thought for the Day “To become truly great, one has to stand with people, not above them.” – Charles de Montesquieu.
S EARCHING AND T REES COMP1927 Computing 15s1 Sedgewick Chapters 5, 12.
CISC220 Fall 2009 James Atlas Lecture 13: Trees. Skip Lists.
Binary Trees 2 Overview Trees. Terminology. Traversal of Binary Trees. Expression Trees. Binary Search Trees.
Binary Trees, Binary Search Trees RIZWAN REHMAN CENTRE FOR COMPUTER STUDIES DIBRUGARH UNIVERSITY.
Tree Data Structures.
Binary Search Trees Binary Search Trees (BST)  the tree from the previous slide is a special kind of binary tree called a binary.
1 Chapter 10 Trees. 2 Definition of Tree A tree is a set of linked nodes, such that there is one and only one path from a unique node (called the root.
Binary SearchTrees [CLRS] – Chap 12. What is a binary tree ? A binary tree is a linked data structure in which each node is an object that contains following.
Starting at Binary Trees
 Trees Data Structures Trees Data Structures  Trees Trees  Binary Search Trees Binary Search Trees  Binary Tree Implementation Binary Tree Implementation.
Binary trees -2 Chapter Threaded trees (depth first) Binary trees have a lot of wasted space: the leaf nodes each have 2 null pointers We can.
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.
Week 8 - Monday.  What did we talk about last time?  BST traversals  Get range implementation.
Binary Trees In computer science, a binary tree is a tree data structure in which each node has at most two children, which are referred to as the left.
David Stotts Computer Science Department UNC Chapel Hill.
Week 13 - Friday.  What did we talk about last time?  Sorting  Insertion sort  Merge sort  Started quicksort.
Discrete Mathematics Chapter 5 Trees.
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.
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.
ADT Binary Search Tree Ellen Walker CPSC 201 Data Structures Hiram College.
Binary Search Trees (BST)
Binary Search Trees … From
COSC 2P03 Week 21 Tree Traversals – reminder Breadth-first traversal: starting from root, visit all nodes on each level in turn, from left to right Depth-first.
Data Structures: A Pseudocode Approach with C, Second Edition 1 Chapter 7 Objectives Create and implement binary search trees Understand the operation.
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.
Week 7 - Wednesday.  What did we talk about last time?  Recursive running time  Master Theorem  Symbol tables.
Binary Search Trees (BST) Let’s look at some pics …and some code.
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 7 Trees_ Part2 TREES. Depth and Height 2  Let v be a node of a tree T. The depth of v is the number of ancestors of v, excluding v itself. 
BSTs, AVL Trees and Heaps Ezgi Shenqi Bran. What to know about Trees? Height of a tree Length of the longest path from root to a leaf Height of an empty.
Binary Search Trees Chapter 7 Objectives
Trees Chapter 15.
Recursive Objects (Part 4)
Week 6 - Wednesday CS221.
Binary Search Tree (BST)
Tree.
Section 8.1 Trees.
Trees.
Week 11 - Friday CS221.
Tree Traversals – reminder
Binary Search Trees Why this is a useful data structure. Terminology
Binary Trees, Binary Search Trees
Tree Traversals – reminder
Binary Tree Traversal Methods
Binary Tree Traversal Methods
Lecture 12 CS203 1.
Binary Tree Traversal Methods
Binary Trees, Binary Search Trees
Chapter 20: Binary Trees.
CO4301 – Advanced Games Development Week 4 Binary Search Trees
Binary Tree Traversal Methods
Trees.
Binary Trees, Binary Search Trees
Presentation transcript:

Week 7 - Friday

 What did we talk about last time?  Trees in general  Binary search trees

Infix to Postfix Converter

public class Tree { private static class Node { public int key; public Object value; public Node left; public Node right; } private Node root = null; … } The book uses a generic approach, with keys of type Key and values of type Value. The algorithms we'll use are the same, but I use int keys to simplify comparison.

 Almost all the methods we call on trees will be recursive  Each will take a Node reference to the root of the current subtree  Because the root is private, assume that every recursive method is called by a public, non-recursive proxy method: public Type doSomething() calls private static Type doSomething( Node root )

private static Node add(Node node, int key, Object value) Proxy: public void add(int key, Object value) { root = add( root, key, value ); } Find where the node should be. If there is already a matching key, do nothing. Otherwise, put a new node at the null location. Note: This can cause an unbalanced tree.

 Let h be the height of a binary tree  A perfect binary tree has 2 h+1 – 1 nodes  Alternatively, a perfect binary tree has 2L – 1 nodes, where L is the number of leaves  A complete binary tree has between 2 h and 2 h+1 – 1 (exclusive) nodes  A binary tree with n nodes has n + 1 null links

 Unbalanced BST:  O(n) find  O(n) insert  O(n) delete  Balanced BST:  O(log n) find  O(log n) insert  O(log n) delete

 Visiting every node in a tree is called a traversal  There are three traversals that we are interested in today:  Preorder  Postorder  Inorder  We'll get to level order traversal in the future

 Preorder:  Process the node, then recursively process its left subtree, finally recursively process its right subtree  NLR  Postorder:  Recursively process the left subtree, recursively process the right subtree, and finally process the node  LRN  Inorder:  Recursively process the left subtree, process the node, and finally recursively process the right subtree  LNR

public class Tree { private static class Node { public int key; public Object value; public Node left; public Node right; } private Node root = null; … } The book uses a generic approach, with keys of type Key and values of type Value. The algorithms we'll use are the same, but I use int keys to simplify comparison.

private static void preorder( Node node ) Proxy: public void preorder() { preorder( root ); } Just print out each node (or a dot). Real traversals will actually do something at each node.

private static void inorder( Node node ) Proxy: public void inorder() { inorder( root ); } Just print out each node (or a dot). Real traversals will actually do something at each node.

private static void postorder( Node node ) Proxy: public void postorder() { postorder( root ); } Just print out each node (or a dot). Real traversals will actually do something at each node.

 We can take the idea of an inorder traversal and use it to store a range of values into a queue  We want to store all values greater than or equal to the min and less than the max private static void getRange( Node node, Queue queue, int min, int max ) Proxy: public Queue getRange(int min, int max){ Queue queue = new ArrayDeque (); getRange( root, queue, min, max ); return queue; }

private static Node delete(Node node, int key) Proxy: public void delete(int key) { root = delete( root, key ); } 1. Find the node 2. Find its replacement (smallest right child or largest left child) 3. Swap out the replacement We may need some subroutines: private static Node smallest(Node node, Node parent) private static Node largest(Node node, Node parent) Note: This can cause an unbalanced tree.

 Finish delete  Breadth first traversal  Balancing binary search trees  2-3 search trees  Red-black trees

 Finish Project 2  Due tonight by midnight  Read section 3.3  Start Assignment 4  Due next Friday  Office hours from 3:30-5pm canceled today due to travel