DS.T.1 Trees Chapter 4 Overview Tree Concepts Traversals Binary Trees Binary Search Trees AVL Trees Splay Trees B-Trees.

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.
Binary Trees, Binary Search Trees COMP171 Fall 2006.
CS 171: Introduction to Computer Science II
Trees, Binary Trees, and Binary Search Trees COMP171.
Chapter 4: Trees General Tree Concepts Binary Trees Lydia Sinapova, Simpson College Mark Allen Weiss: Data Structures and Algorithm Analysis in Java.
Lec 15 April 9 Topics: l binary Trees l expression trees Binary Search Trees (Chapter 5 of text)
© 2006 Pearson Addison-Wesley. All rights reserved11 A-1 Chapter 11 Trees.
Trees Chapter 8. 2 Tree Terminology A tree consists of a collection of elements or nodes, organized hierarchically. The node at the top of a tree is called.
Lecture 10 Trees –Definiton of trees –Uses of trees –Operations on a tree.
CISC220 Fall 2009 James Atlas Lecture 13: Trees. Skip Lists.
CMSC 341 Introduction to Trees. 8/3/2007 UMBC CMSC 341 TreeIntro 2 Tree ADT Tree definition  A tree is a set of nodes which may be empty  If not empty,
DATA STRUCTURES AND ALGORITHMS Lecture Notes 5 Prepared by İnanç TAHRALI.
Binary Trees 2 Overview Trees. Terminology. Traversal of Binary Trees. Expression Trees. Binary Search Trees.
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.
Tree Data Structures.
Data Structures and Algorithm Analysis Trees Lecturer: Jing Liu Homepage:
Binary Trees Definition A binary tree is: (i) empty, or (ii) a node whose left and right children are binary trees typedef struct Node Node; struct Node.
Trees Chapter 8. 2 Tree Terminology A tree consists of a collection of elements or nodes, organized hierarchically. The node at the top of a tree is called.
Trees, Binary Trees, and Binary Search Trees COMP171.
Starting at Binary Trees
 Trees Data Structures Trees Data Structures  Trees Trees  Binary Search Trees Binary Search Trees  Binary Tree Implementation Binary Tree Implementation.
CSE 326: Data Structures Lecture #6 From Lists to Trees Henry Kautz Winter 2002.
1 Storing Hierarchical Information Lists, Stacks, and Queues represent linear sequences Data often contain hierarchical relationships that cannot be expressed.
Chapter 4: Trees Part I: General Tree Concepts Mark Allen Weiss: Data Structures and Algorithm Analysis in Java.
Trees By P.Naga Srinivasu M.tech,(MBA). Basic Tree Concepts A tree consists of finite set of elements, called nodes, and a finite set of directed lines.
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 Tree. Some Terminologies Short review on binary tree Tree traversals Binary Search Tree (BST)‏ Questions.
1 Chapter 4 Trees Basic concept How tree are used to implement the file system How tree can be used to evaluate arithmetic expressions How to use trees.
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.
1. Iterative Preorder Traversal Rpreorder(T) 1. [process the root node] if T!= NULL then Write Data(T) else Write “empty Tree” 2. [process the left subtree]
1 Trees General Trees  Nonrecursive definition: a tree consists of a set of nodes and a set of directed edges that connect pairs of nodes.
TREES General trees Binary trees Binary search trees AVL trees Balanced and Threaded trees.
1 Trees General Trees  Nonrecursive definition: a tree consists of a set of nodes and a set of directed edges that connect pairs of nodes.
Trees CSIT 402 Data Structures II 1. 2 Why Do We Need Trees? Lists, Stacks, and Queues are linear relationships Information often contains hierarchical.
18-1 Chapter 18 Binary Trees Data Structures and Design in Java © Rick Mercer.
1 CMSC 341 Introduction to Trees Textbook sections:
TREES From root to leaf. Trees  A tree is a non-linear collection  The elements are in a hierarchical arrangement  The elements are not accessible.
CSE 373 Data Structures Lecture 7
Trees Saurav Karmakar
Chapter 10 Trees © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved.
Trees Chapter 15.
CC 215 Data Structures Trees
Data Structures Binary Trees 1.
Week 6 - Wednesday CS221.
Binary Search Tree (BST)
Tree.
CMSC 341 Introduction to Trees.
Section 8.1 Trees.
Data Structures & Algorithm Design
CSE 373 Data Structures Lecture 7
Tree Traversals – reminder
TREE DATA STRUCTURE Data Structure 21-Sep-18 Tree
Binary Trees, Binary Search Trees
Binary Tree and General Tree
TREES General trees Binary trees Binary search trees AVL trees
CS223 Advanced Data Structures and Algorithms
Tree Traversals – reminder
Trees CSE 373 Data Structures.
Trees.
Data Structures and Algorithm Analysis Trees
Binary Trees, Binary Search Trees
Trees.
Chapter 20: Binary Trees.
Trees CSE 373 Data Structures.
Trees CSE 373 Data Structures.
General Tree Concepts Binary Trees
Binary Trees, Binary Search Trees
Presentation transcript:

DS.T.1 Trees Chapter 4 Overview Tree Concepts Traversals Binary Trees Binary Search Trees AVL Trees Splay Trees B-Trees

DS.T.2 Terminology Trees are hierarchic structures. root leaves parent children ancestors descendants path path length depth / level height subtrees depth=0 height=0

Recursive Definition: A tree is a set of nodes that is a. empty or b. has one node called the root from which zero or more trees descend. DS.T.3 General (n-ary) Arithmetic Expression Tree ( A + B + (( C * D * E ) / F ) + G ) - H How can we implement general trees with whose nodes can have variable numbers of children?

DS.T.4 Common Traveral Orders for General Trees Preorder Postorder void print_preorder ( TreeNode T) { TreeNode P; if ( T == NULL ) return; else { print T-> Element; P = T -> FirstChild; while (P != NULL) { print_preorder ( P ); P = P -> NextSibling; }

DS.T.5 ( A + B + (( C * D * E ) / F ) + G ) - H A binary tree is a tree in which each node has two subtrees--left and right. Either or both may be empty. What operations are required for a binary tree? That depends …

DS.T.6 Binary Arithmetic Expression Trees Binary Decision Trees Binary Search Trees construct from infix expression add or delete nodes traverse in preorder to produce prefix expression traverse in postorder to evaluate traverse in inorder to output infix expression

DS.T.7 Recursive Preorder Traversal void RPT (TreeNode T) { if (T != NULL) { “process” T -> Element; } Preorder Traversal with a Stack void SPT (TreeNode T, Stack S) { if (T == NULL) return; else push(T,S); while (!isempty(S)) { T = pop(S); “process” T -> Element; if (T -> Right != NULL) push(T -> Right, S); if (T -> Left != NULL) push(T -> Left, S); }

DS.T.8 Binary Search Trees Search trees are look-up tables that are used to find a given key value and return associated data. Example: look up SSN, return name and address. A binary tree satisfies the ordering property if the key value in any given node is > all key values in the node’s left subtree  all key values in the node’s right subtree

DS.T.9 Operations Find the node with a given key FindMin / FindMax key in the tree Insert a new key (and associated data) Delete a key (and associated data) Find, FindMin, FindMax, Insert are easy. Delete is a little bit tricky.

DS.T.10 Deletion of a Node from a Binary Search Tree 1. Find the node with the given key value. 2. Delete that node from the tree. Problem: When you delete a node, what do you replace it by? If it has no children, by NULL. If it has one child, by that child. If it has two children, by the node with the smallest key in its right subtree.

DS.T T Delete node with key 95.

DS.T T Find the node. It has 2 children.

DS.T T Find smallest in its right subtree. TmpCell

DS.T T Replace T’s key value with that of TmpCell. TmpCell Now delete 120 from this subtree.

DS.T T Delete 120 from this subtree T.

DS.T T Find the 120 again. TmpCell It has only one child. Replace it by this child.

DS.T T TmpCell It has only one child. Replace it by this child and free TmpCell.

DS.T

DS.T.19 What do you think of this delete procedure? Is it readable? Is it efficient? How would YOU do it?