The const Keyword The answer is that, yes, we don’t want the function to change the parameter, but neither do we want to use up time and memory creating.

Slides:



Advertisements
Similar presentations
AVL Trees When bad trees happen to good programmers.
Advertisements

Lecture 9 : Balanced Search Trees Bong-Soo Sohn Assistant Professor School of Computer Science and Engineering Chung-Ang University.
CPSC 252 AVL Trees Page 1 AVL Trees Motivation: We have seen that when data is inserted into a BST in sorted order, the BST contains only one branch (it.
ECE 250 Algorithms and Data Structures Douglas Wilhelm Harder, M.Math. LEL Department of Electrical and Computer Engineering University of Waterloo Waterloo,
EECS 311: Chapter 4 Notes Chris Riesbeck EECS Northwestern.
AVL-Trees (Part 1) COMP171. AVL Trees / Slide 2 * Data, a set of elements * Data structure, a structured set of elements, linear, tree, graph, … * Linear:
CS2420: Lecture 24 Vladimir Kulyukin Computer Science Department Utah State University.
TCSS 342 AVL Trees v1.01 AVL Trees Motivation: we want to guarantee O(log n) running time on the find/insert/remove operations. Idea: keep the tree balanced.
CS2420: Lecture 27 Vladimir Kulyukin Computer Science Department Utah State University.
CS 206 Introduction to Computer Science II 12 / 01 / 2008 Instructor: Michael Eckmann.
B + -Trees (Part 1) Lecture 20 COMP171 Fall 2006.
B + -Trees (Part 1). Motivation AVL tree with N nodes is an excellent data structure for searching, indexing, etc. –The Big-Oh analysis shows most operations.
AVL trees. AVL Trees We have seen that all operations depend on the depth of the tree. We don’t want trees with nodes which have large height This can.
B + -Trees (Part 1) COMP171. Slide 2 Main and secondary memories  Secondary storage device is much, much slower than the main RAM  Pages and blocks.
CS 206 Introduction to Computer Science II 11 / 24 / 2008 Instructor: Michael Eckmann.
CSC 2300 Data Structures & Algorithms February 13, 2007 Chapter 4. Trees.
Balanced Trees. Binary Search tree with a balance condition Why? For every node in the tree, the height of its left and right subtrees must differ by.
Chapter 4: Trees Binary Search Trees
Data Structures Using C++ 2E Chapter 11 Binary Trees and B-Trees.
B + -Trees COMP171 Fall AVL Trees / Slide 2 Dictionary for Secondary storage * The AVL tree is an excellent dictionary structure when the entire.
1 B-Trees Section AVL (Adelson-Velskii and Landis) Trees AVL tree is binary search tree with balance condition –To ensure depth of the tree is.
Binary Tree. Binary Trees – An Informal Definition A binary tree is a tree in which no node can have more than two children Each node has 0, 1, or 2 children.
More Trees Multiway Trees and 2-4 Trees. Motivation of Multi-way Trees Main memory vs. disk ◦ Assumptions so far: ◦ We have assumed that we can store.
Chapter 19 - basic definitions - order statistics ( findkth( ) ) - balanced binary search trees - Java implementations Binary Search Trees 1CSCI 3333 Data.
Compiled by: Dr. Mohammad Alhawarat BST, Priority Queue, Heaps - Heapsort CHAPTER 07.
Chapter 19: Binary Trees. Objectives In this chapter, you will: – Learn about binary trees – Explore various binary tree traversal algorithms – Organize.
B + -Trees. Motivation An AVL tree with N nodes is an excellent data structure for searching, indexing, etc. The Big-Oh analysis shows that most operations.
Compiled by: Dr. Mohammad Omar Alhawarat
MA/CSSE 473 Day 21 AVL Tree Maximum height 2-3 Trees Student questions?
Data Structures: A Pseudocode Approach with C, Second Edition1 Objectives Upon completion you will be able to: Explain the differences between a BST and.
CompSci 100E 41.1 Balanced Binary Search Trees  Pathological BST  Insert nodes from ordered list  Search: O(___) ?  The Balanced Tree  Binary Tree.
CIS 068 Welcome to CIS 068 ! Lesson 12: Data Structures 3 Trees.
Lecture 10COMPSCI.220.FS.T Binary Search Tree BST converts a static binary search into a dynamic binary search allowing to efficiently insert and.
CSE 3358 NOTE SET 13 Data Structures and Algorithms.
AVL TREES By Asami Enomoto CS 146 AVL Tree is… named after Adelson-Velskii and Landis the first dynamically balanced trees to be propose Binary search.
AVL Tree: Balanced Binary Search Tree 9.
Data Structures and Algorithms
Binary Search Trees A binary search tree is a binary tree
Lecture No.13 Data Structures Dr. Sohail Aslam
AVL Trees.
CSIT 402 Data Structures II
Balanced Binary Search Trees
Introduction Applications Balance Factor Rotations Deletion Example
Chapter 26 AVL Trees Jung Soo (Sue) Lim Cal State LA.
CS 201 Data Structures and Algorithms
Chapter 29 AVL Trees.
Balanced Trees AVL : Adelson-Velskii and Landis(1962)
MA/CSSE 473 Day 21 AVL Tree Maximum height 2-3 Trees
Lecture 18. Basics and types of Trees
CSCS-200 Data Structures and Algorithms
Trees (Chapter 4) Binary Search Trees - Review Definition
Binary Search Trees.
CSE 373 AVL trees read: Weiss Ch. 4, section
AVL Trees CENG 213 Data Structures.
CS202 - Fundamental Structures of Computer Science II
Balanced-Trees This presentation shows you the potential problem of unbalanced tree and show two way to fix it This lecture introduces heaps, which are.
Reference Variables The symbol “&” has a few different purposes depending on where it occurs in code. When it appears in front of a variable name, it is.
CSE 373: Data Structures and Algorithms
TREES– Interesting problems
Balanced-Trees This presentation shows you the potential problem of unbalanced tree and show two way to fix it This lecture introduces heaps, which are.
Data Structures & Algorithms
AVL Trees CSE 373 Data Structures.
CSE 373 Data Structures and Algorithms
Lecture No.20 Data Structures Dr. Sohail Aslam
Lecture 9: Self Balancing Trees
AVL Tree By Rajanikanth B.
Data Structures Lecture 21 Sohail Aslam.
AVL-Trees (Part 1).
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 &
CSE 373 Data Structures Lecture 8
Presentation transcript:

The const Keyword The answer is that, yes, we don’t want the function to change the parameter, but neither do we want to use up time and memory creating and storing an entire copy of it. So, we make the original object available to the called function by using pass-by-reference. We also mark it constant so that the function will not alter it, even by mistake. Start of lecture 19

Lecture No.19 Data Structure Dr. Sohail Aslam

The const Keyword Use 2: The const keyword appears at the end of class member’s function signature: EType& findMin( ) const; Such a function cannot change or write to member variables of that class. This type of usage often appears in functions that are suppose to read and return member variables.

The const Keyword Use 3: The const keyword appears at the beginning of the return type in function signature: const EType& findMin( ) const; Means, whatever is returned is constant. The purpose is typically to protect a reference variable. This also avoids returning a copy of an object.

Degenerate Binary Search Tree BST for 14, 15, 4, 9, 7, 18, 3, 5, 16, 20, 17 14 4 15 3 9 18 7 16 20 5 17

Degenerate Binary Search Tree BST for 3 4 5 7 9 14 15 16 17 18 20 14 15 4 9 7 18 3 5 16 20 17

Degenerate Binary Search Tree BST for 3 4 5 7 9 14 15 16 17 18 20 14 15 4 9 7 18 3 5 16 20 17 Linked List!

Balanced BST We should keep the tree balanced. One idea would be to have the left and right subtrees have the same height

Balanced BST Does not force the tree to be shallow. 14 15 18 16 20 17 9 7 3 5 Does not force the tree to be shallow.

Balanced BST We could insist that every node must have left and right subtrees of same height. But this requires that the tree be a complete binary tree To do this, there must have (2d+1 – 1) data items, where d is the depth of the tree. This is too rigid a condition.

AVL Tree AVL (Adelson-Velskii and Landis) tree. An AVL tree is identical to a BST except height of the left and right subtrees can differ by at most 1. height of an empty tree is defined to be (–1). Start of 20 (Nov 29)

AVL Tree An AVL Tree level 5 2 8 1 1 4 7 2 3 3

AVL Tree Not an AVL tree level 6 1 8 1 1 4 2 3 5 3

Balanced Binary Tree The height of a binary tree is the maximum level of its leaves (also called the depth). The balance of a node in a binary tree is defined as the height of its left subtree minus height of its right subtree. Here, for example, is a balanced tree. Each node has an indicated balance of 1, 0, or –1.

Balanced Binary Tree -1 1 1 -1 End of 19