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.

Slides:



Advertisements
Similar presentations
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.
Advertisements

S. Sudarshan Based partly on material from Fawzi Emad & Chau-Wen Tseng
1 abstract containers hierarchical (1 to many) graph (many to many) first ith last sequence/linear (1 to 1) set.
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.
1 Trees. 2 Outline –Tree Structures –Tree Node Level and Path Length –Binary Tree Definition –Binary Tree Nodes –Binary Search Trees.
Lec 15 April 9 Topics: l binary Trees l expression trees Binary Search Trees (Chapter 5 of text)
1 abstract containers hierarchical (1 to many) graph (many to many) first ith last sequence/linear (1 to 1) set.
Trees Eric Roberts CS 106B February 20, In our Last Episode... In Friday’s class, I showed how hashing makes it possible to implement the get and.
Binary Search Trees Chapter 7 Objectives
Binary Trees Chapter 6.
Version TCSS 342, Winter 2006 Lecture Notes Trees Binary Trees Binary Search Trees.
CS 146: Data Structures and Algorithms June 18 Class Meeting Department of Computer Science San Jose State University Summer 2015 Instructor: Ron Mak
By : Budi Arifitama Pertemuan ke Objectives Upon completion you will be able to: Create and implement binary search trees Understand the operation.
Lecture Objectives  To learn how to use a tree to represent a hierarchical organization of information  To learn how to use recursion to process trees.
Lecture 10 Trees –Definiton of trees –Uses of trees –Operations on a tree.
S EARCHING AND T REES COMP1927 Computing 15s1 Sedgewick Chapters 5, 12.
Spring 2010CS 2251 Trees Chapter 6. Spring 2010CS 2252 Chapter Objectives Learn to use a tree to represent a hierarchical organization of information.
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 ),
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.
TREES Lecture 12 CS2110 – Fall Announcements  Prelim #1 is tonight!  Olin 155  A-L  5:30  M-Z  5:30  A4 will be posted today  Mid-semester.
Binary Search Trees Binary Search Trees (BST)  the tree from the previous slide is a special kind of binary tree called a binary.
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.
Trees  Linear access time of linked lists is prohibitive Does there exist any simple data structure for which the running time of most operations (search,
 Trees Data Structures Trees Data Structures  Trees Trees  Binary Search Trees Binary Search Trees  Binary Tree Implementation Binary Tree Implementation.
Lec 15 Oct 18 Binary Search Trees (Chapter 5 of text)
Preview  Graph  Tree Binary Tree Binary Search Tree Binary Search Tree Property Binary Search Tree functions  In-order walk  Pre-order walk  Post-order.
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.
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/14/20161 BST Operations Data Structures Ananda Gunawardena.
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.
TREES K. Birman’s and G. Bebis’s Slides. Tree Overview 2  Tree: recursive data structure (similar to list)  Each cell may have zero or more successors.
24 January Trees CSE 2011 Winter Trees Linear access time of linked lists is prohibitive  Does there exist any simple data structure for.
Concepts of Algorithms CSC-244 Unit 19 & 20 Binary Search Tree (BST) Shahid Iqbal Lone Computer College Qassim University K.S.A.
BINARY TREES Objectives Define trees as data structures Define the terms associated with trees Discuss tree traversal algorithms Discuss a binary.
TREES Lecture 11 CS2110 – Spring Readings and Homework  Textbook, Chapter 23, 24  Homework: A thought problem (draw pictures!)  Suppose you use.
1 Balanced trees. Binary Search Tree: Balancing Problem nodeT *t=NULL; … InsertNode(&t, ‘G’); InsertNode(&t, ‘F’); InsertNode(&t, ‘E’); InsertNode(&t,
Trees CSIT 402 Data Structures II 1. 2 Why Do We Need Trees? Lists, Stacks, and Queues are linear relationships Information often contains hierarchical.
Node Declaration and Example typedef struct nodeT { int key; struct nodeT *left, *right; } nodeT, *treeT;
1 Trees. 2 Trees Trees. Binary Trees Tree Traversal.
DS.T.1 Trees Chapter 4 Overview Tree Concepts Traversals Binary Trees Binary Search Trees AVL Trees Splay Trees B-Trees.
CSE 373 Data Structures Lecture 7
CSCE 210 Data Structures and Algorithms
MCS680: Foundations Of Computer Science
Binary Search Tree (BST)
Trees.
Lecture 22 Binary Search Trees Chapter 10 of textbook
Trees.
Data Structures & Algorithm Design
CSE 373 Data Structures Lecture 7
abstract containers sequence/linear (1 to 1) hierarchical (1 to many)
Binary Trees, Binary Search Trees
Chapter 22 : Binary Trees, AVL Trees, and Priority Queues
Find in a linked list? first last 7  4  3  8 NULL
Trees CSE 373 Data Structures.
Binary Trees, Binary Search Trees
Trees.
Trees CSE 373 Data Structures.
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:

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 and how that recursive structure is reflected in its underlying representation. To become familiar with the data structures and algorithms used to implement binary search trees. To recognize that it is possible to maintain balance in a binary search tree as new keys are inserted. To learn how binary search trees can be implemented as a general abstraction. 2

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

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… 4

Tree Terminology Most terms come from family tree analogue 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 5

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

Representing family trees in C How can we represent the hierarchical (parent/children) relationships among the nodes Include a pointer in the parent to point the child A tree is a pointer to a node. A node is a structure that contains some number of trees. 7 #define MaxChildren 5 struct familyNodeT { char *name; struct familyNodeT *children[MaxChildren]; };

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) 8 leftright data

Binary Search Trees A binary search tree is defined by the following properties: 1. Every node contains—possibly in addition to other data—a special value called a key that defines the order of the nodes. 2. Key values are unique, in the sense that no key can appear more than once in the tree. 3. 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. 9 D E G A C B F < <

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 10

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

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

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

Finding nodes in a binary search tree: Recursive typedef struct nodeT { char key; struct nodeT *left, *right; } nodeT, *treeT; 14 nodeT *FindNode(nodeT *t, char key) { if (t == NULL) return NULL; if (key == t->key) return t; if (key key) { return FindNode(t->left, key); } else { return FindNode(t->right, key); } D E G A C B F nodeT *node; nodeT *t; … node=FindNode(t, ‘F’); t 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; 15 nodeT *FindNode(nodeT *t, char key) { while(t !=NULL) { if (key == t->key) return t; if (key key) { t = t->left; } else { t = t->right; } return NULL; } D E G A C B F nodeT *node; nodeT *t; … node=FindNode(t, ‘F’); t

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

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

Exercise: Modify one of the traversal functions to print the tree as follow D +---B | +---A | +---C +---G | +---E | | +---F 18 D E 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); } t

Exercise: Height 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). 19 int height(nodeT *t) { if (t == NULL) return 0; else return (1 + maximumof( height(t->left), height(t->right)) ); } typedef struct nodeT { char key; struct nodeT *left, *right; } nodeT, *treeT;

Exercise: Sum, Min, Max 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? 20 int add(struct tree_node *p) { if (p == NULL) return 0; else return (p->data + add(p->left) + add(p->right) ); } struct tree_node { int data; struct tree_node *left, *right; }

Exercise: Tree Traversal level order void DisplayTreeLevelOrder(nodeT *t) { if (t != NULL) { ?????? } 21 D E G A C B F nodeT *node; nodeT *t; … DisplayTreeLevelOrder(t); D B G A C E F t A little bit harder version D B G A C E F

Insert - Delete 22

Creating a tree in a client program 23 main() { nodeT nt1, *nt2; treeT tt1, *tt2; nt2 = (nodeT *) malloc(sizeof(nodeT)); if(nt2==NULL) { printf( ” no memory ” ); exit(-1); } nt1.key = ‘A’; nt1.left = nt1.right= NULL; nt2->key = ‘D’; nt2->left = nt2->right= NULL; tt1 = nt2; tt2 = &nt2; InsertNode(&nt2, ‘B’); // InsertNode(tt2, ‘B’); } nt1 nt2 typedef struct nodeT { char key; struct nodeT *left, *right; } nodeT, *treeT; key ? left ? right ? key ? left ? right ? tt1 tt2 key A left 0 right 0 key D left 0 right 0 void InsertNode(nodeT **tptr, char key) B // nodeT *tt1, **tt2 // nt2 = New(nodeT *) ;

Inserting new nodes in a binary search tree typedef struct nodeT { char key; struct nodeT *left, *right; } nodeT, *treeT; 24 void InsertNode(nodeT **tptr, char key) { nodeT *tmp; tmp=*tptr; if (tmp == NULL) { tmp=New(nodeT *); tmp->key = key; tmp->left=tmp->right=NULL; *tptr=tmp; return; } if (key key) { InsertNode(&tmp->left, key); } else { InsertNode(&tmp->right, key); } } D E G A C B F nodeT *node; nodeT *t=NULL; InsertNode(&t, ‘D’); InsertNode(&t, ‘B’); … t void InsertNode(nodeT **tptr, char key) { if (*tptr == NULL) { *tptr=New(treeT); (*tptr)->key = key; (*tptr)->left=(*tptr)->right=NULL; return; } if (key key) { InsertNode(&(*tptr)->left, key); } else { InsertNode(&(*tptr)->right, key); }

Exercise: modify this such that each node points parent node 25 typedef struct nodeT { char key; struct nodeT *parent; struct nodeT *left, *right; } nodeT, *treeT; void InsertNode(nodeT **tptr, char key) { nodeT *tmp; tmp=*tptr; if (tmp == NULL) { tmp=New(nodeT *); tmp->key = key; tmp->left=tmp->right=NULL; *tptr=tmp; return; } if (key key) { InsertNode(&tmp->left, key); } else { InsertNode(&tmp->right, key); }

Deleting nodes from a binary search tree 26 D E G A C B F Delete A D E G C B F D E G A C B F Delete E D G A C B F D E G A C B F Delete G D E A C B F D E G A C B F Delete B or D ??? target->left==NULL && target->right==NULL target->left == NULLtarget->right == NULL

Deleting nodes from a binary search tree: easy cases typedef struct nodeT { char key; struct nodeT *left, *right; } nodeT, *treeT; 27 nodeT *t; // Suppose we want to delete ‘E’, // find/determine pointer to target node DeleteNode(&t->right->left); // if ‘A’, DeleteNode(&t->left->left); void DeleteNode(nodeT **p) { nodeT *target; target=*p; if (target->left==NULL && target->right==NULL) { *p=NULL; } else if (target->left == NULL) { *p=target->right; } else if (target->right == NULL) { *p=target->left; } else { /* target has two children, see next slide */ } free(target); } t D E A G C B F left right D G A C B F

Deleting nodes from a binary search tree: two children 28 D E G A C B F void DeleteNode(nodeT **p) { nodeT *target, *lmd_r, *plmd _r ; target=*p; … /* easy cases, see previous slide */ } else { plmd _r = target; lmd _r = target->right; while( lmd _r ->left != NULL){ plmd _r = lmd _r ; lmd _r = lmd _r ->left; } plmd _r ->left = lmd _r ->right; lmd _r ->left = target->left; lmd _r ->right = target->right; *p = lmd _r; } free(target); } 1.Replace the target node with its immediate successor, which is the smallest value in the right subtree (lmd_r -- leftmost descendant in right subtree) 2.Delete lmd_r (easy case, why?) E F G A C C DeleteNode(&t); Is there any other case missing! target->key = lmd_r->key; target = lmd_r; tt (incomplete)

Deleting nodes from a binary search tree: two children (corrected) 29 D G A C B Z void DeleteNode(nodeT **p) { nodeT *target, *lmd_r, *plmd _r ; target=*p; … /* easy cases, see previous slide */ } else { plmd _r = target; lmd _r = target->right; while( lmd _r ->left != NULL){ plmd _r = lmd _r ; lmd _r = lmd _r ->left; } if(plmd_r == target) plmd_r->right = lmd_r->right; else plmd_r->left = lmd_r->right; lmd _r ->left = target->left; lmd _r ->right = target->right; *p = lmd _r; } free(target); } 1.Replace the target node with its immediate successor, which is the smallest value in the right subtree (lmd_r -- leftmost descendant in right subtree) 2.Delete lmd_r (easy case, why?) G Z A C C DeleteNode(&t); Can you think of another strategy (see the exercise in the next slide) target->key = lmd_r->key; target = lmd_r; tt

Exercise: new strategy for deleting nodes from a binary search tree: two children 30 D E G A C B F void DeleteNode(nodeT **p) /* Modify this one*/ { nodeT *target, *lmd_r, *plmd _r ; target=*p; // *rmd_l, *prmd_l; … /* easy cases see previous slide */ } else { plmd _r = target; lmd _r = target->right; while( lmd _r ->left != NULL){ plmd _r = lmd _r ; lmd _r = lmd _r ->left; } if(plmd_r == target) plmd_r->right = lmd_r->right; else plmd_r->left = lmd_r->right; lmd _r ->left = target->left; lmd _r ->right = target->right; *p = lmd _r; } free(target); } 1.Replace the target node with its immediate predecessor, which is the largest value in the left subtree (rmd_l -- rightmost descendant in left subtree) 2.Delete rmd_l (easy case, why?) C E G A F B Delete D target->key = lmd_r->key; target = lmd_r;

Exercise: Deleting a node with two children Randomly select one of the previous strategies Which one will give better balanced tree 31

Recursion in Binary Trees It's very difficult to think about how to iteratively go through all the nodes in a binary tree unless you think recursively??? With recursion, the code is reasonably concise and simple. But in some cases iterative approaches might be possible and more efficient 32