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.

Slides:



Advertisements
Similar presentations
S. Sudarshan Based partly on material from Fawzi Emad & Chau-Wen Tseng
Advertisements

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.
Binary Trees, Binary Search Trees CMPS 2133 Spring 2008.
1 Jake’s Pizza Shop Owner Jake Manager Chef Brad Carol Waitress Waiter Cook Helper Joyce Chris Max Len.
Binary Trees, Binary Search Trees COMP171 Fall 2006.
CS 171: Introduction to Computer Science II
© 2006 Pearson Addison-Wesley. All rights reserved11 B-1 Chapter 11 (continued) Trees.
BST Data Structure A BST node contains: A BST contains
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved L12 (Chapter 20) Lists, Stacks,
Data Structures Using C++ 2E Chapter 11 Binary Trees and B-Trees.
Marc Smith and Jim Ten Eyck
10. Binary Trees A. Introduction: Searching a linked list.
Binary Search Trees Chapter 7 Objectives
Data Structures Using C++1 Chapter 11 Binary Trees.
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.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Data Structures Trees.
1 Trees Tree nomenclature Implementation strategies Traversals –Depth-first –Breadth-first Implementing binary search trees.
Chapter 19: Binary Trees. Objectives In this chapter, you will: – Learn about binary trees – Explore various binary tree traversal algorithms – Organize.
Lecture 17 Non-Linear data structures Richard Gesick.
Trees.ppt1 Introduction Many data structures are linear –unique first component –unique last component –other components have unique predecessor and successor.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Chapter 10: Trees Data Abstraction & Problem Solving with C++
S EARCHING AND T REES COMP1927 Computing 15s1 Sedgewick Chapters 5, 12.
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.
© 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 ),
For Monday Read Weiss, chapter 7, sections 1-3. Homework –Weiss, chapter 4, exercise 6. Make sure you include parentheses where appropriate.
Tree (new ADT) Terminology:  A tree is a collection of elements (nodes)  Each node may have 0 or more successors (called children)  How many does a.
Binary Trees, Binary Search Trees RIZWAN REHMAN CENTRE FOR COMPUTER STUDIES DIBRUGARH UNIVERSITY.
Chapter 19: Binary Trees Java Programming: Program Design Including Data Structures Program Design Including Data Structures.
Trees Dr. Andrew Wallace PhD BEng(hons) EurIng
Binary Search Trees Binary Search Trees (BST)  the tree from the previous slide is a special kind of binary tree called a binary.
1 Chapter 10 Trees. 2 Definition of Tree A tree is a set of linked nodes, such that there is one and only one path from a unique node (called the root.
Topic 15 The Binary Search Tree ADT Binary Search Tree A binary search tree (BST) is a binary tree with an ordering property of its elements, such.
1 10. Binary Trees Read Sec A. Introduction: Searching a linked list. 1. Linear Search /* Linear search a list for a particular item */ 1. Set.
Chapter 11 B Trees. © 2004 Pearson Addison-Wesley. All rights reserved 11 B-2 The ADT Binary Search Tree A deficiency of the ADT binary tree which is.
Binary trees Binary search trees Expression trees Heaps Data Structures and Algorithms in Java, Third EditionCh06 – 1.
Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy Walters, and Godfrey Muganda Modified for use at Midwestern State University Chapter.
Binary Trees Chapter 10. Introduction Previous chapter considered linked lists –nodes connected by two or more links We seek to organize data in a linked.
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.
CMSC 341 Introduction to Trees. 2/21/20062 Tree ADT Tree definition –A tree is a set of nodes which may be empty –If not empty, then there is a distinguished.
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 Chapter 10. CS 308 2Chapter Trees Preview: Preview: The data organizations presented in previous chapters are linear, in that items are one.
Copyright © 2012 Pearson Education, Inc. Chapter 20: Binary Trees.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 20: Binary Trees.
BINARY TREES Objectives Define trees as data structures Define the terms associated with trees Discuss tree traversal algorithms Discuss a binary.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 20: Binary Trees.
Binary Search Trees Chapter 7 Objectives
Data Structure and Algorithms
Trees Chapter 11 (continued)
Trees Chapter 11 (continued)
Binary Search Tree (BST)
Binary Search Tree Chapter 10.
Tree.
Lecture 22 Binary Search Trees Chapter 10 of textbook
Binary Trees, Binary Search Trees
Chapter 20: Binary Trees.
Chapter 21: Binary Trees.
Find in a linked list? first last 7  4  3  8 NULL
Search Sorted Array: Binary Search Linked List: Linear Search
Binary Trees, Binary Search Trees
Chapter 20: Binary Trees.
Binary Trees.
Trees.
Binary Trees, Binary Search Trees
Data Structures Using C++ 2E
Chapter 11 Trees © 2011 Pearson Addison-Wesley. All rights reserved.
A Binary Tree is a tree in which each node has at most 2 children
NATURE VIEW OF A TREE leaves branches root. NATURE VIEW OF A TREE leaves branches root.
Presentation transcript:

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 access point to the tree; it is called the root. A tree is recursive in nature.

Terminology Using a Binary Tree Root Child Parent Leaf Height Level

Full, Complete, Balanced A FULL TreeA COMPLETE TreeA BALANCED Tree

Traversing a Tree PRE – order (v)isit – (l)eft – (r)ightVLR POST – order (l)eft – (r)ight – (v)isitLRV IN – order (l)eft – (v)isit – (r)ightLVR

A PRE-order Traversal (VLR) This traversal is considered “top-down”

A POST-order Traversal (LRV) This traversal is considered “bottom-up”

An IN-order Traversal (LVR) This traversal is considered “left to right”

Traversal Practice

Pre Post In

Sample Code of a Traversal void Tree::InOrder(TreePtr P) { if (P != NULL) { InOrder(P->Left); Process(P); InOrder(P->Right); }

Implementing a Binary Tree Linked struct Node; typedef SomeDataType TreeType; typedef Node* TreePtr; struct Node { TreeType info; TreePtr Left, Right; };

Defining a Binary Tree Linked class Tree { public: Tree(); ~Tree(); bool Empty(); bool Insert(TreeType); bool Delete(TreeType); void Traverse(); private: void InOrder(TreePtr); TreePtr Root; };

Binary Search Trees BST A special type of tree that is very useful! Main characteristic: Given any node P, the left child is lesser than or equal to P; the right child is greater than P. The efficiency of a BST ranges from logarithmic time to linear time.

Example of BST Efficiency How many accesses to find R?

BST Efficiency Assuming a tree is balanced, it’s efficiency is approximately log 2 N where N is the number of elements in the tree. Example: There are 1000 elements in a BST, it’s efficiency therefore is approximately log = 9.9 or 10. This means that it will take in the absolute worst case, 10 accesses to find a value in the tree. If you contrast this to an ordered list, it will take 1000 accesses in the worst case and 500 in the average case to find an element!! If a tree is not balanced, it’s efficiency will degenerate!

BST Operation - Insertion The Insert function can be highly efficient. The new value is always inserted as a leaf node!

BST Operation – Insertion Practice: Build a BST Build a BST from these values: LARRY FRED JOE STEVE NANCY BILL CAROL TERRY

Inserting a Node into a BST Create a node (Test for success) Store data, set right and left pointers null (it will be a leaf) Search tree for insertion point, keep track of node which will become the parent. Attach this node to parent. Return success or failure of operation.

Deleting a Node from a BST There are three cases to account for: Leaf One Child Two Child The algorithm requires a Search to find the node to delete, determining the specific case, and then executing the deletion.

Leaf Case How do you know the node is a leaf? This routine will require 1) a pointer to the node to be deleted and 2) a pointer to the parent.

Delete Leaf Code void Bst::DeleteLeaf (TreePtr P, TreePtr Parent) { //check for root if (Parent == NULL) Root = NULL; else if (Parent->Left == P) Parent->Left = NULL; else Parent->Right = NULL; delete P; }

One Child Case How do you know the node has one child? This routine will require 1) a pointer to the node to be deleted and 2) a pointer to the parent.

Delete One Child Code void Bst::DeleteOneChild (TreePtr P, TreePtr Parent) { 1) save pointer to subtree, must be re-attached 2) check for root case 3) re-attach subtree to parent 4) delete P }

Two Child Case How do you know the node has two children? This routine will require only a pointer to the node to be deleted.

Finding the Closest Predecessor From the two child node to be deleted, take one step left and go as far right as possible. This node is the closest predecessor. Place this value in the node to be deleted. The closest predecessor will be deleted by calling DeleteLeaf or DeleteOneChild.

Delete Two Children Case void Bst::DeleteTwoChild (TreePtr P) { 1) Find closest predecessor (cp), keep track of parent to cp!! 2) Copy cp->info to P->info 3) Call DeleteLeaf or DeleteOneChild for cp }

Traversal Usage Preorder The preorder traversal can be used to effectively save a tree to file that can be reconstructed identically. This type of traversal can be used to copy a tree also. Mike Don Harry Greg Tim Paul Wayne

Traversal Usage Inorder The inorder traversal can be used to obtain a sorted list from a BST. Don Greg Harry Mike Paul Tim Wayne

Traversal Usage Postorder The postorder traversal can be used to delete a tree. A tree needs to be deleted from the bottom up because every node at the point of deletion is a leaf. Order of Deletion Greg Harry Don Paul Wayne Tim Mike

Binary Tree Implementation Array Based – method 1 The first method will store information in the tree traveling down levels going left to right. Given this storage technique, a node stored at slot I in the array will have it’s left child at 2I + 1, and the right child will be at 2I + 2. A parent can be found at (I – 1)/2.

Binary Tree Implementation Array Based – method 2 The second method will have an array of structs. Each struct will contain the information and left and right pointer fields. The pointer fields will simply be index values within the array. Each new value is added at the end of the array as a leaf and the pointer to it’s parent adjusted.

N-Ary Trees First Child-Sibling Implementation