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 &

Slides:



Advertisements
Similar presentations
CS261 Data Structures AVL Trees. Goals Pros/Cons of a BST AVL Solution – Height-Balanced Trees.
Advertisements

Trees Types and Operations
AVL Tree Smt Genap Outline AVL Tree ◦ Definition ◦ Properties ◦ Operations Smt Genap
Binary Trees, Binary Search Trees CMPS 2133 Spring 2008.
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.
1 Binary heaps binary tree that satisfy two properties –structural property (is a complete tree) –heap-ordering property (minimum item on top) Can have.
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.
TCSS 342 BST v1.01 BST addElement To addElement(), we essentially do a find( ). When we reach a null pointer, we create a new node there. void addElement(Object.
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.
CSE373: Data Structures & Algorithms Lecture 6: Binary Search Trees Nicki Dell Spring 2014 CSE373: Data Structures & Algorithms1.
Version TCSS 342, Winter 2006 Lecture Notes Trees Binary Trees Binary Search Trees.
CSE373: Data Structures & Algorithms Lecture 6: Binary Search Trees Lauren Milne Summer
Binary Search Trees. BST Properties Have all properties of binary tree Items in left subtree are smaller than items in any node Items in right subtree.
1 Search Trees - Motivation Assume you would like to store several (key, value) pairs in a data structure that would support the following operations efficiently.
Chapter 19 - basic definitions - order statistics ( findkth( ) ) - balanced binary search trees - Java implementations Binary Search Trees 1CSCI 3333 Data.
BINARY SEARCH TREE. Binary Trees A binary tree is a tree in which no node can have more than two children. In this case we can keep direct links to the.
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.
Lec 15 Oct 18 Binary Search Trees (Chapter 5 of text)
Preview  Graph  Tree Binary Tree Binary Search Tree Binary Search Tree Property Binary Search Tree functions  In-order walk  Pre-order walk  Post-order.
Oct 26, 2001CSE 373, Autumn A Forest of Trees Binary search trees: simple. –good on average: O(log n) –bad in the worst case: O(n) AVL trees: more.
Data Structures AVL 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.
1 CSE 326: Data Structures Trees Lecture 6: Friday, Jan 17, 2003.
CSE373: Data Structures & Algorithms Lecture 6: Binary Search Trees Linda Shapiro Winter 2015.
DAST Tirgul 6. What is a Binary Search Tree? The keys in a binary search tree (BST) are always stored in such a way as to satisfy the search property:
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.
Balancing Binary Search Trees. Balanced Binary Search Trees A BST is perfectly balanced if, for every node, the difference between the number of nodes.
AVL Trees CSE, POSTECH.
CSE 373, Copyright S. Tanimoto, 2002 Binary Search Trees -
AA Trees.
BCA-II Data Structure Using C
Binary Search Trees A binary search tree is a binary tree
BST Trees
Binary search tree. Removing a node
Lecture 15 AVL Trees Slides modified from © 2010 Goodrich, Tamassia & by Prof. Naveen Garg’s Lectures.
Balancing Binary Search Trees
Binary Search Trees.
CSIT 402 Data Structures II
CS202 - Fundamental Structures of Computer Science II
CS202 - Fundamental Structures of Computer Science II
CS 201 Data Structures and Algorithms
Binary Search Trees.
AVL Tree Mohammad Asad Abbasi Lecture 12
AVL Tree 27th Mar 2007.
Binary Search Trees Why this is a useful data structure. Terminology
Binary Trees, Binary Search Trees
AVL Tree A Balanced Binary Search Tree
CSE373: Data Structures & Algorithms Lecture 6: Binary Search Trees
CSE373: Data Structures & Algorithms Lecture 6: Binary Search Trees
AVL Trees CENG 213 Data Structures.
Lec 12 March 9, 11 Mid-term # 1 (March 21?)
CS202 - Fundamental Structures of Computer Science II
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.
CSE 373: Data Structures and Algorithms
CSE 373, Copyright S. Tanimoto, 2002 Binary Search Trees -
AVL Trees CSE 373 Data Structures.
CSE 373 Data Structures and Algorithms
Lecture No.20 Data Structures Dr. Sohail Aslam
CE 221 Data Structures and Algorithms
CS202 - Fundamental Structures of Computer Science II
Lecture 9: Self Balancing Trees
Binary SearchTrees [CLRS] – Chap 12.
Lecture 10 Oct 1, 2012 Complete BST deletion Height-balanced BST
CSE 373 Data Structures Lecture 8
Depth of an AVL tree Theorem: Any AVL tree with n nodes has height less than log n. Proof: Given an n-node AVL tree, we want to find an upper bound.
CS 106B Lecture 20: Binary Search Trees Wednesday, May 17, 2017
CS202 - Fundamental Structures of Computer Science II
CS202 - Fundamental Structures of Computer Science II
Tree (new ADT) Terminology: A tree is a collection of elements (nodes)
Presentation transcript:

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 & x, BinaryNode<Comp> * & t) const; { if (t == NULL) t = new BinaryNode<Comp>(x, NULL, NULL); else if (x < t->element) insert(x, t->left); else if (x > t->element) insert(x, t->right); else ; // duplicate; do appropriate thing } Can be implemented iteratively. Oct 22, 2001 CSE 373, Autumn 2001

findMin, findMax To find the maximum element in the BST, we follow right children until we reach NULL. To find the minimum element in the BST, we follow left children until we reach NULL. 5 4 8 1 7 11 3 Oct 22, 2001 CSE 373, Autumn 2001

BST remove Removing an item disrupts the tree structure. Basic idea: find the node that is to be removed. Then “fix” the tree so that it is still a binary search tree. Three cases: node has no children node has one child node has two children Oct 22, 2001 CSE 373, Autumn 2001

No children, one child 5 4 8 1 7 11 3 5 4 8 1 7 11 3 Oct 22, 2001 CSE 373, Autumn 2001

Two children Replace the node with its successor. Then remove the successor from the tree. 5 4 8 1 7 11 3 7 4 8 1 7 11 3 Oct 22, 2001 CSE 373, Autumn 2001

Height of BSTs n-node BST: Worst case depth: n-1. Claim: The maximum number of nodes in a binary tree of height h is 2h+1 – 1. Proof: The proof is by induction on h. For h = 0, the tree has one node, which is equal to 20+1 – 1. Suppose the claim is true for any tree of height h. Any tree of height h+1 has at most two subtrees of height h. By the induction hypothesis, this tree has at most 2 (2h+1 – 1)+1 = 2h+2 – 1. Oct 22, 2001 CSE 373, Autumn 2001

Height of BSTs, cont’d If we have a BST of n nodes and height h, then by the Claim, n  2h+1 – 1. So, h  log (n+1) – 1. Average depth of nodes in a tree. Assumptions: insert items randomly (with equal likelihood); each item is equally likely to be looked up. Internal path length: the sum of the depths of all nodes. Oct 22, 2001 CSE 373, Autumn 2001

Average Depth Let D(n) be the internal path length of some tree with n nodes. The left subtree has i nodes, and the right subtree has n–i–1 nodes. D(1) = 0 D(n) = D(i) + D(n – i – 1) + n – 1 r TR TL Oct 22, 2001 CSE 373, Autumn 2001

D(n)  1.442 n log n How long does it take to insert the items 1, 2, 3, …, n (in that order) into a BST? What if they were inserted so that the resulting BST were perfectly balanced? Oct 22, 2001 CSE 373, Autumn 2001

AVL Trees Motivation: we want to guarantee O(log n) running time on the find/insert/remove operations. Idea: keep the tree balanced after each operation. Solution: AVL (Adelson-Velskii and Landis) trees. AVL tree property: for every node in the tree, the height of the left and right subtrees differs by at most 1. Oct 22, 2001 CSE 373, Autumn 2001

6 4 8 5 1 7 11 AVL tree 3 6 4 8 1 5 7 11 3 not an AVL tree 2 Oct 22, 2001 CSE 373, Autumn 2001

AVL trees: find, insert AVL tree find is the same as BST find. AVL insert: same as BST insert, except that we might have to “fix” the AVL tree after an insert. These operations will take time O(d), where d is the depth of the node being found/inserted. What is the maximum height of an n-node AVL tree? Oct 22, 2001 CSE 373, Autumn 2001