Data Structures Using C++ 2E

Slides:



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


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.
Binary Trees, Binary Search Trees COMP171 Fall 2006.
CS 171: Introduction to Computer Science II
© 2006 Pearson Addison-Wesley. All rights reserved11 A-1 Chapter 11 Trees.
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.
Data Structures Using C++1 Chapter 11 Binary Trees.
Data Structures Using C++1 Chapter 11 Binary Trees.
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.
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.
Lecture 10 Trees –Definiton of trees –Uses of trees –Operations on a tree.
Binary Trees Chapter Definition And Application Of Binary Trees Binary tree: a nonlinear linked list in which each node may point to 0, 1, or two.
Spring 2010CS 2251 Trees Chapter 6. Spring 2010CS 2252 Chapter Objectives Learn to use a tree to represent a hierarchical organization of information.
Binary Trees, Binary Search Trees RIZWAN REHMAN CENTRE FOR COMPUTER STUDIES DIBRUGARH UNIVERSITY.
Chapter 19: Binary Trees Java Programming: Program Design Including Data Structures Program Design Including Data Structures.
Starting at Binary Trees
Binary Search Tree Traversal Methods. How are they different from Binary Trees?  In computer science, a binary tree is a tree data structure in which.
 Trees Data Structures Trees Data Structures  Trees Trees  Binary Search Trees Binary Search Trees  Binary Tree Implementation Binary Tree Implementation.
TREES. What is a tree ? An Abstract Data Type which emulates a tree structure with a set of linked nodes The nodes within a tree are organized in a hierarchical.
Tree Traversals, TreeSort 20 February Expression Tree Leaves are operands Interior nodes are operators A binary tree to represent (A - B) + C.
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.
M180: Data Structures & Algorithms in Java Trees & Binary Trees Arab Open University 1.
CMSC 341 Introduction to Trees. 2/21/20062 Tree ADT Tree definition –A tree is a set of nodes which may be empty –If not empty, then there is a distinguished.
Binary Tree. Some Terminologies Short review on binary tree Tree traversals Binary Search Tree (BST)‏ Questions.
1 Binary Trees and Binary Search Trees Based on Dale & Co: Object-Oriented Data Structures using C++ (graphics)
1 Chapter 4 Trees Basic concept How tree are used to implement the file system How tree can be used to evaluate arithmetic expressions How to use trees.
Binary Search Trees (BST)
Lecture 17: Trees and Networks I Discrete Mathematical Structures: Theory and Applications.
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.
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.
Foundation of Computing Systems Lecture 4 Trees: Part I.
Trees By JJ Shepherd. Introduction Last time we discussed searching and sorting in a more efficient way Divide and Conquer – Binary Search – Merge Sort.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 20: Binary Trees.
1 Trees. 2 Trees Trees. Binary Trees Tree Traversal.
Tree Representation and Terminology Binary Trees Binary Search Trees Pointer-Based Representation of a Binary Tree Array-Based Representation of a Binary.
TREES From root to leaf. Trees  A tree is a non-linear collection  The elements are in a hierarchical arrangement  The elements are not accessible.
Binary Search Trees Chapter 7 Objectives
Trees Chapter 15.
Data Structure and Algorithms
Binary Trees and Binary Search Trees
Recursive Objects (Part 4)
Data Structures Binary Trees 1.
Binary Search Tree (BST)
Tree.
Section 8.1 Trees.
Data Structures & Algorithm Design
Data Structures Using C++ 2E
Binary Trees, Binary Search Trees
Chapter 20: Binary Trees.
Data Structures Using Java
Chapter 21: Binary Trees.
Tree A tree is a data structure in which each node is comprised of some data as well as node pointers to child nodes
CSE 373, Copyright S. Tanimoto, 2002 Binary Trees -
Binary Trees, Binary Search Trees
Trees.
CSE 373, Copyright S. Tanimoto, 2001 Binary Trees -
CSC 143 Java Trees.
Chapter 20: Binary Trees.
Binary Trees, Binary Search Trees
Chapter 11 Trees © 2011 Pearson Addison-Wesley. All rights reserved.
Presentation transcript:

Data Structures Using C++ 2E Binary Trees and B-Trees 1

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, LT and RT, called the left subtree and right subtree of T, respectively LT and RT are binary trees Can be shown pictorially Parent, left child, right child Node represented as a circle Circle labeled by the node Data Structures Using C++ 2E

Binary Trees (cont’d.) Root node drawn at the top Left child of the root node (if any) Drawn below and to the left of the root node Right child of the root node (if any) Drawn below and to the right of the root node Directed edge (directed branch): arrow FIGURE 11-1 Binary tree Data Structures Using C++ 2E

Binary Trees (cont’d.) FIGURE 11-2 Binary tree with one, two, or three nodes FIGURE 11-3 Various binary trees with three nodes Data Structures Using C++ 2E

Binary Trees (cont’d.) Every node in a binary tree Has at most two children struct defining node of a binary tree For each node The data stored in info A pointer to the left child stored in llink A pointer to the right child stored in rlink Data Structures Using C++ 2E

Binary Trees (cont’d.) Pointer to root node is stored outside the binary tree In pointer variable called the root Of type binaryTreeNode FIGURE 11-4 Binary tree Data Structures Using C++ 2E

Binary Trees (cont’d.) Level of a node Height of a binary tree Number of branches on the path Height of a binary tree Number of nodes on the longest path from the root to a leaf See code on page 604 Data Structures Using C++ 2E

Binary Tree Traversal Must start with the root, and then Visit the node first or Visit the subtrees first Three different traversals Inorder Preorder Postorder Data Structures Using C++ 2E

Binary Tree Traversal (cont’d.) Inorder traversal Traverse the left subtree Visit the node Traverse the right subtree Preorder traversal Data Structures Using C++ 2E

Binary Tree Traversal (cont’d.) Postorder traversal Traverse the left subtree Traverse the right subtree Visit the node Each traversal algorithm: recursive Listing of nodes Inorder sequence Preorder sequence Postorder sequence Data Structures Using C++ 2E

Binary Tree Traversal (cont’d.) FIGURE 11-5 Binary tree for an inorder traversal Data Structures Using C++ 2E

Binary Tree Traversal (cont’d.) Functions to implement the preorder and postorder traversals Data Structures Using C++ 2E

Implementing Binary Trees Operations typically performed on a binary tree Determine if binary tree is empty Search binary tree for a particular item Insert an item in the binary tree Delete an item from the binary tree Find the height of the binary tree Find the number of nodes in the binary tree Find the number of leaves in the binary tree Traverse the binary tree Copy the binary tree Data Structures Using C++ 2E

Binary Search Trees Data in each node Larger than the data in its left child Smaller than the data in its right child FIGURE 11-6 Arbitrary binary tree FIGURE 11-7 Binary search tree Data Structures Using C++ 2E

Binary Search Trees (cont’d.) A binary search tree, T, is either empty or the following is true: T has a special node called the root node T has two sets of nodes, LT and RT , called the left subtree and right subtree of T, respectively The key in the root node is larger than every key in the left subtree and smaller than every key in the right subtree LT and RT are binary search trees Data Structures Using C++ 2E

Binary Search Trees (cont’d.) Operations performed on a binary search tree 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 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++ 2E

Binary Search Trees (cont’d.) Every binary search tree is a binary tree Height of a binary search tree Determined the same way as the height of a binary tree Operations to find number of nodes, number of leaves, to do inorder, preorder, postorder traversals of a binary search tree Same as those for a binary tree Can inherit functions Data Structures Using C++ 2E

Insertion First search the tree and find the place where the new item is to be inserted Can search using algorithm similar to search algorithm designed for binary search trees If the item is already in tree Search ends at a nonempty subtree Duplicates are not allowed If item is not in AVL tree Search ends at an empty subtree; insert the item there After inserting new item in the tree Resulting tree might not be an AVL tree Data Structures Using C++ 2E

Insertion (cont’d.) FIGURE 11-14 AVL tree before and after inserting 90 FIGURE 11-15 AVL tree before and after inserting 75 Data Structures Using C++ 2E

Insertion (cont’d.) FIGURE 11-16 AVL tree before and after inserting 95 FIGURE 11-17 AVL tree before and after inserting 88 Data Structures Using C++ 2E

Search Searches binary search tree for a given item If item found in the binary search tree: returns true Otherwise: returns false Search must start at root node More than one item in a node (usually) Must search array containing the data Two functions required Function search Function searchNode Searches a node sequentially Data Structures Using C++ 2E

Traversing a B-Tree B-tree traversal methods Inorder, preorder, postorder Data Structures Using C++ 2E