CSE 373 Data Structures and Algorithms

Slides:



Advertisements
Similar presentations
CSE 326: Data Structures Binary Search Trees Ben Lerner Summer 2007.
Advertisements

AVL Trees ITCS6114 Algorithms and Data Structures.
Building Java Programs Binary Search Trees reading: 17.3 – 17.4.
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.
CSCE 3110 Data Structures & Algorithm Analysis Binary Search Trees Reading: Chap. 4 (4.3) Weiss.
Building Java Programs Binary Search Trees; TreeSet.
CSE 143 Lecture 20 Binary Search Trees continued; Tree Sets read slides created by Marty Stepp and Hélène Martin
AVL Trees Neil Ghani University of Strathclyde. General Trees Recall a tree is * A leaf storing an integer * A node storing a left subtree, an integer.
AVL Trees CSCI 2720 Fall 2005 Kraemer. Binary Tree Issue  One major problem with the binary trees we have discussed thus far: they can become extremely.
Topic 15 The Binary Search Tree ADT Binary Search Tree A binary search tree (BST) is a binary tree with an ordering property of its elements, such.
1 Trees 4: AVL Trees Section 4.4. Motivation When building a binary search tree, what type of trees would we like? Example: 3, 5, 8, 20, 18, 13, 22 2.
Binary Search Trees Nilanjan Banerjee. 2 Goal of today’s lecture Learn about Binary Search Trees Discuss the first midterm.
CSE 143 Lecture 22 Binary Search Trees continued; Tree Sets read slides adapted from Marty Stepp and Hélène Martin
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.
Data Structures AVL Trees.
CSE 143 Lecture 21 Binary Search Trees, continued read slides created by Marty Stepp
Binary Search Trees (BST)
CSE 143 Lecture 20 Binary Search Trees read 17.3 slides created by Marty Stepp
(c) University of Washington20c-1 CSC 143 Binary Search Trees.
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.
Instructor: Lilian de Greef Quarter: Summer 2017
CSCE 3110 Data Structures & Algorithm Analysis
CSCE 3110 Data Structures & Algorithm Analysis
CSE 143 Lecture 20 Binary Search Trees continued; Tree Sets
CSE 373 Binary search trees; tree height and balance
Recursive Objects (Part 4)
UNIT III TREES.
CSIT 402 Data Structures II
Week 6 - Wednesday CS221.
Binary Search Trees.
COMP 103 Binary Search Trees.
Trees (Chapter 4) Binary Search Trees - Review Definition
Binary Search Trees.
Tree data structure.
Binary Search Trees Why this is a useful data structure. Terminology
Chapter 22 : Binary Trees, AVL Trees, and Priority Queues
Binary Search Trees.
Bit Vector Linked List (Sorted and Unsorted) Hash Table Search Trees
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.
Building Java Programs
Building Java Programs
CSE 373 AVL trees read: Weiss Ch. 4, section
Binary Search Tree AVL Tree
Tree data structure.
CSE 373: Data Structures and Algorithms
CS 367 – Introduction to Data Structures
CSE 373: Data Structures and Algorithms
CSE 373: Data Structures and Algorithms
CSE 332: Data Abstractions Binary Search Trees
CSE 373: Data Structures and Algorithms
CSE 373 Data Structures and Algorithms
CSE 373: Data Structures and Algorithms
CSE 373: Data Structures and Algorithms
AVL Trees CSE 373 Data Structures.
Binary Search Trees.
Building Java Programs
CSE 373: Data Structures and Algorithms
Binary Search Trees continued; Tree Sets
Lecture 21: Binary Search Trees; TreeSet
CSC 143 Binary Search Trees.
Building Java Programs
BST Insert To insert an element, we essentially do a find( ). When we reach a NULL pointer, we create a new node there. void BST::insert(const Comp &
ITCS6114 Algorithms and Data Structures
Lecture 21: Binary Search Trees; TreeSet
Trees.
Lecture 10: BST and AVL Trees
CSE 373 Data Structures Lecture 8
Lecture 9: Intro to Trees
CS 261 – Data Structures AVL Trees.
Presentation transcript:

CSE 373 Data Structures and Algorithms Lecture 10: Trees II

Implementing Set with BST Each Set entry adds a node to tree Node contains String element, references to left/right subtree Tree organized for binary search Quickly search or place to insert/remove element

Implementing Set with BST (cont.) public interface StringSet { public boolean add(String value); public boolean contains(String value); public void print(); public boolean remove(String value); public int size(); }

StringTreeSet class Client code talks to the // A StringTreeSet represents a Set of Strings. public class StringTreeSet { private StringTreeNode root; // null for an empty set methods } Client code talks to the StringTreeSet, not to the node objects inside it Methods of the StringTreeSet create and manipulate the nodes, their data and links between them "X" "M" "P" "I" "L" "J" "C" root

Set implementation: contains public boolean contains(String value) { return contains(root, value); } private boolean contains(StringTreeNode node, String value) { if (node == null) { return false; // not in set } else if (node.data.compareTo(value) == 0) { return true; // found! } else if (node.data.compareTo(value) > 0) { return contains(node.left, value); // search left } else { return contains(node.right, value); // search right

Set implementation: insert Starts like contains Trace out path where node should be Add node as new leaf Don't change any other nodes or references Correct place to maintain binary search tree property

Set implementation: insert public boolean add(String value) { int oldSize = size(); this.root = add(root, value); return oldSize != size(); } private StringTreeNode add(StringTreeNode node, String value) { if (node == null) { node = new StringTreeNode(value); numElements++; } else if (node.data.compareTo(value) == 0) { return node; } else if (node.data.compareTo(value) > 0) { node.left = add(node.left, value); } else { node.right = add(node.right, value);

Set implementation: remove Possible states for the node to be removed: a leaf: replace with null a node with a left child only: replace with left child a node with a right child only: replace with right child a node with both children: replace with min value from right "X" "M" "P" "I" "L" "J" "C" root "X" "P" "I" "M" "J" "C" root set.remove("L");

Set implementation: remove public boolean remove(String value) { int oldSize = numElements; root = remove(root, value); return oldSize > numElements; } protected StreeNode remove(StreeNode node, String value) { if (node == null) { return node; } else if (node.data.compareTo(value) < 0) { node.right = remove(node.right, value); } else if (node.data.compareTo(value) > 0) { node.left = remove(node.left, value); } else { if (node.right != null && node.left != null) { node.data = getMinValue(node.right); node.right = remove(node.right, node.data); } else if (node.right != null) { node = node.right; numElements--; node = node.left; return node;

Evaluate Set as BST Space used Runtime Overhead of two references per entry BST adds nodes as needed; no excess capacity Runtime add, contains take time proportional to tree height height expected to be O(log N) Why? Let’s talk about tree height first…

Tree height calculation Height is max number of edges from root to leaf height(null) = -1 height(1) = 0 height(A)? Hint: it's recursive! A hleft hright height(t) = 1 + max {h_left,h_right} OK, let’s get a bit of practice working with trees. Almost anything we do on a tree will be most easily thought of as recursive (even if we implement it iteratively) Height = max ( height(left), height(right) ) + 1 Height (null) = -1 Runtime: O(N) visit each node once.

A Balanced Tree Values: 2 8 14 15 18 20 21 Order added: 15, 8, 2, 20, 21, 14, 18 Different tree structures possible Depends on order inserted 7 nodes, expected height log 7 ≈ 3 Perfectly balanced 21 18 20 8 15 14 2 root

Mostly Balanced Tree Same Values: 2 8 14 15 18 20 21 Order added: 20, 8, 21, 18, 14, 15, 2 Mostly balanced, height 4 root 15 14 21 8 20 18 2

Degenerate Tree Same Values: 2 8 14 15 18 20 21 Order added: 2, 8, 14, 15, 18, 20, 21 Totally unbalanced, height 6 root 2 8 14 15 18 20 21

Binary Trees: Some Numbers 2/28/2019 Binary Trees: Some Numbers Recall: height of a tree = length of longest path from the root to a leaf. For binary tree of height h: max # of leaves: max # of nodes: min # of leaves: min # of nodes: 2h 2h, for perfect tree 2(h + 1) - 1 2h+1 – 1, for perfect tree 1 Some tree calculations, for tree of height h: - Max number of leaves (perfect tree): 2^h - Max number of nodes (perfect tree): 2^(h+1) - 1 - Min number of nodes/leaves (degenerate tree): 1 - What fraction of the tree is located in the last level of a perfect tree? ½ 1, for “list” tree h + 1 h+1, for “list” tree

Implementing Set ADT (Revisited) Insert Remove Search Unsorted array (1) (n) Sorted array (log(n)+n) (log(n) + n) (log(n)) Linked list BST (if balanced) (log n)

AVL Tree Motivation Observation: the shallower the BST the better For a BST with n nodes Average case height is (log n) Worst case height is (n) Simple cases such as insert(1, 2, 3, ..., n) lead to the worst case scenario: height is (n) Strategy: Don't let the tree get lopsided Constantly monitor balance for each subtree Rebalance subtree before going too far astray

Balanced Tree Balanced Tree: a tree in which heights of subtrees are approximately equal unbalanced tree balanced tree

Tree Balance and Height (a) The balanced tree has a height of: __________ (b) The unbalanced tree has a height of:__________