Associate Professor Dr. Raed Ibraheem Hamed

Similar documents
Binary Trees, Binary Search Trees

TREES. Trees - Introduction

7.1 Introduction. A (free) tree T is A simple graph such that for every pair of vertices v and w there is a unique path from v to w

Section 5.5. Left subtree The left subtree of a vertex V on a binary tree is the graph formed by the left child L of V, the descendents

CSCI-401 Examlet #5. Name: Class: Date: True/False Indicate whether the sentence or statement is true or false.

Algorithms in Systems Engineering ISE 172. Lecture 16. Dr. Ted Ralphs

Computational Optimization ISE 407. Lecture 16. Dr. Ted Ralphs

Chapter 20: Binary Trees

Trees. (Trees) Data Structures and Programming Spring / 28

Programming II (CS300)

Data Structures and Algorithms for Engineers

Binary Search Tree (2A) Young Won Lim 5/17/18

a graph is a data structure made up of nodes in graph theory the links are normally called edges

Chapter 4 Trees. Theorem A graph G has a spanning tree if and only if G is connected.

March 20/2003 Jayakanth Srinivasan,

Binary Trees

Binary Tree. Preview. Binary Tree. Binary Tree. Binary Search Tree 10/2/2017. Binary Tree

Trees. Truong Tuan Anh CSE-HCMUT

Trees, Binary Trees, and Binary Search Trees

Data Structures. Trees. By Dr. Mohammad Ali H. Eljinini. M.A. Eljinini, PhD

DATA STRUCTURES AND ALGORITHMS. Hierarchical data structures: AVL tree, Bayer tree, Heap

Partha Sarathi Mandal

Analysis of Algorithms

Lec 17 April 8. Topics: binary Trees expression trees. (Chapter 5 of text)

Binary Search Tree (3A) Young Won Lim 6/6/18

Abstract Data Structures IB Computer Science. Content developed by Dartford Grammar School Computer Science Department

Search Trees: BSTs and B-Trees

Trees! Ellen Walker! CPSC 201 Data Structures! Hiram College!

Binary Search Tree (3A) Young Won Lim 6/4/18

Binary Search Tree (3A) Young Won Lim 6/2/18

Binary Trees and Binary Search Trees

Successor/Predecessor Rules in Binary Trees

CS 171: Introduction to Computer Science II. Binary Search Trees

Advanced Tree Data Structures

Advanced Java Concepts Unit 5: Trees. Notes and Exercises

CSI33 Data Structures

CISC 235 Topic 3. General Trees, Binary Trees, Binary Search Trees

CSE2331/5331. Topic 6: Binary Search Tree. Data structure Operations CSE 2331/5331

Binary Trees. Examples:

INF2220: algorithms and data structures Series 1

Binary Search Trees. See Section 11.1 of the text.

CSE 230 Intermediate Programming in C and C++ Binary Tree

A set of nodes (or vertices) with a single starting point

CSI33 Data Structures

Friday, March 30. Last time we were talking about traversal of a rooted ordered tree, having defined preorder traversal. We will continue from there.

Data Structure - Binary Tree 1 -

BST Implementation. Data Structures. Lecture 4 Binary search trees (BST) Dr. Mahmoud Attia Sakr University of Ain Shams

CS24 Week 8 Lecture 1

Binary Trees. College of Computing & Information Technology King Abdulaziz University. CPCS-204 Data Structures I

Introduction to Computers and Programming. Concept Question

Trees. T.U. Cluj-Napoca -DSA Lecture 2 - M. Joldos 1

Trees. Trees. CSE 2011 Winter 2007

2-3 Tree. Outline B-TREE. catch(...){ printf( "Assignment::SolveProblem() AAAA!"); } ADD SLIDES ON DISJOINT SETS

(2,4) Trees. 2/22/2006 (2,4) Trees 1

Design and Analysis of Algorithms Lecture- 9: Binary Search Trees

Lecture 6: Analysis of Algorithms (CS )

Friday Four Square! 4:15PM, Outside Gates

First Semester - Question Bank Department of Computer Science Advanced Data Structures and Algorithms...

INF2220: algorithms and data structures Series 1

IX. Binary Trees (Chapter 10) Linear search can be used for lists stored in an array as well as for linked lists. (It's the method used in the find

6-TREE. Tree: Directed Tree: A directed tree is an acyclic digraph which has one node called the root node

CE 221 Data Structures and Algorithms

IX. Binary Trees (Chapter 10)

Tree: non-recursive definition. Trees, Binary Search Trees, and Heaps. Tree: recursive definition. Tree: example.

CS 206 Introduction to Computer Science II

Programming II (CS300)

CS350: Data Structures Binary Search Trees

There are many other applications like constructing the expression tree from the postorder expression. I leave you with an idea as how to do it.

Lecture 7. Binary Search Trees / AVL Trees

! Tree: set of nodes and directed edges. ! Parent: source node of directed edge. ! Child: terminal node of directed edge

(2,4) Trees Goodrich, Tamassia (2,4) Trees 1

Cpt S 122 Data Structures. Data Structures Trees

Programming II (CS300)

Binary Trees. BSTs. For example: Jargon: Data Structures & Algorithms. root node. level: internal node. edge.

Principles of Computer Science

Computer Science 210 Data Structures Siena College Fall Topic Notes: Trees

Module 4: Index Structures Lecture 13: Index structure. The Lecture Contains: Index structure. Binary search tree (BST) B-tree. B+-tree.

Garbage Collection: recycling unused memory

Trees. Q: Why study trees? A: Many advance ADTs are implemented using tree-based data structures.

CS 206 Introduction to Computer Science II

Data Structures and Algorithms

* Due 11:59pm on Sunday 10/4 for Monday lab and Tuesday 10/6 Wednesday Lab

Trees 11/15/16. Chapter 11. Terminology. Terminology. Terminology. Terminology. Terminology

CS 350 : Data Structures Binary Search Trees

Data Structure and Algorithm Homework #3

Data Structures And Algorithms

Using a Heap to Implement a Priority Queue

Lecture: Analysis of Algorithms (CS )

Binary Search Tree. Revised based on textbook author s notes.

Binary Search Trees > = 2014 Goodrich, Tamassia, Goldwasser. Binary Search Trees 1

Binary search trees. Binary search trees are data structures based on binary trees that support operations on dynamic sets.

Binary search trees 3. Binary search trees. Binary search trees 2. Reading: Cormen et al, Sections 12.1 to 12.3

Tree Travsersals and BST Iterators

CS350: Data Structures Red-Black Trees

STUDENT LESSON AB30 Binary Search Trees

DATABASE MANAGEMENT SYSTEMS

Binary search trees (BST) Binary search trees (BST)

Advanced Java Concepts Unit 5: Trees. Notes and Exercises

CSC148-Section:L0301

Binary Trees. Height 1

Transcription:

Associate Professor Dr. Raed Ibraheem Hamed University of Human Development, College of Science and Technology Computer Science Department 2015 2016 Department of Computer Science _ UHD 1

What this Lecture is about: Traversals Traversing Trees Types of traversals Search Trees (BST) How to search a binary tree? Some terminology of Binary Trees Department of Computer Science _ UHD 2

Binary Tree Traversal Methods It's unclear how we should print a tree. Top to bottom? Left to right? A tree traversal is a specific order in which to trace the nodes of a tree. There are 3 common tree traversals. 1. in-order: left, root, right 2. pre-order: root, left, right 3. post-order: left, right, root. Department of Computer Science _ UHD 3

Binary Tree Traversal Methods Types of traversals Pre-order Visit root, traverse left child, traverse right child In-order Traverse left child, visit root, traverse right child The in-order traversal is probably the easiest to see, because it sorts the values from smallest to largest. Post-Order Traverse left child, traverse right child, visit root It is also called a depth-first search. Department of Computer Science _ UHD 4

Traversing Trees Pre-order traversal would give: A, B, D, E, C Tree A B C D E pre-order: root, left, right Department of Computer Science _ UHD 5

Traversing Trees In-order traversal would give: D, B, E, A, C in-order: left, root, right Department of Computer Science _ UHD 6

Traversing Trees Post-order traversal would give: D, E, B, C, A post-order: left, right, root Department of Computer Science _ UHD 7

Traversing Trees Level-order Traversal would give: A, B, C, D, E Department of Computer Science _ UHD 8

Traversing Trees Preorder: Root, then Children + A * B / C D Postorder: Children, then Root A B C D / * + Inorder: Left child, Root, Right child A + B * C / D + A * B / C D Department of Computer Science _ UHD 9

Preorder, Postorder and Inorder Pseudo Code Department of Computer Science _ UHD 10

Tree Traversal Example Ex. Write the 3 traversals of the given tree. In-order: Chewbacca, Han, Lando, Leia, Luke, Obi, Vader, Yoda Pre-order: Luke, Han, Chewbacca, Leia, Lando, Vader, Obi, Yoda Post-order: Chewbacca, Lando, Leia, Han, Obi, Yoda, Vader, Luke Department of Computer Science _ UHD 11

Illustrations for Traversals Assume: visiting a node is printing its data Preorder: 15 8 2 6 3 7 11 10 12 14 20 27 22 30 Inorder: 2 3 6 7 8 10 11 12 14 15 20 22 27 30 2 8 11 15 20 27 Postorder: 3 7 6 2 10 14 12 11 8 22 30 27 20 15 6 3 7 10 12 14 22 30 Department of Computer Science _ UHD 12

Preorder Of Expression Tree / * + + - e f a b c d / * + a b - c d + e f Gives prefix form of expression! Department of Computer Science _ UHD 13

Inorder Of Expression Tree Gives infix form of expression Department of Computer Science _ UHD 14

Postorder Of Expression Tree a b + c d - * e f + / Gives postfix form of expression! Department of Computer Science _ UHD 15

Some terminology of Binary Trees The successor nodes of a node are called its children The predecessor node of a node is called its parent The "beginning" node is called the root (has no parent) A node without children is called a leaf Department of Computer Science _ UHD 16

Some terminology of Binary Trees What is the max #nodes at some level i? The max # nodes at level i is where i = 0,1,2,...,L-1 0 2 1 2 2 2 3 2 Department of Computer Science _ UHD 17

How to search a binary tree? (1) Start at the root (2) Search the tree level by level, until you find the element you are searching for or you reach a leaf. Department of Computer Science _ UHD 18

Binary Search Trees (BSTs) Binary Search Tree Property: The value stored at a node is greater than the value stored at its left child and less than the value stored at its right child Department of Computer Science _ UHD 19

Binary Search Trees (BSTs) Where is the smallest element? Ans: leftmost element Where is the largest element? Ans: rightmost element Department of Computer Science _ UHD 20

How to search a binary search tree? (1) Start at the root (2) Compare the value of the item you are searching for with the value stored at the root (3) If the values are equal, then item found; otherwise, if it is a leaf node, then not found Department of Computer Science _ UHD 21

How to search a binary search tree? (4) If it is less than the value stored at the root, then search the left subtree (5) If it is greater than the value stored at the root, then search the right subtree (6) Repeat steps 2-6 for the root of the subtree chosen in the previous step 4 or 5 Department of Computer Science _ UHD 22

Other Kinds of Binary Trees Full Binary Tree: A full binary tree is a binary tree where all the leaves are on the same level and every non-leaf has two children The first four full binary trees are: Department of Computer Science _ UHD 23

Examples of Non-Full Binary Trees These trees are NOT full binary trees: (do you know why?) Department of Computer Science _ UHD 24

Labeling of Full Binary Trees Label the nodes from 1 to n from the top to the bottom, left to right 1 1 2 3 1 2 3 4 5 6 7 4 1 2 3 5 6 7 8 9 10 11 12 13 14 15 25 Department of Computer Science _ UHD 25

Thank you??? Department of Computer Science _ UHD 26