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

Slides:



Advertisements
Similar presentations
Chapter 12 Binary Search Trees
Advertisements

Binary Trees CSC 220. Your Observations (so far data structures) Array –Unordered Add, delete, search –Ordered Linked List –??
Comp 122, Spring 2004 Binary Search Trees. btrees - 2 Comp 122, Spring 2004 Binary Trees  Recursive definition 1.An empty tree is a binary tree 2.A node.
Senem Kumova Metin Spring2009 BINARY TREES && TREE TRAVERSALS Chapter 10 in A Book on C.
Binary Trees Chapter 6. Linked Lists Suck By now you realize that the title to this slide is true… By now you realize that the title to this slide is.
C How to Program, 6/e © by Pearson Education, Inc. All Rights Reserved.
Advanced Data Structures
Binary Trees, Binary Search Trees CMPS 2133 Spring 2008.
Binary Trees, Binary Search Trees COMP171 Fall 2006.
CS 104 Introduction to Computer Science and Graphics Problems Data Structure & Algorithms (4) Data Structures 11/18/2008 Yang Song.
BST Data Structure A BST node contains: A BST contains
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.
Lec 15 April 9 Topics: l binary Trees l expression trees Binary Search Trees (Chapter 5 of text)
Unit 11a 1 Unit 11: Data Structures & Complexity H We discuss in this unit Graphs and trees Binary search trees Hashing functions Recursive sorting: quicksort,
Chapter 12 C Data Structures Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 12 – Data Structures Outline 12.1Introduction.
Data Structures Arrays both single and multiple dimensions Stacks Queues Trees Linked Lists.
CSCE 3110 Data Structures & Algorithm Analysis Binary Search Trees Reading: Chap. 4 (4.3) Weiss.
Chapter 12 Data Structure Associate Prof. Yuh-Shyan Chen Dept. of Computer Science and Information Engineering National Chung-Cheng University.
Review Binary Tree Binary Tree Representation Array Representation Link List Representation Operations on Binary Trees Traversing Binary Trees Pre-Order.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Custom Templatized Data Structures.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Templatized Tree.
 2007 Pearson Education, Inc. All rights reserved C Data Structures.
Chapter 19: Binary Trees. Objectives In this chapter, you will: – Learn about binary trees – Explore various binary tree traversal algorithms – Organize.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 223 – Advanced Data Structures Course Review Midterm.
Lecture 10 Trees –Definiton of trees –Uses of trees –Operations on a tree.
© 2011 Pearson Addison-Wesley. All rights reserved 11 B-1 Chapter 11 (continued) Trees.
1 Trees A tree is a data structure used to represent different kinds of data and help solve a number of algorithmic problems Game trees (i.e., chess ),
Binary Trees, Binary Search Trees RIZWAN REHMAN CENTRE FOR COMPUTER STUDIES DIBRUGARH UNIVERSITY.
Programming Practice Introduction Tree Operations. Binary Search Tree. File Processing Create, read, write and update files. Sequential.
Chapter 19: Binary Trees Java Programming: Program Design Including Data Structures Program Design Including Data Structures.
Binary SearchTrees [CLRS] – Chap 12. What is a binary tree ? A binary tree is a linked data structure in which each node is an object that contains following.
Starting at Binary Trees
Preview  Graph  Tree Binary Tree Binary Search Tree Binary Search Tree Property Binary Search Tree functions  In-order walk  Pre-order walk  Post-order.
Programming Practice 3 - Dynamic Data Structure
Week 7 - Friday.  What did we talk about last time?  Trees in general  Binary search trees.
Computer Science 112 Fundamentals of Programming II Introduction to Trees.
© Copyright 2012 by Pearson Education, Inc. All Rights Reserved. 1 Chapter 19 Binary Search Trees.
C How to Program, 7/e © by Pearson Education, Inc. All Rights Reserved.
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.
Introduction to Trees IT12112 Lecture 05 Introduction Tree is one of the most important non-linear data structures in computing. It allows us to implement.
Java How to Program, 9/e © Copyright by Pearson Education, Inc. All Rights Reserved.
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 Search Trees (BSTs) 18 February Binary Search Tree (BST) An important special kind of binary tree is the BST Each node stores some information.
Binary Tree. Some Terminologies Short review on binary tree Tree traversals Binary Search Tree (BST)‏ Questions.
1/14/20161 BST Operations Data Structures Ananda Gunawardena.
1 Lecture 21: Binary Search Tree delete etc. operations Lecturer: Santokh Singh CompSci 105 SS 2005 Principles of Computer Science.
Lecture - 11 on Data Structures. Prepared by, Jesmin Akhter, Lecturer, IIT,JU Threaded Trees Binary trees have a lot of wasted space: the leaf nodes each.
Binary Search Trees (BST)
Rooted Tree a b d ef i j g h c k root parent node (self) child descendent leaf (no children) e, i, k, g, h are leaves internal node (not a leaf) sibling.
Binary Search Trees … From
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.
BINARY TREES Objectives Define trees as data structures Define the terms associated with trees Discuss tree traversal algorithms Discuss a binary.
Trees CSIT 402 Data Structures II 1. 2 Why Do We Need Trees? Lists, Stacks, and Queues are linear relationships Information often contains hierarchical.
(c) University of Washington20-1 CSC 143 Java Trees.
CSE 373 Data Structures Lecture 7
Chapter 12 – Data Structures
Recursive Objects (Part 4)
12 C Data Structures.
12 C Data Structures.
Chapter 22 Custom Generic Data Structures
Binary Search Tree (BST)
Binary Search Tree Chapter 10.
Binary Trees, Binary Search Trees
Binary Tree and General Tree
Binary Trees, Binary Search Trees
Chapter 20: Binary Trees.
Binary Trees, Binary Search Trees
Chapter 11 Trees © 2011 Pearson Addison-Wesley. All rights reserved.
NATURE VIEW OF A TREE leaves branches root. NATURE VIEW OF A TREE leaves branches root.
Presentation transcript:

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

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.)

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

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 ).

 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. Function insertNode (Cont.)

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:

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:

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:

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)