CSE 1002 Fundamentals of Software Development 2 More Linked Structures

Slides:



Advertisements
Similar presentations
Singly Linked List BTECH, EE KAZIRANGA UNIVERSITY.
Advertisements

S. Sudarshan Based partly on material from Fawzi Emad & Chau-Wen Tseng
Tree Data Structures &Binary Search Tree 1. Trees Data Structures Tree  Nodes  Each node can have 0 or more children  A node can have at most one parent.
Senem Kumova Metin Spring2009 BINARY TREES && TREE TRAVERSALS Chapter 10 in A Book on C.
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.
Computer Science C++ High School Level By Guillermo Moreno.
Recursion practice. Problem 0 Using recursion (and no arrays), write the code to read in a series of numbers (until EOF) and then print them backwards.
Binary Trees Terminology A graph G = is a collection of nodes and edges. An edge (v 1,v 2 ) is a pair of vertices that are directly connected. A path,
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.
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.
CS21, Tia Newhall Binary Search Trees (BST) 1.Hierarchical data structure with a single pointer to root node 2.Each node has at most two child nodes (a.
Properties: -Each node has a value -The left subtree contains only values less than the parent node’s value -The right subtree contains only values greater.
CSCE 3110 Data Structures & Algorithm Analysis Binary Search Trees Reading: Chap. 4 (4.3) Weiss.
Department of Computer Engineering Faculty of Engineering, Prince of Songkla University 1 8 – Trees.
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.
INTRODUCTION TO BINARY TREES P SORTING  Review of Linear Search: –again, begin with first element and search through list until finding element,
 Trees Data Structures Trees Data Structures  Trees Trees  Binary Search Trees Binary Search Trees  Binary Tree Implementation Binary Tree Implementation.
Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy Walters, and Godfrey Muganda Modified for use at Midwestern State University Chapter.
1 Nell Dale Chapter 8 Binary Search Trees Modified from the slides by Sylvia Sorkin, Community College of Baltimore County - Essex Campus C++ Plus Data.
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 CSE 2341 Object Oriented Programming with C++ Note Set #21.
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.
Binary Search Trees (BST)
Trees. What is a tree? You know…  tall  green  leafy  possibly fruit  branches  roots  I’m sure you’ve seen them.
Copyright © 2012 Pearson Education, Inc. Chapter 20: Binary Trees.
Concepts of Algorithms CSC-244 Unit 19 & 20 Binary Search Tree (BST) Shahid Iqbal Lone Computer College Qassim University K.S.A.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 20: Binary Trees.
1 Trees. Objectives To understand the concept of trees and the standard terminology used to describe them. To appreciate the recursive nature of a tree.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 20: Binary Trees.
BSTs, AVL Trees and Heaps Ezgi Shenqi Bran. What to know about Trees? Height of a tree Length of the longest path from root to a leaf Height of an empty.
Chapter 12 – Data Structures
CSCE 3110 Data Structures & Algorithm Analysis
CSCE 3110 Data Structures & Algorithm Analysis
5.13 Recursion Recursive functions Functions that call themselves
UNIT – I Linked Lists.
S. Sudarshan Based partly on material from Fawzi Emad & Chau-Wen Tseng
UNIT III TREES.
Binary Search Tree (BST)
Lecture Trees Chapter 9 of textbook 1. Concepts of trees
Trees.
Binary Search Tree Chapter 10.
Lecture 22 Binary Search Trees Chapter 10 of textbook
ITEC 2620M Introduction to Data Structures
Binary Search Trees.
Tree data structure.
Tree data structure.
i206: Lecture 13: Recursion, continued Trees
Binary Tree Applications
Chapter 20: Binary Trees.
Threaded Trees Binary trees have a lot of wasted space: the leaf nodes each have 2 null pointers We can use these pointers to help us in inorder traversals.
TREES General trees Binary trees Binary search trees AVL trees
Chapter 21: Binary Trees.
Tree data structure.
Tree data structure.
Tree data structure.
Tree A tree is a data structure in which each node is comprised of some data as well as node pointers to child nodes
Search Sorted Array: Binary Search Linked List: Linear Search
Tree data structure.
A Robust Data Structure
Binary Trees: Motivation
Trees Chapter 10.
Non-Linear Structures
Philip Bernhard, PhD Spring 2018
ECE 103 Engineering Programming Chapter 64 Tree Implementation
CSC 143 Binary Search Trees.
Binary Search Trees.
Search Sorted Array: Binary Search Linked List: Linear Search
Tree traversals BST properties Search Insertion
Trees Trees.
Tree A tree is a data structure in which each node is comprised of some data as well as node pointers to child nodes
Presentation transcript:

CSE 1002 Fundamentals of Software Development 2 More Linked Structures Philip Bernhard, PhD Spring 2018

Binary Search Trees Recall that in a Binary Search Tree (BST), the data in a given node is greater than the data in all children on the left side and is less than the data of all children on the right side.

Binary Search Tree Operations (Review) Given a BST, how do each of the following operations work? search insertion deletion

BST Operations Search for 3, 12, 15 Insert 4, 7, 9, 13, 21 Delete 3, 12, 6,….10? (notice how delete can get complicated!) 10 6 14 3 8 12 20

BST Operations Search for 3, 12, 15 Insert 4, 7, 9, 11, 19 Delete 3, 12, 6,….10? 14 10 20 8 12 6 3

BST - Operations Starting with an empty BST, insert the following values in the specified order: 9, 7, 13, 2, 8, 11, 25 Now do the same, but in the following order: 13, 25, 9, 11, 8, 7, 2 Lastly, try the following order: 2, 7, 8, 9, 11, 13, 25

Balanced vs. Unbalance BSTs Notice how insertion order determines the structure of a BST. In the first case, the resulting BST is said to be balanced. In the second case, the resulting BST is somewhat balanced, and in the third case, the resulting BST is not balanced at all. The BST in the third case is said to be degenerate.

Balanced vs. Unbalance BSTs How balanced a BST is affects searching. Searching a balanced BST takes logarithmic time. Searching a degenerate BST takes linear time. Logarithmic time good, linear time bad…

Balanced vs. Unbalance BSTs Analysis of algorithms will be discussed in CSE 2010. Also note that there are more sophisticated insertion and re-balancing algorithms that keep a BST balanced, thereby improving search times.

Link-based Binary Trees Recall the typedef introduced to implement a pointer-based Binary tree: typedef struct treeNode { int data; struct treeNode *left; struct treeNode *right; } treeNode;

Recursive Print - Linked Tree Also recall the recursive print procedure: void printTree(treeNode *tempPtr) { if(tempPtr != NULL) { printTree(tempPtr->left); // follow left first printf("\n%d", tempPtr->data); // print data in node printTree(tempPtr->right); // follow right last } return;

BST Operations What would be output from the recursive print function for the following binary tree? 10 6 14 3 8 12 20

In-Order Traversal The function performs what is called an in-order traversal. Starting at the root of any binary tree, an in-order traversal: performs an in-order traversal of it’s left sub-tree. “visits” the root performs an in-order traversal of it’s right sub-tree. In the current context, “visit” simply means output, but more generally could mean anything.

Pre-Order Traversal Similarly, a pre-order traversal will work as follows: Starting at the root of any binary tree, a pre-order traversal: “visits” the root performs a pre-order traversal of it’s left sub-tree. performs a pre-order traversal of it’s right sub-tree. What would a pre-order traversal of the previous binary tree output? And how would the code be modified for print?

Post-Order Traversal Similarly, a post-order traversal will work as follows: Starting at the root of any binary tree, a post-order traversal: performs a pre-order traversal of it’s left sub-tree. performs a pre-order traversal of it’s right sub-tree. “visits” the root What would a post-order traversal of the previous binary tree output? And how would the code be modified for print?

Array-Based BST A complete program implementing all three traversals using an array-based BST. #include <stdio.h> int tree[1000]; void initializeTree() { int i; for (i=0; i<1000; i++) tree[i] = -1; }

Array-Based BST void addNode(int index, int newData) { if(tree[index] == -1) // -1 == NULL tree[index] = newData; else { if(newData < tree[index]) addNode((index*2)+1, newData); else addNode((index*2)+2, newData); } return;

Array-Based BST void preOrderPrintTree(int index) { if(tree[index] != -1) { printf(" %d ",tree[index]); preOrderPrintTree((index*2)+1); preOrderPrintTree((index*2)+2); } return; void inOrderPrintTree(int index) { inOrderPrintTree((index*2)+1); inOrderPrintTree((index*2)+2);

Array-Based BST void postOrderPrintTree(int index) { if(tree[index] != -1) { postOrderPrintTree((index*2)+1); postOrderPrintTree((index*2)+2); printf(" %d ",tree[index]); } return;

Array-Based BST void main() { int num, status; initializeTree(); printf("Please enter an integer to be inserted "); printf("(q to quit): "); status = scanf("%ld", &num); while (status == 1) { addNode(0,num); printf("Please enter next integer (q to quit): "); } printf(“\npreorder:\n"); preOrderPrintTree(0); printf(“\ninOrder:\n"); inOrderPrintTree(0); printf(“\npostOrder:\n"); postOrderPrintTree(0); printf("\n");