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

Similar documents
CSI33 Data Structures

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

CSI33 Data Structures

March 20/2003 Jayakanth Srinivasan,

Binary Trees, Binary Search Trees

TREES. Trees - Introduction

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

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

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

Chapter 20: Binary Trees

Associate Professor Dr. Raed Ibraheem Hamed

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

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

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

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

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

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

Advanced Java Concepts Unit 5: Trees. Notes and Exercises

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

CE 221 Data Structures and Algorithms

Binary Trees

Programming II (CS300)

Introduction to Computers and Programming. Concept Question

Trees. Manolis Koubarakis. Data Structures and Programming Techniques

CSC148 Week 6. Larry Zhang

Binary Trees and Binary Search Trees

Binary Trees. Examples:

MULTIMEDIA COLLEGE JALAN GURNEY KIRI KUALA LUMPUR

Stacks, Queues and Hierarchical Collections

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

Bioinformatics Programming. EE, NCKU Tien-Hao Chang (Darby Chang)

Tree. A path is a connected sequence of edges. A tree topology is acyclic there is no loop.

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

Analysis of Algorithms

Cpt S 122 Data Structures. Data Structures Trees

Data Structures and Algorithms for Engineers

CMPSCI 187: Programming With Data Structures. Lecture #26: Binary Search Trees David Mix Barrington 9 November 2012

Trees. Trees. CSE 2011 Winter 2007

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

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

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

F453 Module 7: Programming Techniques. 7.2: Methods for defining syntax

Advanced Java Concepts Unit 5: Trees. Notes and Exercises

Trees. Truong Tuan Anh CSE-HCMUT

Stacks, Queues and Hierarchical Collections. 2501ICT Logan

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

Visit ::: Original Website For Placement Papers. ::: Data Structure

Chapter 11.!!!!Trees! 2011 Pearson Addison-Wesley. All rights reserved 11 A-1

Chapter 11.!!!!Trees! 2011 Pearson Addison-Wesley. All rights reserved 11 A-1

Trees : Part 1. Section 4.1. Theory and Terminology. A Tree? A Tree? Theory and Terminology. Theory and Terminology

Trees, Binary Trees, and Binary Search Trees

Recursive Data Structures and Grammars

Tree. Virendra Singh Indian Institute of Science Bangalore Lecture 11. Courtesy: Prof. Sartaj Sahni. Sep 3,2010

MID TERM MEGA FILE SOLVED BY VU HELPER Which one of the following statement is NOT correct.

CS61B Lecture #20: Trees. Last modified: Mon Oct 8 21:21: CS61B: Lecture #20 1

Section Summary. Introduction to Trees Rooted Trees Trees as Models Properties of Trees

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

Data Structure Lecture#10: Binary Trees (Chapter 5) U Kang Seoul National University

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.

Tree Applications. Processing sentences (computer programs or natural languages) Searchable data structures

Operations on Heap Tree The major operations required to be performed on a heap tree are Insertion, Deletion, and Merging.

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

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

Data Structures Question Bank Multiple Choice

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

Partha Sarathi Mandal

Also, recursive methods are usually declared private, and require a public non-recursive method to initiate them.


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

Upcoming ACM Events Linux Crash Course Date: Time: Location: Weekly Crack the Coding Interview Date:

Data Structure. IBPS SO (IT- Officer) Exam 2017

Garbage Collection: recycling unused memory

Why Do We Need Trees?

Introduction to Binary Trees

DS ata Structures Aptitude

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

Binary Search Trees. See Section 11.1 of the text.

CS301 - Data Structures Glossary By

Trees. A tree is a directed graph with the property

Trees: examples (Family trees)

Largest Online Community of VU Students

Recursion, Binary Trees, and Heaps February 18

Binary Trees. Height 1

3137 Data Structures and Algorithms in C++

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

CS302 - Data Structures using C++

Lecture Notes 16 - Trees CSS 501 Data Structures and Object-Oriented Programming Professor Clark F. Olson

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

Algorithms and Data Structures

Lecture 26. Introduction to Trees. Trees

12 Abstract Data Types

Revision Statement while return growth rate asymptotic notation complexity Compare algorithms Linear search Binary search Preconditions: sorted,

Terminology. The ADT Binary Tree. The ADT Binary Search Tree

R13. II B. Tech I Semester Supplementary Examinations, May/June DATA STRUCTURES (Com. to ECE, CSE, EIE, IT, ECC)

COSC 2007 Data Structures II Final Exam. Part 1: multiple choice (1 mark each, total 30 marks, circle the correct answer)

MLR Institute of Technology

DATA STRUCTURE : A MCQ QUESTION SET Code : RBMCQ0305

binary tree empty root subtrees Node Children Edge Parent Ancestor Descendant Path Depth Height Level Leaf Node Internal Node Subtree

Trees. Tree Structure Binary Tree Tree Traversals

Heap: A binary heap is a complete binary tree in which each, node other than root is smaller than its parent. Heap example: Fig 1. NPTEL IIT Guwahati

Transcription:

Data Structures Trees By Dr. Mohammad Ali H. Eljinini

Trees Are collections of items arranged in a tree like data structure (none linear). Items are stored inside units called nodes. However: We can use arrays and linked structures to represent trees, as we will see later. So, let us start with some trees anatomy

Trees Anatomy Root Level 0 R Internal Node Level 1 S T Edge (or Branch) Level 2 X U V W Level 3 Y Z Leaf

Trees Anatomy Every Tree starts at the root. S and T are children of R. S is the parent of X. The root in on Level 0. In a Tree, there is exactly one path from R to any other node. The descendants of S are X, Y, and Z. The ancestors of node Y are X, S, and R.

Binary Trees A Binary Tree: is a tree in which each node has at mos two children. Example: R S T X U W Y Z V

Complete Binary Trees A Complete Binary Tree: is a Binary Tree in which each level must be completed (full), before we start a new level. Note: Items are left on the last level are filled left to right. Example: R S T Full X V U W Full Y Z Last level: start filling left to right!

Sequential BT Representation (array-based) H D K B F J L A C 0 1 2 3 4 5 6 7 8 9 Lets call the above array, Array A: 1. The Root is A[1] 2. Left Child of A[i] is at A[2i] 3. Right Child of A[i] is at A[2i+1] 4. The Parent of A[i] is A[i/2] Array A 1 H 2 3 D K 4 5 6 7 B F J L Example: Node B at A[4]: Parent of B: A[4/2] = A[2] has D LC of B : A[2*4] = A[8] has A RC of B : A[2*4+1] = A[9] has C 8 9 A C A Binary Tree represented in Array A So, we can represent a BT in an Array, but we have to determine where each item should be located in the array as shown above.

Linked BT Representation (Linked-based) R H D K B F public class TreeNode{ itemtype item; TreeNode LC, RC; // Methods of get and set } // as done previously LC item TreeNode RC TreeNode is a class contains: item and two references (LC and RC)

Linked BT Representation (Linked-based) But, how do we create the first node (Root)? TreeNode R; // a reference to TreeNode Create the node and set it s address in R: R = new TreeNode( ); R Store character H in Root Node: R.setItem( H ); H Store null (for now) in LC and RC: R.setLC(null); R.setRC(null);

Linked BT Representation (Linked-based) Now, let s insert another node to the left: TreeNode p; // another TreeNode reference Create the node and set it s address in p: p = new TreeNode(); R Fill in the new node: p H p.setitem( D ); p.setlc(null); p.setrc(null); D Attach the new node: R.setLC(p);

Applications of Trees: Heaps A Heap is: - A complete BT - and, the value in any parent is greater then the value in its LC and in RC. - That s it! Examples: 7 9 10?? 8 6 4?? 8 7 5 10 4 6 4 7 2 9 1 5 Not Complete Complete, but not heap! Heap

Applications of Heaps: 1. Priority Queues: The highest value comes out first! Note: Highest value is located in the root of the heap (always)!! Problem: If we remove the value in the root, we must reconstruct the heap again! Solution: Heapify! How? 1.Delete last leaf, and place its value in Root. 2.Exchange value of root with the largest value of its two children. 3.Exchange values down the path until the last level.

Applications of Heaps: 2. HeapSort: A sorting algorithm using a heap! How it works? 1.Insert values to be sorted in a heap. 2.Root has highest value: move it into array. 3.Heapify. 4.Repeat step 2 and 3 until all values are moved into the array.

Traversing Binary Trees: Expression Trees: A Binary Tree used to represent algebraic expressions formed with binary operators. Example: (b 2-4ac)/2a (b^2-4*a*c)/(2*a) - / * Note: 1. All operands are in leafs. 2. All operations are in internal nodes. ^ b 2 * 4 a * 2 a c

Traversing Binary Trees: Three ways to Travers Expression Trees: 1. Preorder: (output expression in prefix notation) - Visit Root - Traverse Left subtree in preorder - Traverse right subtree in preorder. 2. Inorder: (output expression in infix notation) - Traverse Left subtree in inorder - Visit Root - Traverse right subtree in inorder. 3. Postorder: (output expression in postfix notation) - Traverse Left subtree in Postorder - Traverse right subtree in Postorder. - Visit Root

Traversing Binary Trees: Implementation of Preorder traversal: public void pretraverse(treenode t) { if(t!=null) { Console.Write(t.getItem()); pretraverse(t.getlc()); // recursive traversal pretraverse(t.getrc()); // and now RC. } } pretraverse is a recursive method that will traverse the Left Child (LC) and Right Child (RC) in recursive manner.

Traversing Binary Trees: None-Recursive Preorder traversal (using Stack): Void pretraverse(treenode t) { Stack s; TreeNode n; s.init(); s.push(t); while(!s.empty()) { n = s.pop(); if(n!=null) { Console.WriteLine( N.getItem() ); s.push(n.getrc()); s.push(n.getlc()); } } }

Binary Search Trees (BST): A BST is: A Binary Tree where: Values in Left subtree < values in Node N < Values in right subtee. Example: 3, 2, 5, 9, 7, 1, 4, 10. Searching in BST: if key > current item move to RC else move to LC 1 2 3 4 5 9 7 10

Binary Search Trees (BST): TreeSort Algorithm: 1. Insert elements into BST 2. Perform inorder traversal Example: O M T C E P U Algorithm: 1. BST 2. Inorder (LNR) C E M O P T U C E M O P T U

Exercise: Trees 1. Construct expression tree whose PostOrder Traversal is: 2^2a*b*-b2^+ab-/ 2. Give Preorder, InOrder, and Postorder of the following expression tree X S R U T W Y Z V 3. Write a program to construct an expression tree, give a character string specifying its preorder traversal. 4. Write a function takes root and key, then search for the key in BST. 5. Write a function to construct a BST.