1 Binary Trees Informal defn: each node has 0, 1, or 2 children Informal defn: each node has 0, 1, or 2 children Formal defn: a binary tree is a structure.

Slides:



Advertisements
Similar presentations
Chapter 12 Binary Search Trees
Advertisements

AVL Trees1 Part-F2 AVL Trees v z. AVL Trees2 AVL Tree Definition (§ 9.2) AVL trees are balanced. An AVL Tree is a binary search tree such that.
AVL Trees1 Part-F2 AVL Trees v z. AVL Trees2 AVL Tree Definition (§ 9.2) AVL trees are balanced. An AVL Tree is a binary search tree such that.
S. Sudarshan Based partly on material from Fawzi Emad & Chau-Wen Tseng
AVL Tree Smt Genap Outline AVL Tree ◦ Definition ◦ Properties ◦ Operations Smt Genap
Time Complexity of Basic BST Operations Search, Insert, Delete – These operations visit the nodes along a root-to- leaf path – The number of nodes encountered.
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.
CS 171: Introduction to Computer Science II
BST Data Structure A BST node contains: A BST contains
Lec 15 April 9 Topics: l binary Trees l expression trees Binary Search Trees (Chapter 5 of text)
Chapter 4: Trees Binary Search Trees
Fundamentals of Python: From First Programs Through Data Structures
Data Structures Using C++ 2E Chapter 11 Binary Trees and B-Trees.
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.
Data Structures Using C++1 Chapter 11 Binary Trees.
CSE373: Data Structures & Algorithms Lecture 6: Binary Search Trees Lauren Milne Summer
Chapter 19 - basic definitions - order statistics ( findkth( ) ) - balanced binary search trees - Java implementations Binary Search Trees 1CSCI 3333 Data.
Chapter 19: Binary Trees. Objectives In this chapter, you will: – Learn about binary trees – Explore various binary tree traversal algorithms – Organize.
CS Data Structures Chapter 15 Trees Mehmet H Gunes
Lecture 10 Trees –Definiton of trees –Uses of trees –Operations on a tree.
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.
Tree Data Structures.
1 Balanced Trees There are several ways to define balance Examples: –Force the subtrees of each node to have almost equal heights –Place upper and lower.
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
Search Trees. Binary Search Tree (§10.1) A binary search tree is a binary tree storing keys (or key-element pairs) at its internal nodes and satisfying.
1 Searching Searching in a sorted linked list takes linear time in the worst and average case. Searching in a sorted array takes logarithmic time in the.
CS 253: Algorithms Chapter 13 Balanced Binary Search Trees (Balanced BST) AVL Trees.
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.
Tree Traversals, TreeSort 20 February Expression Tree Leaves are operands Interior nodes are operators A binary tree to represent (A - B) + C.
Chapter 4: Trees Part I: General Tree Concepts Mark Allen Weiss: Data Structures and Algorithm Analysis in Java.
CSS446 Spring 2014 Nan Wang.  to study trees and binary trees  to understand how binary search trees can implement sets  to learn how red-black trees.
Binary Tree. Some Terminologies Short review on binary tree Tree traversals Binary Search Tree (BST)‏ Questions.
Binary Search Trees (BST)
Rooted Tree a b d ef i j g h c k root parent node (self) child descendent leaf (no children) e, i, k, g, h are leaves internal node (not a leaf) sibling.
CIS 068 Welcome to CIS 068 ! Lesson 12: Data Structures 3 Trees.
1. Iterative Preorder Traversal Rpreorder(T) 1. [process the root node] if T!= NULL then Write Data(T) else Write “empty Tree” 2. [process the left subtree]
AVL Trees 1. Balancing a BST Goal – Keep the height small – For any node, left and right sub-tree have approximately the same height Ensures fast (O(lgn))
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.
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.
Foundation of Computing Systems Lecture 4 Trees: Part I.
CSE373: Data Structures & Algorithms Lecture 6: Binary Search Trees Linda Shapiro Winter 2015.
Question 4 Tutorial 8. Part A Insert 20, 10, 15, 5,7, 30, 25, 18, 37, 12 and 40 in sequence into an empty binary tree
AVL Trees AVL (Adel`son-Vel`skii and Landis) tree = – A BST – With the property: For every node, the heights of the left and right subtrees differ at most.
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.
1 Trees. 2 Trees Trees. Binary Trees Tree Traversal.
COSC 2007 Data Structures II Chapter 13 Advanced Implementation of Tables II.
Trees Chapter 15.
Search Trees.
Lecture 15 AVL Trees Slides modified from © 2010 Goodrich, Tamassia & by Prof. Naveen Garg’s Lectures.
Binary Search Tree (BST)
Introduction Applications Balance Factor Rotations Deletion Example
Binary Search Tree Chapter 10.
Tree.
Red-Black Trees 9/12/ :44 AM AVL Trees v z AVL Trees.
AVL Tree 27th Mar 2007.
Chapter 22 : Binary Trees, AVL Trees, and Priority Queues
Red-Black Trees 11/13/2018 2:07 AM AVL Trees v z AVL Trees.
AVL Trees CENG 213 Data Structures.
Red-Black Trees 11/26/2018 3:42 PM AVL Trees v z AVL Trees.
Red-Black Trees 2/24/ :17 AM AVL Trees v z AVL Trees.
Chapter 20: Binary Trees.
Red-Black Trees 5/19/2019 6:39 AM AVL Trees v z AVL Trees.
1 Lecture 13 CS2013.
Non-Linear data structures
Data Structures Using C++ 2E
Chapter 11 Trees © 2011 Pearson Addison-Wesley. All rights reserved.
Presentation transcript:

1 Binary Trees Informal defn: each node has 0, 1, or 2 children Informal defn: each node has 0, 1, or 2 children Formal defn: a binary tree is a structure that Formal defn: a binary tree is a structure that  contains no nodes, or  is comprised of three disjoint sets of nodes:  a root  a binary tree called its left subtree  a binary tree called its right subtree A binary tree that contains no nodes is called empty A binary tree that contains no nodes is called empty Note: the position of a child matters! Note: the position of a child matters!

2 Binary Trees Full binary tree : Full binary tree :  All internal nodes have two children. Complete binary tree : Complete binary tree :  All leaves have the same depth  All internal nodes have two children  A complete binary tree of height h has 2 h -1 internal nodes and 2 h leaves  Also: a binary tree with n nodes has height at least  lgn 

3 Tree applications Expression evaluations (note how different traversals result in different notation) Expression evaluations (note how different traversals result in different notation) Parsing (as part of the compilation process) Parsing (as part of the compilation process) Storing and retrieving information by a key Storing and retrieving information by a key Representing structured objects (e.g. the universe in an adventure game) Representing structured objects (e.g. the universe in an adventure game) Useful when needing to make a decision on how to proceed (tic-tac-toe, chess) Useful when needing to make a decision on how to proceed (tic-tac-toe, chess)

4 Binary tree traversal Traversing a tree = visiting its nodes Traversing a tree = visiting its nodes There are three ways to traverse a binary tree There are three ways to traverse a binary tree  preorder : visit root, visit left subtree, visit right subtree  inorder : visit left subtree, visit root, visit right subtree  postorder : visit left subtree, visit right subtree, visit root

5 Example: Inorder traversal template Tree ::Inorder(TreeNode *subroot ) { if (subroot!=NULL) { Inorder(subroot  left); cout << subroot  element; Inorder(subroot  right); }

6 Binary trees Use for storing and retrieving information Use for storing and retrieving information We want to insert, delete and search at least as fast and, if possible, faster than with a linked list We want to insert, delete and search at least as fast and, if possible, faster than with a linked list We want to take advantage of the lgn height We want to take advantage of the lgn height Idea: Store information in an ordered way (use a key) Idea: Store information in an ordered way (use a key) Result: a Binary Search Tree (BST) Result: a Binary Search Tree (BST)

7 Binary Search Tree (BST) A BST is a binary tree with the following property: A BST is a binary tree with the following property:  The key of the root is larger than any key in the left subtree and smaller than any key in the right subtree (the subtrees are also BSTs) Note: This definition does not allow duplicate keys. Note: This definition does not allow duplicate keys. It’s now easy to search for an element It’s now easy to search for an element

8 Binary Search Tree (BST) How do we insert an element into a BST? How do we insert an element into a BST? We have to make sure it is inserted at the correct position. We have to make sure it is inserted at the correct position. It is a combination of Search and Insert. It is a combination of Search and Insert.

9 Binary Search Tree (BST) How do we remove an element from a BST? How do we remove an element from a BST? Removing a leaf is easy Removing a leaf is easy Removing an internal node can be tricky. Removing an internal node can be tricky. The tree will need to be rearranged (how?) The tree will need to be rearranged (how?)

10 Balanced trees The good news: Why not sort a sequence by inserting the elements into a BST? The good news: Why not sort a sequence by inserting the elements into a BST?  on average O(nlgn) comparisons  we get to keep the tree The bad news: The insertion procedure can result in a tree of height n after inserting n elements. The bad news: The insertion procedure can result in a tree of height n after inserting n elements. We would prefer to get trees that are guaranteed to have logarithmic height in the worst case. We would prefer to get trees that are guaranteed to have logarithmic height in the worst case. Such trees are called balanced. Such trees are called balanced.

11 AVL trees AVL tree = a binary search tree with the following property: for every node the heights of the left and right subtrees differ at most by one. AVL tree = a binary search tree with the following property: for every node the heights of the left and right subtrees differ at most by one. That’s all very nice but how do we guarantee it? That’s all very nice but how do we guarantee it? We have to somehow modify the insert and delete functions. We have to somehow modify the insert and delete functions. If, after an insertion or deletion, the property is not satisfied, we “rotate” the tree to make it balanced. If, after an insertion or deletion, the property is not satisfied, we “rotate” the tree to make it balanced.

12 AVL trees When can an insertion of a child y at node x cause an imbalance? When can an insertion of a child y at node x cause an imbalance?  when both x and y are left children  when both x and y are right children  when x is a right child and y is a left child  when y is a right child and x is a left child

13 AVL trees There are two types of rotations: There are two types of rotations:  single rotation  fixes imbalance of type 1/2  double rotation  fixes imbalance of type 3/4 Let’s draw some trees... Let’s draw some trees...

14 AVL trees How/when do we decide whether to rotate? How/when do we decide whether to rotate? Example: insertion Example: insertion  step 1: walk down the tree to insert the node in the correct position  step 2: walk up the tree checking the property at each node we need a helper function to determine the heights of the subtrees of each node. we need a helper function to determine the heights of the subtrees of each node. we need to be able to determine whether to perform a single or a double rotation we need to be able to determine whether to perform a single or a double rotation

15 AVL trees Just how balanced are AVL trees? Just how balanced are AVL trees? It can be shown that the worst case height of an AVL tree is at most 44% more than the minimum possible for BST (i.e. approximately 1.44 lgn) It can be shown that the worst case height of an AVL tree is at most 44% more than the minimum possible for BST (i.e. approximately 1.44 lgn) What does this mean? What does this mean?  Searching/Inserting/Removing now take O(lgn) in the worst case.