Cpt S 122 Data Structures. Data Structures Trees

Similar documents
Cpt S 122 Data Structures. Course Review Midterm Exam # 1

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

TREES. Trees - Introduction

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

Binary Trees, Binary Search Trees

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

C Data Structures Stacks. Stack. push Adds a new node to the top of the stack

Chapter 20: Binary Trees

Programming II (CS300)

Cpt S 122 Data Structures. Data Structures

Recursion, Trees and Tree Traversal. Module CE System Programming & Computer Control Systems

Cpt S 122 Data Structures. Course Review FINAL. Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University

Introduction. for large input, even access time may be prohibitive we need data structures that exhibit times closer to O(log N) binary search tree

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

Binary Trees

INF2220: algorithms and data structures Series 1

Why Do We Need Trees?

Algorithms. AVL Tree

9/29/2016. Chapter 4 Trees. Introduction. Terminology. Terminology. Terminology. Terminology

Data Structures and Algorithms for Engineers

Data Structures. Outline. Introduction Linked Lists Stacks Queues Trees Deitel & Associates, Inc. All rights reserved.

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

CSC148 Week 6. Larry Zhang

MULTIMEDIA COLLEGE JALAN GURNEY KIRI KUALA LUMPUR

Analysis of Algorithms

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

Binary Search Trees. See Section 11.1 of the text.

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

Trees. CSE 373 Data Structures

Outline. Preliminaries. Binary Trees Binary Search Trees. What is Tree? Implementation of Trees using C++ Tree traversals and applications

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

Data Structures Question Bank Multiple Choice

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

March 20/2003 Jayakanth Srinivasan,

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

Algorithms. Deleting from Red-Black Trees B-Trees

Tree Structures. A hierarchical data structure whose point of entry is the root node

Trees. Introduction & Terminology. February 05, 2018 Cinda Heeren / Geoffrey Tien 1

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

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

Why Use Binary Trees? Data Structures - Binary Trees 1. Trees (Contd.) Trees

Garbage Collection: recycling unused memory

CSE 373 APRIL 17 TH TREE BALANCE AND AVL

Multi-way Search Trees. (Multi-way Search Trees) Data Structures and Programming Spring / 25

Binary Trees. Height 1

Data Structures - Binary Trees 1

Trees. Eric McCreath

IX. Binary Trees (Chapter 10)

CS350: Data Structures Tree Traversal

Advanced Tree Data Structures

Lecture 26. Introduction to Trees. Trees

[ DATA STRUCTURES ] Fig. (1) : A Tree

Data Structure Advanced

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

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

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

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

Course Review for. Cpt S 223 Fall Cpt S 223. School of EECS, WSU

Binary Trees. Examples:

STUDENT LESSON AB30 Binary Search Trees

CE 221 Data Structures and Algorithms

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

Binary Tree. Binary tree terminology. Binary tree terminology Definition and Applications of Binary Trees

Name CPTR246 Spring '17 (100 total points) Exam 3

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

Binary Search Trees Treesort

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

Associate Professor Dr. Raed Ibraheem Hamed

INF2220: algorithms and data structures Series 1

Sorted Arrays. Operation Access Search Selection Predecessor Successor Output (print) Insert Delete Extract-Min

Trees. Tree Structure Binary Tree Tree Traversals

Trees. Truong Tuan Anh CSE-HCMUT

Friday Four Square! 4:15PM, Outside Gates

Announcements. Midterm exam 2, Thursday, May 18. Today s topic: Binary trees (Ch. 8) Next topic: Priority queues and heaps. Break around 11:45am

CSI33 Data Structures

Advanced Java Concepts Unit 5: Trees. Notes and Exercises

Tree Structures. Definitions: o A tree is a connected acyclic graph. o A disconnected acyclic graph is called a forest

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

CSE 530A. B+ Trees. Washington University Fall 2013

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

CS302 - Data Structures using C++

Binary Trees and Binary Search Trees

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

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

Hierarchical data structures. Announcements. Motivation for trees. Tree overview

CS350: Data Structures Red-Black Trees

Fall, 2015 Prof. Jungkeun Park

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

Algorithms and Data Structures

Trees, Binary Trees, and Binary Search Trees

CS 231 Data Structures and Algorithms Fall Recursion and Binary Trees Lecture 21 October 24, Prof. Zadia Codabux

Red-Black Trees. Based on materials by Dennis Frey, Yun Peng, Jian Chen, and Daniel Hood

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

CSE 373 Spring Midterm. Friday April 21st

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

Search Trees. COMPSCI 355 Fall 2016

Data Structures and Algorithms

Copyright 1998 by Addison-Wesley Publishing Company 147. Chapter 15. Stacks and Queues

B-Trees. CS321 Spring 2014 Steve Cutchin

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

Transcription:

Cpt S 122 Data Structures Data Structures Trees Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University

Motivation Trees are one of the most important and extensively used data structures in computer science File systems of several popular operating systems are implemented as trees For example My Documents/ My Pictures/ 0001.jpg 0002.jpg My Videos/ Nice.mpg My Music/ School Files/ CptS223/ FunStuffs/

Topics Binary Trees Binary Search Tree insertnode, deletenode inorder, preorder, postorder Applications Duplicate elimination, Searching etc

Trees Linked lists, stacks and queues are linear data structures. A tree is a nonlinear, two-dimensional data structure with special properties. Tree nodes contain two or more links.

Trees (Cont.) Binary trees trees whose nodes all contain two links (none, one, or both of which may be NULL). The root node is the first node in a tree. Each link in the root node refers to a child. The left child is the first node in the left subtree, and the right child is the first node in the right subtree. The children of a node are called siblings. A node with no children is called a leaf node. Computer scientists normally draw trees from the root node down exactly the opposite of trees in nature.

Trees (Cont.)

Trees (Cont.) A special binary tree is called a binary search tree. A binary search tree (with no duplicate node values) has following properties. the values in any left subtree are less than the value in its parent node. the values in any right subtree are greater than the value in its parent node. The shape of the binary search tree that corresponds to a set of data can vary, depending on the order in which the values are inserted into the tree.

Trees (Cont.) Operations or functions Inserting a node into the tree Deleting a node from the tree Searching a node in the tree Traversals Inorder Preorder Postorder

Trees self-referential structure

Trees Example

Function insertnode

Function insertnode

Function insertnode The function used to create a binary search tree is recursive. Function insertnode receives the address of the tree and an integer to be stored in the tree as arguments. A node can be inserted only as a leaf node in a binary search tree.

Function insertnode(cont.) The steps for inserting a node in a binary search tree are as follows: If *treeptr is NULL, create a new node. Call malloc, assign the allocated memory to *treeptr, Assign to (*treeptr)->data the integer to be stored, Assign to (*treeptr)->leftptr and (*treeptr)->rightptr the value NULL Return control to the caller (either main or a previous call to insertnode).

Function insertnode(cont.) If the value of *treeptr is not NULL and the value to be inserted is less than (*treeptr)->data, function insertnode is called with the address of (*treeptr)- >leftptr to insert the node in the left subtree of the node pointed to by treeptr. If the value to be inserted is greater than (*treeptr)->data, function insertnode is called with the address of (*treeptr)- >rightptr to insert the node in the right subtree of the node pointed to by treeptr. Otherwise, the recursive steps continue until a NULL pointer is found, then Step 1 is executed to insert the new node.

Tree Traversal Function The functions used to traverse the tree are recursive. Traversal functions are inorder, preorder and postorder. Each receive a tree (i.e., the pointer to the root node of the tree) and traverse the tree.

Traversals: Function inorder The steps for an inorder traversal are: left, root, right Traverse the left subtree inorder. Process the value in the node. Traverse the right subtree inorder. The value in a node is not processed until the values in its left subtree are processed. The inorder traversal of the tree is: 6 13 17 27 33 42 48

inorder Function inorder traversal is: left, root, right

Traversals: Function inorder The inorder traversal of a binary search tree prints the node values in ascending order. The process of creating a binary search tree actually sorts the data this process is called the binary tree sort.

Traversals: Function preorder The steps for a preorder traversal are: root, left, right Process the value in the node. Traverse the left subtree preorder. Traverse the right subtree preorder. The value in each node is processed as the node is visited. After the value in a given node is processed, the values in the left subtree are processed, then those in the right subtree are processed. The preorder traversal of the tree is: 27 13 6 17 42 33 48

preorder Function preorder traversal is: root, left, right

Traversals: Function postorder The steps for a postorder traversal are: left, right, root Traverse the left subtree postorder. Traverse the right subtree postorder. Process the value in the node. The value in each node is not printed until the values of its children are printed. The postorder traversal of the tree is: 6 17 13 33 48 42 27

postorder Function postorder traversal is: left, right, root

BST Applications: Duplicate Elimination The binary search tree facilitates duplicate elimination. An attempt to insert a duplicate value will be recognized a duplicate will follow the same go left or go right decisions on each comparison as the original value did. The duplicate will eventually be compared with a node in the tree containing the same value. The duplicate value may simply be discarded at this point.

Binary Tree Search Searching a binary tree for a value that matches a key value is fast. If the tree is tightly packed, each level contains about twice as many elements as the previous level. A binary search tree with n elements would have a maximum of log 2 n levels. a maximum of log 2 n comparisons would have to be made either to find a match or to determine that no match exists. Searching a (tightly packed) 1,000,000 element binary search tree requires no more than 20 comparisons 2 20 > 1,000,000.

Other Binary Tree Operations The level order traversal of a binary tree visits the nodes of the tree row-by-row starting at the root node level. On each level of the tree, the nodes are visited from left to right. The level order traversal is not a recursive algorithm.

Exercise Implement the level order binary tree traversal using a common data structure we have discussed in the class. Write the pseudo code of this algorithm.

Level Order Binary Tree Traversal Use the Queue data structure to control the output of the level order binary tree traversal. Algorithm Insert/enqueue the root node in the queue While there are nodes left in the queue, Get/dequeue the node in the queue Print the node s value If the pointer to the left child of the node is not null Insert/enqueue the left child node in the queue If the pointer to the right child of the node is not null Insert/enqueue the right child node in the queue

Other Common Tree Data Strictures Binary search trees (BSTs) Support O(log 2 N) operations Balanced trees AVL trees, Splay trees B-trees for accessing secondary storage

Conclusions Accessing elements in a linear linked list can be prohibitive especially for large amounts of input. Trees are simple data structures for which the running time of most operations is O(log N) on average. For example, if N = 1 million: Searching an element in a linear linked list requires at most O(N) comparisons (i.e. 1 million comparisons) Searching an element in a binary search tree (a kind of tree) requires O(log 2 N) comparisons ( 20 comparisons)