Data Structures Using C++1 Chapter 11 Binary Trees.

Slides:



Advertisements
Similar presentations
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 20: Binary Trees.
Advertisements


Trees Types and Operations
S. Sudarshan Based partly on material from Fawzi Emad & Chau-Wen Tseng
Binary Trees, Binary Search Trees CMPS 2133 Spring 2008.
Binary Trees, Binary Search Trees COMP171 Fall 2006.
Trees, Binary Trees, and Binary Search Trees COMP171.
C++ Programming:. Program Design Including
Gordon College Prof. Brinton
Lec 15 April 9 Topics: l binary Trees l expression trees Binary Search Trees (Chapter 5 of text)
1 abstract containers hierarchical (1 to many) graph (many to many) first ith last sequence/linear (1 to 1) set.
Starting Out with C++: Early Objects 5/e © 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5 th Edition Chapter 19 Binary.
Data Structures Using C++ 2E Chapter 11 Binary Trees and B-Trees.
C++ Programming: Program Design Including Data Structures, Third Edition Chapter 20: Binary Trees.
Binary Search Trees Chapter 7 Objectives
Data Structures Using C++1 Chapter 11 Binary Trees.
Advanced Data Structures and Algorithms COSC-600 Lecture presentation-6.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Templatized Tree.
Chapter 19: Binary Trees. Objectives In this chapter, you will: – Learn about binary trees – Explore various binary tree traversal algorithms – Organize.
1 TK1924 Program Design & Problem Solving Session 2011/2012 L8: Binary Trees.
© 2011 Pearson Addison-Wesley. All rights reserved 11 B-1 Chapter 11 (continued) Trees.
1 Trees A tree is a data structure used to represent different kinds of data and help solve a number of algorithmic problems Game trees (i.e., chess ),
Binary Trees, Binary Search Trees RIZWAN REHMAN CENTRE FOR COMPUTER STUDIES DIBRUGARH UNIVERSITY.
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.
Chapter 19: Binary Trees Java Programming: Program Design Including Data Structures Program Design Including Data Structures.
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.
Starting at Binary Trees
 Trees Data Structures Trees Data Structures  Trees Trees  Binary Search Trees Binary Search Trees  Binary Tree Implementation Binary Tree Implementation.
AVL Trees. AVL Node Structure The AVL node structure follows the same structure as the binary search tree, with the addition of a term to store the.
D. ChristozovCOS 221 Intro to CS II AVL Trees 1 AVL Trees: Balanced BST Binary Search Trees Performance Height Balanced Trees Rotation AVL: insert, delete.
Chapter 4: Trees Part I: General Tree Concepts Mark Allen Weiss: Data Structures and Algorithm Analysis in Java.
Data Structures Using Java1 Chapter 10 Binary 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 Search Trees (BST)
Copyright © 2012 Pearson Education, Inc. Chapter 20: Binary Trees.
Chapter 11 Binary Trees Dr. Youssef Harrath
Data Structures Using C++ 2E Chapter 11 Binary Trees.
AL-HUSEEN BIN TALAL UNIVERSITY College of Engineering Department of Computer Engineering Algorithms and Data Structures Binary Tree Course No.:
1 Trees General Trees  Nonrecursive definition: a tree consists of a set of nodes and a set of directed edges that connect pairs of nodes.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 20: Binary Trees.
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.
CMSC 202, Version 5/02 1 Trees. CMSC 202, Version 5/02 2 Tree Basics 1.A tree is a set of nodes. 2.A tree may be empty (i.e., contain no nodes). 3.If.
1 Trees General Trees  Nonrecursive definition: a tree consists of a set of nodes and a set of directed edges that connect pairs of nodes.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 20: Binary Trees.
Chapter 11. Chapter Summary  Introduction to trees (11.1)  Application of trees (11.2)  Tree traversal (11.3)  Spanning trees (11.4)
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.
Binary Search Trees Chapter 7 Objectives
Trees Chapter 15.
Data Structure and Algorithms
Trees Chapter 11 (continued)
Trees Chapter 11 (continued)
UNIT III TREES.
Binary Search Tree (BST)
AVL Trees A BST in which, for any node, the number of levels in its two subtrees differ by at most 1 The height of an empty tree is -1. If this relationship.
Tree.
Data Structures Using C++ 2E
Binary Trees, Binary Search Trees
Chapter 20: Binary Trees.
Chapter 22 : Binary Trees, AVL Trees, and Priority Queues
Data Structures Using Java
Chapter 21: Binary Trees.
AVL Trees CENG 213 Data Structures.
Binary Trees, Binary Search Trees
Trees.
Chapter 20: Binary Trees.
Trees.
Binary Trees, Binary Search Trees
Data Structures Using C++ 2E
Presentation transcript:

Data Structures Using C++1 Chapter 11 Binary Trees

Data Structures Using C++2 Chapter Objectives Learn about binary trees Explore various binary tree traversal algorithms Learn how to organize data in a binary search tree Discover how to insert and delete items in a binary search tree Explore nonrecursive binary tree traversal algorithms Learn about AVL (height-balanced) trees

Data Structures Using C++3 Binary Trees Definition: A binary tree, T, is either empty or such that: –T has a special node called the root node; –T has two sets of nodes, L T and R T, called the left subtree and right subtree of T, respectively; –L T and R T are binary trees

Data Structures Using C++4 Binary Tree

Data Structures Using C++5 Binary Tree With One Node The root node of the binary tree = A L A = empty R A = empty

Data Structures Using C++6 Binary Trees With Two Nodes

Data Structures Using C++7 Binary Trees With Two Nodes

Data Structures Using C++8 Various Binary Trees With Three Nodes

Data Structures Using C++9 Binary Trees Following struct defines the node of a binary tree: template struct nodeType { elemType info; nodeType *llink; nodeType *rlink; };

Data Structures Using C++10 Nodes For each node: –Data is stored in info –The pointer to the left child is stored in llink –The pointer to the right child is stored in rlink

Data Structures Using C++11 General Binary Tree

Data Structures Using C++12 Binary Tree Definitions Leaf: node that has no left and right children Parent: node with at least one child node Level of a node: number of branches on the path from root to node Height of a binary tree: number of edges on the longest path from root to a leaf

Data Structures Using C++13 Height of a Binary Tree Recursive algorithm to find height of binary tree: (height(p) denotes height of binary tree with root p): if(p is NULL) height(p) = 0 else height(p) = 1 + max(height(p->llink), height(p->rlink))

Data Structures Using C++14 Height of a Binary Tree Function to implement above algorithm: template int height(nodeType *p) { if(p == NULL) return 0; else return 1 + max(height(p->llink), height(p->rlink)); }

Data Structures Using C++15 Copy Tree Useful operation on binary trees is to make identical copy of binary tree Use function copyTree when we overload assignment operator and implement copy constructor On next slide, we copy from “otherTree” into “copiedTree”

Data Structures Using C++16 Copy Tree template void copyTree(nodeType * &copiedTreeRoot, nodeType * otherTreeRoot) { if(otherTreeRoot == NULL) copiedTreeRoot = NULL; else { copiedTreeRoot = new nodeType ; copiedTreeRoot->info = otherTreeRoot->info; copyTree(copiedTreeRoot->llink, otherTreeRoot->llink); copyTree(copiedTreeRoot->rlink, otherTreeRoot->rlink); } }//end copyTree

Data Structures Using C++17 Binary Tree Traversal Must start with the root, then –Visit the node first, or –Visit the subtrees first –“Visit” means to process the node (e.g. print its key value) Three different traversals –Inorder –Preorder –Postorder

Data Structures Using C++18 Traversals Inorder –Traverse the left subtree –Visit the node –Traverse the right subtree Preorder –Visit the node –Traverse the left subtree –Traverse the right subtree

Data Structures Using C++19 Traversals Postorder –Traverse the left subtree –Traverse the right subtree –Visit the node

Data Structures Using C++20 Example Traversals Inorder: B D A C Preorder: A B D C Postorder: D B C A

Data Structures Using C++21 Binary Tree: Inorder Traversal template void inorder(nodeType *p) { if(p != NULL) { inorder(p->llink); cout info << ” “; inorder(p->rlink); }

Data Structures Using C++22 Binary Tree: Traversals template void preorder(nodeType *p) { if(p != NULL) { cout info << ” “; preorder(p->llink); preorder(p->rlink); } template void postorder(nodeType *p) { if(p != NULL) { postorder(p->llink); postorder(p->rlink); cout info << ” “; } }1

Data Structures Using C++23 Binary Search Trees Data in each node –Larger than the data in its left child –Smaller than the data in its right child A binary search tree T is either empty or: –T has a special node called the root node –T has two sets of nodes, L T and R T, called the left subtree and right subtree of T, respectively –Key in root node larger than every key in L T and smaller than every key in R T –L T and R T are binary search trees

Data Structures Using C++24 Binary Search Trees

Data Structures Using C++25 Operations Performed on Binary Search Trees Determine whether the binary search tree is empty Search the binary search tree for a particular item Insert an item in the binary search tree Delete an item from the binary search tree

Data Structures Using C++26 Operations Performed on Binary Search Trees Find the height of the binary search tree Find the number of nodes in the binary search tree Find the number of leaves in the binary search tree Traverse the binary search tree Copy the binary search tree

Data Structures Using C++27 Binary Search Tree Analysis Worst Case: Linear tree

Data Structures Using C++28 Binary Search Tree Analysis Theorem: Let T be a binary search tree with n nodes, where n > 0.The average number of nodes visited in a search of T is approximately 1.39 log 2 n Number of comparisons required to determine whether x is in T is one more than the number of comparisons required to insert x in T Number of comparisons required to insert x in T same as the number of comparisons made in unsuccessful search, reflecting that x is not in T

Data Structures Using C++29 Binary Search Tree Analysis It follows that: It is also known that: Solving Equations (11-1) and (11-2)

Data Structures Using C++30 Nonrecursive Inorder Traversal

Data Structures Using C++31 Nonrecursive Inorder Traversal: General Algorithm 1.current = root; //start traversing the binary tree at // the root node 2.while(current is not NULL or stack is nonempty) if(current is not NULL) { push current onto stack; current = current->llink; } else { pop stack into current; visit current; //visit the node current = current->rlink; //move to the //right child }

Data Structures Using C++32 Nonrecursive Preorder Traversal: General Algorithm 1. current = root; //start the traversal at the root node 2. while(current is not NULL or stack is nonempty) if(current is not NULL) { visit current; push current onto stack; current = current->llink; } else { pop stack into current; current = current->rlink; //prepare to visit //the right subtree }

Data Structures Using C++33 Nonrecursive Postorder Traversal 1.current = root; //start traversal at root node 2.v = 0; 3.if(current is NULL) the binary tree is empty 4.if(current is not NULL) a.push current into stack; b.push 1 onto stack; c.current = current->llink; d.while(stack is not empty) if(current is not NULL and v is 0) { push current and 1 onto stack; current = current->llink; }

Data Structures Using C++34 Nonrecursive Postorder Traversal (Continued) else { pop stack into current and v; if(v == 1) { push current and 2 onto stack; current = current->rlink; v = 0; } else visit current; }

Data Structures Using C++35 AVL (Height-balanced Trees) A perfectly balanced binary tree is a binary tree such that: –The height of the left and right subtrees of the root are equal –The left and right subtrees of the root are perfectly balanced binary trees

Data Structures Using C++36 Perfectly Balanced Binary Tree

Data Structures Using C++37 AVL (Height-balanced Trees) An AVL tree (or height-balanced tree) is a binary search tree such that: –The height of the left and right subtrees of the root differ by at most 1 –The left and right subtrees of the root are AVL trees

Data Structures Using C++38 AVL Trees

Data Structures Using C++39 Non-AVL Trees

Data Structures Using C++40 Insertion Into AVL Tree

Data Structures Using C++41 Insertion Into AVL Trees

Data Structures Using C++42 Insertion Into AVL Trees

Data Structures Using C++43 Insertion Into AVL Trees

Data Structures Using C++44 Insertion Into AVL Trees

Data Structures Using C++45 AVL Tree Rotations Reconstruction procedure: rotating tree left rotation and right rotation Suppose that the rotation occurs at node x Left rotation: certain nodes from the right subtree of x move to its left subtree; the root of the right subtree of x becomes the new root of the reconstructed subtree Right rotation at x: certain nodes from the left subtree of x move to its right subtree; the root of the left subtree of x becomes the new root of the reconstructed subtree

Data Structures Using C++46 AVL Tree Rotations

Data Structures Using C++47 AVL Tree Rotations

Data Structures Using C++48 AVL Tree Rotations

Data Structures Using C++49 AVL Tree Rotations

Data Structures Using C++50 AVL Tree Rotations

Data Structures Using C++51 When do we use which rotations? If x is the node we need to adjust, and x and x’s deeper subtree have balance factors of same sign: –If both factors are positive (>), then make left rotation at x –If both negative (<), make right rotation at x If of opposite sign, do double rotation –x is < : rotate deepest subtree left, rotate x right –x is > : rotate deepest subtree right, rotate x left (What if x’s factor is = ?)

Data Structures Using C++52 The Insertion Algorithm 1.Insert node into available position, as with standard BST 2.Trace back the path from inserted node up towards root, adjusting balance factors and/or rotating tree as necessary  Rotations done via rules on previous slide Example, pp

Data Structures Using C++53 AVL Tree Rotations

Data Structures Using C++54 Deletion From AVL Trees Case 1: the node to be deleted is a leaf Case 2: the node to be deleted has one child Case 3: the node to be deleted has a left child and a right child Handle these the same as deleting from BST, but then trace path to root, adjusting balance factors/rotating as needed

Data Structures Using C++55 Analysis: AVL Trees Consider all the possible AVL trees of height h. Let T h be an AVL tree of height h such that T h has the fewest number of nodes. Let T hl denote the left subtree of T h and T hr denote the right subtree of T h. Then: where | T h | denotes the number of nodes in T h.

Data Structures Using C++56 Analysis: AVL Trees Suppose that T hl is of height h – 1 and T hr is of height h – 2. T hl is an AVL tree of height h – 1 such that T hl has the fewest number of nodes among all AVL trees of height h – 1. T hr is an AVL tree of height h – 2 that has the fewest number of nodes among all AVL trees of height h – 2. T hl is of the form T h -1 and T hr is of the form T h -2. Hence:

Data Structures Using C++57 Analysis: AVL Trees Let Fh+2 = |Th | + 1. Then: Called a Fibonacci sequence; solution to Fh is given by: Hence From this it can be concluded that

Data Structures Using C++58 Chapter Summary Binary trees Binary search trees Recursive traversal algorithms Nonrecursive traversal algorithms AVL trees