Data Structures Chapter 10 Binary Trees Andreas Savva.

Slides:



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

Senem Kumova Metin Spring2009 BINARY TREES && TREE TRAVERSALS Chapter 10 in A Book on C.
SUNY Oneonta Data Structures and Algorithms Visualization Teaching Materials Generation Group Binary Search Tree A running demonstration of binary search.
1 Data Structures CSCI 132, Spring 2014 Lecture 37 Binary Search Trees II.
Comp 245 Data Structures Trees. Introduction to the Tree ADT A tree is a non-linear structure. A treenode can point to 0 to N other nodes. There is one.
1 Tree Traversal Section 9.3 Longin Jan Latecki Temple University Based on slides by Paul Tymann, Andrew Watkins, and J. van Helden.
CS 171: Introduction to Computer Science II
Trees, Binary Trees, and Binary Search Trees COMP171.
Kymberly Fergusson CSE1303 Part A Data Structures and Algorithms Summer Semester 2003 Lecture A12 – Binary Trees.
Tree Traversal. Traversal Algorithms preorder inorder postorder.
Kymberly Fergusson CSE1303 Part A Data Structures and Algorithms Summer Semester 2003 Lecture A12 – Binary Trees.
Binary Tree Properties & Representation. Minimum Number Of Nodes Minimum number of nodes in a binary tree whose height is h. At least one node at each.
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.
Department of Computer Eng. & IT Amirkabir University of Technology (Tehran Polytechnic) Data Structures Lecturer: Abbas Sarraf Trees.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Sixth Edition Chapter 19: Binary Trees.
Binary Search Trees Chapter 6.
Chapter Chapter Summary Introduction to Trees Applications of Trees (not currently included in overheads) Tree Traversal Spanning Trees Minimum.
By : Budi Arifitama Pertemuan ke Objectives Upon completion you will be able to: Create and implement binary search trees Understand the operation.
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.
Kruse/Ryba ch101 Object Oriented Data Structures Binary Trees Binary Search Trees Height Balance:AVL Trees Splay Trees.
UNCA CSCI September, 2001 These notes were prepared by the text’s author Clifford A. Shaffer Department of Computer Science Virginia Tech Copyright.
CS Data Structures Chapter 5 Trees. Chapter 5 Trees: Outline  Introduction  Representation Of Trees  Binary Trees  Binary Tree Traversals 
Searching: Binary Trees and Hash Tables CHAPTER 12 6/4/15 Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education,
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.
Binary Trees Chapter 10. Introduction Previous chapter considered linked lists –nodes connected by two or more links We seek to organize data in a linked.
A Review of Binary Search Trees Dr. Gang Qian Department of Computer Science University of Central Oklahoma.
DATA STRUCTURES AND ALGORITHMS Lecture Notes 5 Prepared by İnanç TAHRALI.
Binary Trees 2 Overview Trees. Terminology. Traversal of Binary Trees. Expression Trees. Binary Search Trees.
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.
Tree 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.
Binary Trees Definition A binary tree is: (i) empty, or (ii) a node whose left and right children are binary trees typedef struct Node Node; struct Node.
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.
Binary Tree. Contiguous versus Linked List Insertion in Contiguous list needs a lot of move. For big chunks of records it is time consuming. Linked List.
Trees, Binary Trees, and Binary Search Trees COMP171.
1 Data Structures CSCI 132, Spring 2014 Lecture 36 Binary Search Trees.
 Trees Data Structures Trees Data Structures  Trees Trees  Binary Search Trees Binary Search Trees  Binary Tree Implementation Binary Tree Implementation.
1 10. Binary Trees Read Sec A. Introduction: Searching a linked list. 1. Linear Search /* Linear search a list for a particular item */ 1. Set.
Binary trees Binary search trees Expression trees Heaps Data Structures and Algorithms in Java, Third EditionCh06 – 1.
1 Data Structures CSCI 132, Spring 2014 Lecture 20 Linked Lists.
Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy Walters, and Godfrey Muganda Modified for use at Midwestern State University Chapter.
Topics Definition and Application of Binary Trees Binary Search Tree Operations.
Binary Trees Chapter 10. Introduction Previous chapter considered linked lists –nodes connected by two or more links We seek to organize data in a linked.
Binary Trees In computer science, a binary tree is a tree data structure in which each node has at most two children, which are referred to as the left.
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.
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 Tree. Tree  A nonlinear data structure consisting of nodes, each of which contains data and pointers to other nodes.  Each node has only.
CHAPTER 5 TREE CSEB324 DATA STRUCTURES & ALGORITHM.
Copyright © 2012 Pearson Education, Inc. Chapter 20: Binary Trees.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 20: Binary Trees.
Traversing Trees. Traversing is the systematic way of accessing, or ‘visiting’ all the nodes in a tree. Consider the three operations: V: Visit a node.
CS 240Chapter 10 – TreesPage Chapter 10 Trees The tree abstract data type provides a hierarchical to the representation of certain types of relationships.
Chapter 7 Trees_ Part2 TREES. Depth and Height 2  Let v be a node of a tree T. The depth of v is the number of ancestors of v, excluding v itself. 
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 20: Binary Trees.
ISOM MIS 215 Module 5 – Binary Trees. ISOM Where are we? 2 Intro to Java, Course Java lang. basics Arrays Introduction NewbieProgrammersDevelopersProfessionalsDesigners.
1 Trees. 2 Trees Trees. Binary Trees Tree Traversal.
 آشنايي با درخت  درخت هاي دودويي  پيمايش درختان  هرم  جنگل اهداف 1.
DS.T.1 Trees Chapter 4 Overview Tree Concepts Traversals Binary Trees Binary Search Trees AVL Trees Splay Trees B-Trees.
Chapter 05 Trees (Part II). Array Representation We can use an array to represent a complete binary tree. Lemma 5.4 ▫If a complete binary tree with.
Trees Chapter 15.
Paul Tymann and Andrew Watkins
Binary Search Tree (BST)
Chapter 20: Binary Trees.
Chapter 21: Binary Trees.
Data Structures CSCI 132, Spring 2014 Lecture 35 Binary Trees
Paul Tymann, Andrew Watkins,
Paul Tymann, Andrew Watkins,
Chapter 20: Binary Trees.
Presentation transcript:

Data Structures Chapter 10 Binary Trees Andreas Savva

2 Lists vs Trees Lists have great advantages of flexibility but the have one week feature: Lists have great advantages of flexibility but the have one week feature: They are sequential lists – can move through them only one position at a time. They are sequential lists – can move through them only one position at a time. Trees Trees Valuable for a range of applications, especially for problems of information retrieval. Valuable for a range of applications, especially for problems of information retrieval. The are not sequential. The are not sequential.

3 Binary Trees A binary tree is either empty or it consists of a node called a root together with two binary trees called the left subtree and the right subtree of the root. A binary tree is either empty or it consists of a node called a root together with two binary trees called the left subtree and the right subtree of the root. Jim DotRon Guy EvaJan Kay JonKim Tim RoyTom Amy Ann Amy Ann Dot Eva Guy Jan Jim Jon Kay Kim Ron Roy Tim Tom

4 Binary Trees root left right root 3 nodes 2 nodes Incorrect Binary Trees Examples:

5 Nodes Let x + y mean that the root has x nodes on the left and y nodes on the right, then Let x + y mean that the root has x nodes on the left and y nodes on the right, then

6 Traversal of Binary Trees Traversal means “moving through all the nodes visiting each one in turn”. Traversal means “moving through all the nodes visiting each one in turn”. It is one of the most important operations on binary trees. It is one of the most important operations on binary trees. There are many different orders in which all the nodes could be traversed. There are many different orders in which all the nodes could be traversed. V – visiting a node (root) L – traversing the left subtree (left) R – traversing the right subtree (right) VLR LVR LRV preorder inorder postorder preorder inorder postorder VRL RVL RLV mirror images

7 Examples 1 23 Preorder:1, 2, 3 Inorder:2, 1, 3 Postorder:2, 3, 1 Preorder:1, 2, 3, 4, 5 Inorder:1, 4, 3, 5, 2 Postorder:4, 5, 3, 2, Preorder:1, 5, 2, 4, 3 Inorder:5, 1, 4, 2, 3 Postorder:5, 4, 3, 2, 1

8 Examples (continue) Jim DotRon Guy EvaJan Kay JonKim Tim RoyTom Amy Ann Preorder:Jim, Dot, Amy, Ann, Guy, Eva, Jan, Ron, Kay, Jon, Kim, Tim, Roy, Tom Inorder:Amy, Ann, Dot, Eva, Guy, Jan, Jim, Jon, Kay, Kim, Ron, Roy, Tim, Tom Postorder:Ann, Amy, Eva, Jan, Guy, Dot, Jon, Kim, Kay, Roy, Tom, Tim, Ron, Jim

9 Expression Trees + ab log x ! n – b × c a or c < d a < b Preorder:+ a b Inorder:a + b Postorder:a b + Preorder:log x Inorder:log x Postorder:x log Preorder:! n Inorder:n ! Postorder:n ! Preorder:– a × b c Inorder:a – b × c Postorder:a b c × – Preorder:or < a b < c d Inorder:a < b or c < d Postorder:a b < c d < or

10 = + / X 2 × a – b –  0.5 b  2 × c 4 × a Preorder:= X/ + – b  – b 2 × × 4 a c 0.5 × 2 a Inorder: X= ( – b + ( b 2 – 4 × a × c) 0.5 ) / ( 2 × a) Postorder: Xb – b 2 4 a × c × – 0.5 + 2 a × / = Related to the Polish form Preorder – Prefix Inorder – Infix Postorder – Postfix

11 The Node Implementation template struct Node { // data members Entry entry; Node *left, *right; // constructors Node(); Node(const Entry &x); }; template Node ::Node() { left = right = NULL; } template Node ::Node(const Entry &x) { entry = x; left = right = NULL; }

12 Linked Implementation of Binary Trees Jim Dot AmyGuy AnnEvaJanKimRoyTomJon Ron KayTim

13 template class Tree { public: Tree(); void clear(); bool empty() const; int size() const; int height() const; void preorder(void (*visit)(Entry &)); void inorder(void (*visit)(Entry &)); void postorder(void (*visit)(Entry &)); Error_code insert(const Entry &x); Error_code remove(const Entry &x); // Safequards ~Tree(); Tree(const Tree &original); void operator = (const Tree &original); protected: int size_recursive(Node *sub_root) const; int height_recursive(Node *sub_root) const; void recursive_preorder(Node *sub_root, void(*visit)(Entry &)); void recursive_inorder(Node *sub_root, void(*visit)(Entry &)); void recursive_postorder(Node *sub_root, void(*visit)(Entry &)); Error_code insert_recursive(Node * &subroot, const Entry &x); Error_code remove_recursive(Node * &subroot, const Entry &x); Error_code remove_root(Node * &subroot); Node *root; }; The Tree Implementation

14 Create Tree template Tree ::Tree() { root = NULL; } Constructor: root

15 Empty template bool Tree ::empty() const { return root == NULL; }

16 Size template int Tree ::size_recursive(Node *sub_root) const { if (sub_root == NULL) return 0; else return 1 + size_recursive(sub_root->left) + size_recursive(sub_root->right); } template int Tree ::size() const { return size_recursive(root); }

17 Height template int Tree ::height_recursive(Node *sub_root) const { if (sub_root == NULL) return 0; else return 1 + max(height_recursive(sub_root->left), height_recursive(sub_root->right)); } template int Tree ::height() const { return height_recursive(root); }

18 Preorder template void Tree ::recursive_preorder(Node *sub_root, void(*visit)(Entry &)) { if (sub_root != NULL) { (*visit)(subroot->entry); recursive_preorder(sub_root->left, visit); recursive_preorder(sub_root->right, visit); } template void Tree ::preorder(void(*visit)(Entry &)) { recursive_preorder(root, visit); }

19 Traverse Preorder void print(int &x) { cout << x << endl; } the_tree.preorder(print); Output

20 Inorder template void Tree ::recursive_inorder(Node *sub_root, void(*visit)(Entry &)) { if (sub_root != NULL) { recursive_inorder(sub_root->left, visit); (*visit)(subroot->entry); recursive_inorder(sub_root->right, visit); } template void Tree ::inorder(void(*visit)(Entry &)) { recursive_inorder(root, visit); }

21 Postorder template void Tree ::recursive_postorder(Node *sub_root, void(*visit)(Entry &)) { if (sub_root != NULL) { recursive_postorder(sub_root->left, visit); recursive_postorder(sub_root->right, visit); (*visit)(subroot->entry); } template void Tree ::postorder(void(*visit)(Entry &)) { recursive_postorder(root, visit); }

22 template class Tree { public:..... void preorder(); void inorder(); void postorder();..... protected:..... void recursive_preorder(Node *sub_root); void recursive_inorder(Node *sub_root); void recursive_postorder(Node *sub_root);..... Node *root; }; The Class Tree

23 Display Preorder template void Tree ::recursive_preorder(Node *sub_root) { if (sub_root != NULL) { cout entry << ” ”; recursive_preorder(sub_root->left); recursive_preorder(sub_root->right); } template void Tree ::preorder() { recursive_preorder(root); }

24 Display Inorder template void Tree ::recursive_inorder(Node *sub_root) { if (sub_root != NULL) { recursive_inorder(sub_root->left); cout entry << ” ”; recursive_inorder(sub_root->right); } template void Tree ::inorder() { recursive_inorder(root); }

25 Display Postorder template void Tree ::recursive_postorder(Node *sub_root) { if (sub_root != NULL) { recursive_postorder(sub_root->left); recursive_postorder(sub_root->right); cout entry << ” ”; } template void Tree ::postorder() { recursive_postorder(root); }

26 Insert into a Binary Search Tree Insert: e, b, d, f, a, g, c e f g b a dc Exercises: Insert: b, d, f, a, g, c, e Insert: b, d, f, a, g, c, e Insert: d, c, a, f, g, b, e Insert: d, c, a, f, g, b, e Insert: 1, 2, 3, 4, 5, 6, 7, 8, 9 Insert: 1, 2, 3, 4, 5, 6, 7, 8, 9 Insert: 8, 12, 3, 5, 1, 15, 4, 10, 6, 13, 7, 20, 18, 2, 9, 14 Insert: 8, 12, 3, 5, 1, 15, 4, 10, 6, 13, 7, 20, 18, 2, 9, 14 Insert: 6, 3, 8, 1, 4, 7, 5, 9,

27 Insert template Error_code Tree ::insert_recursive(Node *&sub_root, const Entry &x) { if (sub_root == NULL) { Node *temp = new Node (x); if (temp == NULL) return overflow; sub_root = temp; return success; } else if (x entry) return insert_recursive(sub_root->left, x); else return insert_recursive(sub_root->right, x); } template Error_code Tree ::insert(const Entry &x) { return insert_recursive(root, x); }

28 Remove x Detete x Deletion of a leaf x Detete x One subtree empty Replace x by w Neither subtree empty x w w w w Delete original w

29 How can 1 be removed from the tree? Remove How can 2 be removed from the tree?

How can 3 be removed from the tree? 9 Remove How can 6 be removed from the tree?

31 Remove How can 9 be removed from the tree?

32 Remove template Error_code Tree ::remove_recursive(Node *&sub_root, const Entry &x) { if (sub_root == NULL) return not_found; if (sub_root->entry == x) return remove_root(sub_root); else if (x entry) return remove_recursive(sub_root->left, x); else return remove_recursive(sub_root->right, x); } template Error_code Tree ::remove(const Entry &x) { return remove_recursive(root, x); }

33 Remove Root template Error_code Tree ::remove_root(Node *&sub_root) { Node *to_delete = sub_root; // remember to delete at end if (sub_root->right==NULL) sub_root = sub_root->left; else if (sub_root->left == NULL) sub_root = sub_root->right; else { // neither subtree is empty. to_delete = sub_root->left; // Move left to find predecessor Node *parent = sub_root; // Parent of to_delete while (to_delete->right != NULL) { // to_delete is not the predecessor parent = to_delete; to_delete = to_delete->right; } sub_root->entry = to_delete->entry; // Move from to_delete to root if (parent == sub_root) sub_root->left = to_delete->left; else parent->right = to_delete->left; } delete to_delete; return success; }