Binary Trees Lecture 36 Wed, Apr 21, 2004 9/21/2018 Binary Trees.

Slides:



Advertisements
Similar presentations
S. Sudarshan Based partly on material from Fawzi Emad & Chau-Wen Tseng
Advertisements

Binary Trees, Binary Search Trees CMPS 2133 Spring 2008.
Trees Chapter 8.
ITEC200 – Week08 Trees. 2 Chapter Objectives Students can: Describe the Tree abstract data type and use tree terminology such as.
Fall 2007CS 2251 Trees Chapter 8. Fall 2007CS 2252 Chapter Objectives To learn how to use a tree to represent a hierarchical organization of information.
Trees, Binary Trees, and Binary Search Trees COMP171.
Trees Chapter 8. Chapter 8: Trees2 Chapter Objectives To learn how to use a tree to represent a hierarchical organization of information To learn how.
Data Structures Data Structures Topic #8. Today’s Agenda Continue Discussing Table Abstractions But, this time, let’s talk about them in terms of new.
© 2006 Pearson Addison-Wesley. All rights reserved11 A-1 Chapter 11 Trees.
© 2006 Pearson Addison-Wesley. All rights reserved11 A-1 Chapter 11 Trees.
A Binary Search Tree Implementation Chapter 25 Copyright ©2012 by Pearson Education, Inc. All rights reserved.
Marc Smith and Jim Ten Eyck
Binary Trees Chapter 6.
Data Structures Arrays both single and multiple dimensions Stacks Queues Trees Linked Lists.
Trees Chapter 8. 2 Tree Terminology A tree consists of a collection of elements or nodes, organized hierarchically. The node at the top of a tree is called.
CS Data Structures Chapter 15 Trees Mehmet H Gunes
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.
Trees Chapter 8. Chapter 8: Trees2 Chapter Objectives To learn how to use a tree to represent a hierarchical organization of information To learn how.
Spring 2010CS 2251 Trees Chapter 6. Spring 2010CS 2252 Chapter Objectives Learn to use a tree to represent a hierarchical organization of information.
INTRODUCTION TO BINARY TREES P SORTING  Review of Linear Search: –again, begin with first element and search through list until finding element,
Chapter 6 Binary Trees. 6.1 Trees, Binary Trees, and Binary Search Trees Linked lists usually are more flexible than arrays, but it is difficult to use.
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.
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.
Trees Chapter 8. 2 Tree Terminology A tree consists of a collection of elements or nodes, organized hierarchically. The node at the top of a tree is called.
Trees, Binary Trees, and Binary Search Trees COMP171.
Starting at Binary Trees
 Trees Data Structures Trees Data Structures  Trees Trees  Binary Search Trees Binary Search Trees  Binary Tree Implementation Binary Tree Implementation.
Tree Traversals, TreeSort 20 February Expression Tree Leaves are operands Interior nodes are operators A binary tree to represent (A - B) + C.
Binary Search Trees Lecture 5 1. Binary search tree sort 2.
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.
Binary Tree. Some Terminologies Short review on binary tree Tree traversals Binary Search Tree (BST)‏ Questions.
ADT Binary Search Tree Ellen Walker CPSC 201 Data Structures Hiram College.
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. 2 Trees Trees. Binary Trees Tree Traversal.
Binary Tree ADT: Properties
CSCE 210 Data Structures and Algorithms
AA Trees.
Binary Trees.
S. Sudarshan Based partly on material from Fawzi Emad & Chau-Wen Tseng
UNIT III TREES.
Week 6 - Wednesday CS221.
Binary Trees "The best time to plant a tree is twenty years ago. The second best time is now." -Chinese proverb Real programmmers always confuse Christmas.
Chapter 11 Trees © 2011 Pearson Addison-Wesley. All rights reserved.
Tree.
Lecture 22 Binary Search Trees Chapter 10 of textbook
Lecture 18. Basics and types of Trees
Binary Tree Applications
Binary Trees, Binary Search Trees
Map interface Empty() - return true if the map is empty; else return false Size() - return the number of elements in the map Find(key) - if there is an.
Binary Trees.
Trees and Binary Trees.
Trees.
Find in a linked list? first last 7  4  3  8 NULL
Trees.
Binary Trees.
Chapter 16 Tree Implementations
Trees CMSC 202, Version 5/02.
Binary Tree Traversals
CMSC 202 Trees.
2-3-4 Trees Red-Black Trees
Lecture 36 Section 12.2 Mon, Apr 23, 2007
Binary Trees, Binary Search Trees
Binary Trees.
Mark Redekopp David Kempe
Chapter 11 Trees © 2011 Pearson Addison-Wesley. All rights reserved.
Instructor: Dr. Michael Geiger Spring 2017 Lecture 30: Sorting & heaps
Binary Trees, Binary Search Trees
Introduction to Trees Chapter 6 Objectives
NATURE VIEW OF A TREE leaves branches root. NATURE VIEW OF A TREE leaves branches root.
Tree (new ADT) Terminology: A tree is a collection of elements (nodes)
Presentation transcript:

Binary Trees Lecture 36 Wed, Apr 21, 2004 9/21/2018 Binary Trees

Topics Binary trees Array implementation Linked implementation Binary tree nodes 9/21/2018 Binary Trees

Binary Trees A binary tree is a data structure with the following properties. It is either empty or it has a root node. Each node in the binary tree may have up to two children (called left and right). Each node, except the root node, has exactly one parent. The root node has no parent. 9/21/2018 Binary Trees

A Binary Tree 10 20 30 40 50 60 70 9/21/2018 Binary Trees

Terminology The tree metaphor The family metaphor tree, root, branch, leaf. The family metaphor parent, child, sibling, ancestor, descendant. 9/21/2018 Binary Trees

Tree Terminology 10 20 30 40 50 60 70 9/21/2018 Binary Trees

Tree Terminology Root 10 20 30 40 50 60 70 9/21/2018 Binary Trees

Tree Terminology 10 20 30 40 50 60 70 Parent 9/21/2018 Binary Trees

Tree Terminology 10 20 30 40 50 60 70 Leaves 9/21/2018 Binary Trees

Tree Terminology 10 20 30 40 50 60 70 Right Child 9/21/2018 Binary Trees

Tree Terminology Subtree 10 20 30 40 50 60 70 9/21/2018 Binary Trees

Array Implementation of a Binary Tree In an array binary tree, the nodes of the tree are stored in an array. Position 0 is left empty. The root is stored in position 1. For the element in position n, The left child is in position 2n. The right child is in position 2n + 1. The parent is in position n/2. 9/21/2018 Binary Trees

Array Implementation 10 20 30 40 50 60 70 1 2 3 4 5 6 7 8 9 9/21/2018 9/21/2018 Binary Trees

Array Implementation Unused 10 20 30 40 50 60 70 1 2 3 4 5 6 7 8 9 9/21/2018 Binary Trees

Array Implementation Root 10 20 30 40 50 60 70 1 2 3 4 5 6 7 8 9 9/21/2018 Binary Trees

Array Implementation Parent 10 20 30 40 50 60 70 1 2 3 4 5 6 7 8 9 9/21/2018 Binary Trees

Array Implementation Parents, do you know where your children are? 10 20 30 40 50 60 70 1 2 3 4 5 6 7 8 9 Parents, do you know where your children are? 9/21/2018 Binary Trees

Array Implementation Parents, do you know where your children are? 10 20 30 40 50 60 70 1 2 3 4 5 6 7 8 9 Parents, do you know where your children are? 9/21/2018 Binary Trees

Advantages of the Array Implementation This representation is very efficient when The tree is full, and The structure of the tree will not be modified. 9/21/2018 Binary Trees

Linked Implementation of a Binary Tree In a linked binary tree, each node contains pointers to its left and right children. A linked binary tree object has one data member: BinaryTreeNode* root - A pointer to the root node. 9/21/2018 Binary Trees

Binary Tree Nodes A binary tree node has three components. A value (data) that contains the tree element. A left pointer (left) that points to the root node of the left subtree. A right pointer (right) that points to the root node of the right subtree. 9/21/2018 Binary Trees

Binary Tree Nodes Nodes are allocated and deallocated dynamically. The binary tree always has allocated the exact number of nodes necessary to store the tree elements. Example binarytreenode.h 9/21/2018 Binary Trees

Binary Tree Implementation Lecture 37 Thu, Apr 22, 2004 9/21/2018 Binary Trees

Topics Binary tree interface Implementation of Search() Implementation of Draw() 9/21/2018 Binary Trees

Binary Tree Constructors BinaryTree(const T& value); BinaryTree(const BinaryTree& lft, const BinaryTree& rgt); BinaryTree(const T& value, const BinaryTree& lft, BinaryTree(const BinaryTree& tree); 9/21/2018 Binary Trees

Binary Tree Destructor 9/21/2018 Binary Trees

Binary Tree Inspectors int Size() const; int Height() const; bool Empty() const; T& Root() const; BinaryTree LeftSubtree() const; BinaryTree RightSubtree() const; bool IsCountBalanced() const; bool IsHeightBalanced() const; 9/21/2018 Binary Trees

Binary Tree Mutators void MakeEmpty(); void SetRoot(const T& value); 9/21/2018 Binary Trees

Binary Tree Facilitators void Input(istream& in); void Output(ostream& out); bool Equal(BinaryTree tree) const; 9/21/2018 Binary Trees

Binary Tree Operators BinaryTree& operator=(const BinaryTree&); istream& operator>>(istream&, BinaryTree&); ostream& operator<<(ostream&, const BinaryTree&); bool operator==(BinaryTree&); 9/21/2018 Binary Trees

Binary Tree Traversal Functions void PreorderTraversal(void (* Visit) (BinaryTreeNode*)) const; void InorderTraversal(void (* Visit) void PostorderTraversal(void (* Visit) void LevelorderTraversal(void (* Visit) 9/21/2018 Binary Trees

Other Binary Tree Functions T* Search(const T& value) const; void Draw() const; 9/21/2018 Binary Trees

Linked Binary Tree Implementation Example binarytree.h BinaryTreeTest.cpp 9/21/2018 Binary Trees

Binary Tree Traversals Lecture 38 Mon, Apr 26, 2004 9/21/2018 Binary Trees

Topics Binary tree traversals Binary tree iterators Pre-order traversals In-order traversals Post-order traversals Level-order traversals Binary tree iterators Pre-order iterators In-order iterators Post-order iterators Level-order iterators 9/21/2018 Binary Trees

Binary Tree Traversals Four Standard Traversals Pre-order Traversal. In-order Traversal. Post-order Traversal. Level-order Traversal. 9/21/2018 Binary Trees

Pre-order Traversal At each node, Also called an NLR traversal. First visit the node, Then perform a pre-order traversal of the left subtree, Then perform a pre-order traversal of the right subtree. Also called an NLR traversal. Node - Left - Right. 9/21/2018 Binary Trees

Pre-Order Traversal 10 20 30 40 50 60 70 9/21/2018 Binary Trees

Pre-Order Traversal 10 20 30 40 50 60 70 10 9/21/2018 Binary Trees

Pre-Order Traversal 10 20 30 40 50 60 70 10 20 9/21/2018 Binary Trees

Pre-Order Traversal 10 20 30 40 50 60 70 10 20 40 9/21/2018 Binary Trees

Pre-Order Traversal 10 20 30 40 50 60 70 10 20 40 50 9/21/2018 Binary Trees

Pre-Order Traversal 10 20 30 40 50 60 70 10 20 30 40 50 9/21/2018 Binary Trees

Pre-Order Traversal 10 20 30 40 50 60 70 10 20 30 40 50 60 9/21/2018 Binary Trees

Pre-Order Traversal 10 20 30 40 50 60 70 10 20 30 40 50 60 70 9/21/2018 Binary Trees

Pre-Order Traversal Order: 10, 20, 40, 50, 30, 60, 70 10 20 30 40 50 9/21/2018 Binary Trees

In-order Traversal At each node, Also called an LNR traversal. First, perform an in-order traversal of the left subtree, Then visit the node, Then perform an in-order traversal of the right subtree. Also called an LNR traversal. Left - Node - Right. 9/21/2018 Binary Trees

In-Order Traversal 10 20 30 40 50 60 70 9/21/2018 Binary Trees

In-Order Traversal 10 20 30 40 50 60 70 40 9/21/2018 Binary Trees

In-Order Traversal 10 20 30 40 50 60 70 20 40 9/21/2018 Binary Trees

In-Order Traversal 10 20 30 40 50 60 70 20 40 50 9/21/2018 Binary Trees

In-Order Traversal 10 20 30 40 50 60 70 10 20 40 50 9/21/2018 Binary Trees

In-Order Traversal 10 20 30 40 50 60 70 10 20 40 50 60 9/21/2018 Binary Trees

In-Order Traversal 10 20 30 40 50 60 70 10 20 30 40 50 60 9/21/2018 Binary Trees

In-Order Traversal 10 20 30 40 50 60 70 10 20 30 40 50 60 70 9/21/2018 Binary Trees

In-Order Traversal Order: 40, 20, 50, 10, 60, 30, 70 10 20 30 40 50 60 9/21/2018 Binary Trees

Post-order Traversal At each node, Also called an LRN traversal. First perform a post-order traversal of the left subtree, Then perform a post-order traversal of the right subtree, Then visit the node. Also called an LRN traversal. Left - Right - Node. 9/21/2018 Binary Trees

Post-Order Traversal 10 20 30 40 50 60 70 9/21/2018 Binary Trees

Post-Order Traversal 10 20 30 40 50 60 70 40 9/21/2018 Binary Trees

Post-Order Traversal 10 20 30 40 50 60 70 40 50 9/21/2018 Binary Trees

Post-Order Traversal 10 20 30 40 50 60 70 20 40 50 9/21/2018 Binary Trees

Post-Order Traversal 10 20 30 40 50 60 70 20 40 50 60 9/21/2018 Binary Trees

Post-Order Traversal 10 20 30 40 50 60 70 20 40 50 60 70 9/21/2018 Binary Trees

Post-Order Traversal 10 20 30 40 50 60 70 20 30 40 50 60 70 9/21/2018 Binary Trees

Post-Order Traversal 10 20 30 40 50 60 70 10 20 30 40 50 60 70 9/21/2018 Binary Trees

Post-Order Traversal Order: 40, 50, 20, 60, 70, 30, 10 10 20 30 40 50 9/21/2018 Binary Trees

Level-order Traversal Traverse the levels of the tree from top to bottom. Within each level, visit the nodes from left to right. 9/21/2018 Binary Trees

Level-Order Traversal 10 20 30 40 50 60 70 9/21/2018 Binary Trees

Level-Order Traversal 10 20 30 40 50 60 70 10 9/21/2018 Binary Trees

Level-Order Traversal 10 20 30 40 50 60 70 10 20 9/21/2018 Binary Trees

Level-Order Traversal 10 20 30 40 50 60 70 10 20 30 9/21/2018 Binary Trees

Level-Order Traversal 10 20 30 40 50 60 70 10 20 30 40 9/21/2018 Binary Trees

Level-Order Traversal 10 20 30 40 50 60 70 10 20 30 40 50 9/21/2018 Binary Trees

Level-Order Traversal 10 20 30 40 50 60 70 10 20 30 40 50 60 9/21/2018 Binary Trees

Level-Order Traversal 10 20 30 40 50 60 70 10 20 30 40 50 60 70 9/21/2018 Binary Trees

Level-Order Traversal 10 20 30 40 50 60 70 10 20 30 40 50 60 70 Order: 10, 20, 30, 40, 50, 60, 70 9/21/2018 Binary Trees

Binary Tree Iterators A binary tree iterator systematically visits each node of a binary tree. The four standard iterators correspond to the four standard traversal methods. Pre-order iterator In-order iterator Post-order iterator Level-order iterator 9/21/2018 Binary Trees

Pre-order Iterator Visit the nodes in a pre-order traversal. Use a stack to store unvisited nodes. Example binarytreeiterator.h binarytreepreorderiterator.h 9/21/2018 Binary Trees

Pre-order Iterator 10 20 30 40 50 60 70 10 10 10 20 20 20 30 30 30 40 50 60 70 9/21/2018 Binary Trees

In-order Iterator Visit the nodes in an in-order traversal. Use a stack to store unvisited nodes. Example binarytreeiterator.h binarytreeinorderiterator.h 9/21/2018 Binary Trees

In-order Iterator 10 20 30 40 50 60 70 10 10 20 20 30 30 40 50 60 70 9/21/2018 Binary Trees

Post-order Iterator Visit the nodes in a post-order traversal. Use a stack to store unvisited nodes. Example binarytreeiterator.h binarytreepostorderiterator.h 9/21/2018 Binary Trees

Post-order Iterator 10 20 30 40 50 60 70 10 10 20 20 30 30 40 50 60 70 9/21/2018 Binary Trees

Level-order Iterator Visit the nodes in a level-order traversal. Use a queue to store unvisited nodes. 9/21/2018 Binary Trees

Level-order Iterator 10 20 30 40 50 60 70 10 20 20 30 30 40 40 50 50 60 60 70 70 9/21/2018 Binary Trees

Traversals and Expression Trees Perform an in-order traversal of the expression tree and print the nodes. * + - 4 5 6 3 9/21/2018 Binary Trees

Traversals and Expression Trees Perform a post-order traversal to evaluate the expression tree. * + - 4 5 6 3 9/21/2018 Binary Trees

Binary Search Trees Lecture 39 9/21/2018 Binary Trees

Topics Binary search trees Inserting a node Deleting a node Balancing a binary search tree 9/21/2018 Binary Trees

Binary Search Trees A binary search tree is a binary tree with the following properties. There is a total order relation on the members in the tree. At every node, every member of the left subtree is less than or equal to the node value. At every node, every member of the right subtree is greater than or equal to the node value. 9/21/2018 Binary Trees

BinarySearchTree Implementation The BinarySearchTree class is implemented as a subclass of the BinaryTree class. 9/21/2018 Binary Trees

Binary Search Tree Interface Mutators void Insert(const T& value); void Delete(const T& value); Other member functions T* Search(const T& value) const; void CountBalance(); 9/21/2018 Binary Trees

Searching a BinarySearchTree Begin at the root node. Apply the following algorithm recursively. Compare the value to the node data. If it is equal, you are done. If it is less, search the left subtree. If it is greater, search the right subtree. If the subtree is empty, the value is not in the tree. 9/21/2018 Binary Trees

Inserting a Value into a BinarySearchTree Begin at the root node. Apply the following algorithm recursively. Compare the value to the node data. If it is less (or equal), continue with the left subtree. If is is greater, continue with the right subtree. When the subtree is empty, attach the node as a subtree. 9/21/2018 Binary Trees

Deleting a Value from a BinarySearchTree Perform a search to locate the value. This node has Two children. One child. No child. 9/21/2018 Binary Trees

Deleting a Value from a BinarySearchTree Case 1: The node has no child. Delete the node. Case 2: The node has one child. Replace the node with the subtree of which the child is the root. 9/21/2018 Binary Trees

Deleting a Value from a BinarySearchTree Case 3: The node has two children. Locate the next smaller value in the tree. This value is the rightmost value of the left subtree. Move left one step. Move right as far as possible. Swap this value with the value to be deleted. The node to be deleted now has at most one child. 9/21/2018 Binary Trees

Count-Balancing a BinarySearchTree Write a function MoveNodeRight() that will move the largest value of the left subtree to the right subtree. Locate the largest value in the left subtree. Delete it (but save the value). Place it at the root. Insert the old root value into the right subtree. 9/21/2018 Binary Trees

Count-Balancing a BinarySearchTree Write a similar function MoveNodeLeft(). Apply either MoveNodeRight() or MoveNodeLeft() repeatedly at the root node until the tree is balanced at the root. Then apply these functions recursively, down to the leaves. 9/21/2018 Binary Trees

BinarySearchTree Implementation Example binarysearchtree.h BinarySearchTreeTest.cpp 9/21/2018 Binary Trees