Trees.

Slides:



Advertisements
Similar presentations
Senem Kumova Metin Spring2009 BINARY TREES && TREE TRAVERSALS Chapter 10 in A Book on C.
Advertisements

Computer Science C++ High School Level By Guillermo Moreno.
Binary Trees, Binary Search Trees CMPS 2133 Spring 2008.
Binary Trees, Binary Search Trees COMP171 Fall 2006.
CS 171: Introduction to Computer Science II
Lec 15 April 9 Topics: l binary Trees l expression trees Binary Search Trees (Chapter 5 of text)
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.
Version TCSS 342, Winter 2006 Lecture Notes Trees Binary Trees Binary Search Trees.
Trees.ppt1 Introduction Many data structures are linear –unique first component –unique last component –other components have unique predecessor and successor.
S EARCHING AND T REES COMP1927 Computing 15s1 Sedgewick Chapters 5, 12.
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 ),
Trees and Graphs CSE 2320 – Algorithms and Data Structures Vassilis Athitsos University of Texas at Arlington 1.
Binary Trees. Binary Tree Finite (possibly empty) collection of elements A nonempty binary tree has a root element The remaining elements (if any) are.
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 ADTs Tree concepts. Applications of Trees. A Tree ADT – requirements, contract. Linked implementation of Trees. Binary Tree ADTs. Binary Search.
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, Binary Trees, and Binary Search Trees COMP171.
Computer Science: A Structured Programming Approach Using C Trees Trees are used extensively in computer science to represent algebraic formulas;
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.
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.
M180: Data Structures & Algorithms in Java Trees & Binary Trees Arab Open University 1.
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.
24 January Trees CSE 2011 Winter Trees Linear access time of linked lists is prohibitive  Does there exist any simple data structure for.
Trees and Graphs CSE 2320 – Algorithms and Data Structures Vassilis Athitsos University of Texas at Arlington 1.
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.
1 Trees What is a Tree? Tree terminology Why trees? What is a general tree? Implementing trees Binary trees Binary tree implementation Application of Binary.
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.
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.
1 Trees. 2 Trees Trees. Binary Trees Tree Traversal.
Tree Representation and Terminology Binary Trees Binary Search Trees Pointer-Based Representation of a Binary Tree Array-Based Representation of a Binary.
What is a Tree? Formally, we define a tree T as a set of nodes storing elements such that the nodes have a parent-child relationship, that satisfies the.
DS.T.1 Trees Chapter 4 Overview Tree Concepts Traversals Binary Trees Binary Search Trees AVL Trees Splay Trees B-Trees.
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
Data Structures and Design in Java © Rick Mercer
CSCE 210 Data Structures and Algorithms
Lecture 1 (UNIT -4) TREE SUNIL KUMAR CIT-UPES.
Recursive Objects (Part 4)
MCS680: Foundations Of Computer Science
Week 6 - Wednesday CS221.
Binary Search Tree (BST)
Binary Search Tree Chapter 10.
CMSC 341 Introduction to Trees.
Section 8.1 Trees.
Data Structures & Algorithm Design
Lecture 18. Basics and types of Trees
abstract containers sequence/linear (1 to 1) hierarchical (1 to many)
ITEC 2620M Introduction to Data Structures
Binary Trees, Binary Search Trees
Chapter 22 : Binary Trees, AVL Trees, and Priority Queues
COSC2100: Data Structures and Algorithms
Binary Trees.
Lec 12 March 9, 11 Mid-term # 1 (March 21?)
Find in a linked list? first last 7  4  3  8 NULL
Binary Trees Based on slides by Alyssa Harding & Marty Stepp
Trees.
Binary Trees.
Lecture 36 Section 12.2 Mon, Apr 23, 2007
Binary Trees, Binary Search Trees
Trees.
CSC 143 Java Trees.
Binary Trees.
Binary Trees, Binary Search Trees
NATURE VIEW OF A TREE leaves branches root. NATURE VIEW OF A TREE leaves branches root.
Tree (new ADT) Terminology: A tree is a collection of elements (nodes)
Presentation transcript:

Trees

What is a tree? A tree is defined to be a collection of individual entries called nodes for which the following properties hold: As long as the tree contains any nodes at all, there is a specific node called the root that forms the top of a hierarchy. Every other node is connected to the root by a unique line of descent.

Trees Are Everywhere Tree-structured hierarchies occur in many contexts outside of computer science. Game trees Biological classifications Organization charts Directory hierarchies Family trees Many more…

Tree Terminology Most terms come from family tree analogue Other terms William I is the root of the tree. Adela is a child of William I and the parent of Stephen. Robert, William II, Adela, and Henry I are siblings. Henry II is a descendant of William I, Henry I, and Matilda William I is an ancestor of everyone else. Other terms Nodes that have no children are called leaves Nodes that are neither the root nor a leaf are called interior nodes The height/depth of a tree is the length of the longest path from root to a leaf

Recursive nature of a tree Take any node in a tree together with all its descendants, the result is also a tree (called a subtree of the original one) Each node in a tree can be considered the root of its own subtree This is the recursive nature of tree structures. A tree is simply a node and a set of attached subtrees — possibly empty set in the case of a leaf node— The recursive character of trees is fundamental to their underlying representation as well as to most algorithms that operate on trees.

Binary Trees: One of the most important subclasses of trees with many practical applications A binary tree is defined to be a tree in which the following additional properties hold: Each node in the tree has at most two children. Every node except the root is designated as either a left child or a right child of its parent. This geometrical relationship allows to represent ordered collections of data using binary trees (binary search tree) left right data

Binary Search Trees < D E G A C B F < A binary search tree is defined by the following properties: Every node contains—possibly in addition to other data—a special value called a key that defines the order of the nodes. Key values are unique, in the sense that no key can appear more than once in the tree. At every node in the tree, the key value must be greater than all the keys in its left subtree less than all the keys in its right subtree.

Motivation for Using Binary Search Trees (BST) Suppose we want to keep keys sorted What will be the complexity of lookup and insert if we use an Array Lookup can be done in O(log N), how? Enter/Insert will be in O(N) How about using Linked List (LL) Lookup/Enter will be done in O(N). Why? LL cannot find middle element efficiently Can both Lookup and Enter be done in O(log N)? Yes, by using Binary search trees

Binary Search Trees A binary search tree Not a binary search tree

Binary search trees Two binary search trees representing the same set: Average depth of a node is O(log N); maximum depth of a node is O(N)

We may use a different function Tree Node Declaration typedef struct nodeT { char key; struct nodeT *left, *right; } nodeT, *treeT; struct nodeT { char key; struct nodeT *left, *right; }; typedef struct nodeT nodeT; typedef nodeT *treeT; Depending on key type We may use a different function to compare keys

Finding nodes in a binary search tree: Recursive typedef struct nodeT { char key; struct nodeT *left, *right; } nodeT, *treeT; nodeT *FindNode(nodeT *t, char key) { if (t == NULL) return NULL; if (key == t->key) return t; if (key < t->key) { return FindNode(t->left, key); } else { return FindNode(t->right, key); } t D E G A C B F nodeT *node; nodeT *t; … node=FindNode(t, ‘F’); Depending on key type We may use a different function to compare keys

Exercise: Iterative version of Finding nodes in a binary search tree typedef struct nodeT { char key; struct nodeT *left, *right; } nodeT, *treeT; nodeT *FindNode(nodeT *t, char key) { while(t !=NULL) { if (key == t->key) return t; if (key < t->key) { t = t->left; } else { t = t->right; } return NULL; t D E G A C B F nodeT *node; nodeT *t; … node=FindNode(t, ‘F’);

Tree Traversal (inorder) G A C B F void DisplayTree(nodeT *t) { if (t != NULL) { DisplayTree(t->left); printf(“%c “, t->key); DisplayTree(t->right); } nodeT *node; nodeT *t; … DisplayTree(t); A B C D E F G

Preorder and Postorder Tree Traversal G A C B F t void PreOrderWalk(nodeT *t) { if (t != NULL) { printf(“%c “, t->key); DisplayTree(t->left); DisplayTree(t->right); } void PostOrderWalk(nodeT *t) { if (t != NULL) { DisplayTree(t->left); DisplayTree(t->right); printf(“%c “, t->key); } D B A C G E F A C B F E G D

Exercise: Modify one of the traversal functions to print the tree as follow G A C B F void PrintTree(nodeT *t) { ModifyPreOrderWalk(t, 1); } void ModifyPreOrderWalk(nodeT *t, int h) { int i; if (t == NULL) return; for(i=0; i < h-1; i++) { printf(“| “); } if (h>1) printf(“+---“); printf(“%c\n“, t->key); ModifyPreOrderWalk(t->left, h+1); ModifyPreOrderWalk(t->right, h+1); D +---B | +---A | +---C +---G | +---E | | +---F

Exercise: Height typedef struct nodeT { char key; struct nodeT *left, *right; } nodeT, *treeT; Suppose we want to find the height of a Binary Search Tree t Write a function that returns the height of the tree Recursive Idea 1 + Maximum of (height of left subtree, height of right subtree). int height(nodeT *t) { if (t == NULL) return 0; else return (1 + maximumof( height(t->left), height(t->right)) ); }

Exercise: Sum, Min, Max struct tree_node { int data; struct tree_node *left, *right; } Suppose we store integer values in a Binary Search Tree t Write a function that returns the sum of values in the tree Recursive Idea Value of the current node + the sum of values of all nodes of left subtree + the sum of values of all nodes in right subtree. How about max, min in bst or just in a binary tree where values may not be sorted? int add(struct tree_node *p) { if (p == NULL) return 0; else return (p->data + add(p->left) + add(p->right) ); }