Department of Computer Engineering Faculty of Engineering, Prince of Songkla University 1 8 – Trees.

Slides:



Advertisements
Similar presentations
Chapter 12 Binary Search Trees
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.
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
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.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Sixth Edition Chapter 19: Binary Trees.
Binary Trees Chapter 6.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Data Structures Trees.
Binary Tree. Binary Trees – An Informal Definition A binary tree is a tree in which no node can have more than two children Each node has 0, 1, or 2 children.
Recursion Bryce Boe 2013/11/18 CS24, Fall Outline Wednesday Recap Lab 7 Iterative Solution Recursion Binary Tree Traversals Lab 7 Recursive Solution.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Templatized Tree.
1 TK1924 Program Design & Problem Solving Session 2011/2012 L8: Binary Trees.
1 Joe Meehean. A A B B D D I I C C E E X X A A B B D D I I C C E E X X  Terminology each circle is a node pointers are edges topmost node is the root.
Chapter 5 Binary Trees. Definitions and Properties A binary tree is made up of a finite set of elements called nodes A binary tree is made up of a finite.
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.
CISC220 Fall 2009 James Atlas Lecture 13: Trees. Skip Lists.
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 ),
Chapter 6 Binary Trees. 6.1 Trees, Binary Trees, and Binary Search Trees Linked lists usually are more flexible than arrays, but it is difficult to use.
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.
Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy Walters, and Godfrey Muganda Modified for use at Midwestern State University Chapter.
Topics Definition and Application of Binary Trees Binary Search Tree Operations.
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.
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.
Trees 3 The Binary Search Tree Section 4.3. Binary Search Tree Also known as Totally Ordered Tree Definition: A binary tree B is called a binary search.
1 Binary Trees and Binary Search Trees Based on Dale & Co: Object-Oriented Data Structures using C++ (graphics)
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.
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.
ADT Binary Search Tree Ellen Walker CPSC 201 Data Structures Hiram College.
Binary Search Trees (BST)
Copyright © 2012 Pearson Education, Inc. Chapter 20: Binary Trees.
COSC 2P03 Week 21 Tree Traversals – reminder Breadth-first traversal: starting from root, visit all nodes on each level in turn, from left to right Depth-first.
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.
Binary Tree Implementation. Binary Search Trees (BST) Nodes in Left subtree has smaller values Nodes in right subtree has bigger values.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 20: Binary Trees.
1 Joe Meehean. A A B B D D I I C C E E X X A A B B D D I I C C E E X X  Terminology each circle is a node pointers are edges topmost node is the root.
CMSC 202, Version 5/02 1 Trees. CMSC 202, Version 5/02 2 Tree Basics 1.A tree is a set of nodes. 2.A tree may be empty (i.e., contain no nodes). 3.If.
More Binary Search Trees, Project 2 Bryce Boe 2013/08/06 CS24, Summer 2013 C.
1 CSC 211 Data Structures Lecture 25 Dr. Iftikhar Azim Niaz 1.
Trees CSIT 402 Data Structures II 1. 2 Why Do We Need Trees? Lists, Stacks, and Queues are linear relationships Information often contains hierarchical.
Chapter 7 Trees_ Part2 TREES. Depth and Height 2  Let v be a node of a tree T. The depth of v is the number of ancestors of v, excluding v itself. 
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 20: Binary Trees.
1 Binary Search Trees. 2 Binary Search Trees Binary Search Trees The Binary Search Tree (BST) Search, Insertion and Traversal of BST Removal of nodes.
(c) University of Washington20-1 CSC 143 Java Trees.
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
Binary Trees and Binary Search Trees
Binary Search Trees Chapter 7 Objectives
Week 6 - Wednesday CS221.
Tree.
Section 8.1 Trees.
Data Structures & Algorithm Design
Binary Trees, Binary Search Trees
Chapter 20: Binary Trees.
Map interface Empty() - return true if the map is empty; else return false Size() - return the number of elements in the map Find(key) - if there is an.
Chapter 21: Binary Trees.
Find in a linked list? first last 7  4  3  8 NULL
Binary Trees, Binary Search Trees
CSC 143 Java Trees.
Chapter 20: Binary Trees.
Binary Trees, Binary Search Trees
Data Structures Using C++ 2E
NATURE VIEW OF A TREE leaves branches root. NATURE VIEW OF A TREE leaves branches root.
Presentation transcript:

Department of Computer Engineering Faculty of Engineering, Prince of Songkla University 1 8 – Trees

2 Overview: 1. Tree Example 2. Binary Trees 3. Binary Trees in C 4. Tree Traversal 5. Binary Search Trees 6. Searching a BST 7. Insertion into a BST 8. Deletion from a BST

3 1. Tree Example Board of Directors Research Division Manufacturing Division Admin Marketing Computer Systems Accounts Payable Payroll

4 2. Binary Trees Each element (node) of the tree may have 0, 1 or 2 successors a c gfed b kji h

5 Binary Tree Jargon a is the root of the tree b, c are the children of a left subtree of a: the tree whose root is b right subtree of a: the tree whose root is c d, e, h, i, j are descendents of b every node is a descendent of a (the root) h, i, j, k, g are the leaves of the tree

6 Shape Examples for 6 nodes:

7 Some Binary Tree ADT Operations create an empty tree build a new tree from a data value and two subtrees deletion insertion check to see if a tree is empty print out all the elements in a tree

8 3. Binary Trees in C 3.1 Binary Tree Type 3.2 Examples 3.3 Some Functions 3.4 Size of a Binary Tree 3.5 Height of a Binary Tree

9 3.1 Binary Tree Type struct treenode { struct treenode *leftptr; int data; struct treenode *rightptr; }; typedef struct treenode TREENODE; typedef TREENODE *TREE; treenode data rightptrleftptr

Examples TREE t; The empty tree: t = NULL; A tree with a single element 2: t NULL t t 2 NN

11 t NNN NN A more complicated tree:

Some Functions int isEmptyTree ( TREE t ) { if (t == NULL) return 1; /* true */ else return 0; /* false */ }

13 int value(TREE t) { if (isEmptyTree(t)) { printf(“Tree is empty \n”); return -1; } else return t->data; } TREE mkEmpty(void) /* return an empty tree */ { return NULL; }

14 TREE mkTree(int x, TREE leftT,TREE rightT) /* make a new tree from a data value x and two existing trees */ { TREE temp; temp = malloc(sizeof(TREENODE)); temp->data = x; temp->leftptr = leftT; temp->rightptr = rightT; return temp; }

15 TREE lsTree(TREE t) { return t->leftptr; /* don’t bother dealing with the empty tree case */ } TREE rsTree(TREE t) { return t->rightptr; }

Size of a Binary Tree an empty tree has 0 size a non-empty tree has one node plus the number of elements in the subtrees n tree2 tree1 size(tree) = 1 + size(tree1) + size(tree2)

17 C Version int size(TREE t) { if (isEmptyTree(t)) return 0; else return (1 + size(lsTree(t)) + size(rsTree(t)) ); }

18 Rephrased int size(TREE t) { if (t == NULL) return 0; else return (1 + size(t->leftptr) + size(t->rightptr)); }

Height of a Binary Tree the height of an empty tree is 0 the height of a non-empty tree is 1 plus the height of the highest subtree n tree2 tree1 height of tree1 height of tree2 height(tree) = 1 + max(height(tree1), height(tree2))

20 C Version int height(TREE t) { if(isEmptyTree(t)) return 0; else return (1 + max(height(lsTree(t)), height(rsTree(t)))); }

21 2 general approaches to traversal sequence depth-first traversal proceeds along a path from the root through one child to the most distant descendent of that first child before processing a second child breadth-first traversal proceeds horizontally from the root to all of its children, then to its children’s children, and so forth until all nodes have been processed 4. Tree Traversal

22 Depth-First Traversals

23 3 different depth-first traversal sequences 1. Preorder Traversal (N-L-R) look at the root node first, then the left subtree, then the right subtree 2. Inorder Traversal (L-N-R) (or mid-order) look at the left subtree first, then the root node, then the right subtree 3. Postorder Traversal (L-R-N) look at the left subtree first, then the right subtree, then the root node 4.1 Depth-First Traversals N R L

24 Traversal Nodes Visited preorder inorder postorder Example

25 Print a Tree using Preorder void preorder_print(TREE t) { if (!isEmptyTree(t)) { printf(“%3d”, value(t)); preorder_print(lsTree(t)); preorder_print(rsTree(t)); } }

26 Inorder and Postorder Printing void inorder_print(TREE t) { if (!isEmptyTree(t)) { inorder_print(lsTree(t)); printf(“%3d”, value(t)); inorder_print(rsTree(t)); } }

27 void postorder_print(TREE t) { if (!isEmptyTree(t)) { postorder_print(lsTree(t)); postorder_print(rsTree(t)); printf(“%3d”, value(t)); } }

28 all of the children of a node before proceeding with the next level that is, given a root at level n, we process all nodes at level n before processing with the nodes at level n + 1 to traverse a tree in depth-first order, we use a stack (Remember: recursion uses stack ) to traverse a tree in breadth-first order, we use a queue 4.2 Breadth-first Traversal

29

30

31 5. Binary Search Trees 5.1 Informal Definition 5.2 Examples 5.3 C BST Type

Informal Definition A Binary Search Tree (BST) is a binary tree, with an ordering on its nodes. In every subtree (including the whole tree): all values in the left subtree are 'less than' the value in the root node all values in the right subtree are 'greater than' the value in the root node

Examples is a BST is not a BST

C BST Type Change the typedef names to reflect the fact that we are treating the tree as a BST: typedef struct treenode BSTNODE; typedef BSTNODE *BST; C’s typing is not powerful enough to include the BST ordering information

35 6. Searching a BST 6.1 Informal Definition 6.2 Example 6.3 C Version

Informal Definition To recursively search for an item (called the key) in a BST: A key cannot be in an empty tree For a non-empty tree: if key == root value, stop if key < root value, search left subtree if key > root value, search right subtree

Example Consider searching for

C Version int bstsearch(int key, BST t) { int v; if (isEmptyTree(t)) return 0; v = value(t); if (key == v) return 1; if (key < v) return bstsearch(key, lsTree(t)); if (key > v) return bstsearch(key, rsTree(t)); }

39 7. Insertion into a BST 7.1 Examples 7.2 Informal Insertion Definition 7.3 Tree Creation Functions 7.4 bstinsert() 7.5 More Efficient Insertion 7.6 Comparing bstinsert() and insert_node() 7.7 insert_node2()

Examples Insert the following into an empty BST: The resulting tree:

41 Rearrange the values, and reinsert them: The resulting tree:

Informal Insertion Definition 1. If the tree is empty, the value becomes the root of a new tree. If the tree is not empty, then: 2. If value < root, it must be inserted into the left subtree. 3. If value > root, it must be inserted into the right subtree. 4. If value == root, then stop.

Tree Creation Functions BST mkEmpty(void) { return NULL;} BST mkTree(int x,BST leftT, BST rightT) { BST t; t = malloc(sizeof(BSTNODE)); t->data = x; t->leftptr = leftT; t->rightptr = rightT; return t; }

bstinsert() BST bstinsert(int x, BST t) { int v; BST t1, t2; if (isEmptyTree(t)) return mkTree(x,mkEmpty(),mkEmpty()); v = value(t); //value of root node t1 = lsTree(t); t2 = rsTree(t); continued

45 if (x v) return mkTree(v,t1,bstinsert(x,t2)); else return mkTree(v, t1, t2); /* or return t; */ } easy to understand inefficient due to copying

46 Use: BST t, newt; t = bstinsert(5, bstinsert(7, bstinsert(1, bstinsert(2, bstinsert(4, mkEmpty()))))); newt = bstinsert(9, t);

t 4 7 newt bstinsert() has copied all of the nodes in the path from the root down to the parent node of where the number has been inserted.

More Efficient Insertion Modify the input tree to include the new value. no copying destructive assignment

49 insert_node() void insert_node(BST *tp, int value) { /* tp is a pointer to a BST */ if( *tp == NULL) { *tp = malloc(sizeof(BSTNODE)); (*tp)->data = value; (*tp)->leftptr = NULL; (*tp)->rightptr = NULL; } else if (value data) insert_node(&((*tp)->leftptr),value); else if (value > (*tp)->data) insert_node(&((*tp)->rightptr),value); else printf(“duplicate node\n”); }

50 Use: BST t = NULL; insert_node(&t, 4); /* address of pointer */ insert_node(&t, 2); insert_node(&t, 5); insert_node(&t, 7);

51 What is &((*tp)->leftptr)? tp is a pointer to a BST *tp is a BST (*tp)->leftptr is the left subtree, another BST &((*tp)->leftptr) is the address of that BST, which is the type of the first arg of insert_node() The same reasoning applies to the use of: &((*tp)->rightptr)

Comparing bstinsert() and insert_node() bstinsert() easy to understand but inefficient copying insert_node() efficient but hard to understand call by reference

insert_node2() /* call by reference replaced by returns */ BST insert_node2(BST t, int value) { if (t == NULL) { t = malloc(sizeof(BSTNODE)); t->data = value; t->leftptr = NULL; t->rightptr = NULL; } continued

54 else if (value data) t->leftptr = insert_node2(t->leftptr, value); else if (value > t->data) t->rightptr = insert_node2(t->rightptr, value); else printf("duplicate node\n"); return t; }

55 Use: BST t = NULL; t = insert_node2(t, 4); t = insert_node2(t, 2); t = insert_node2(t, 5); t = insert_node2(t, 7);

56 8. Deletion from a BST 8.1 Deletion Cases 8.2 Examples

Deletion Cases There are three cases. The deleted node has: 1) no children 2) one child 3) two children

Examples Consider the BST t:

59 Case 1: deleted node has No Children Delete 3 from t:

60 Case 2 : deleted node has 1 Child Delete 8 from t: adjust the link to the child of the deleted node 2 3

61 Case 3: deleted node has 2 Children n tree2 tree1 rest of tree delete n x x tree2 tree1 rest of tree 3.1) Select the least value in the right subtree to replace  x is the next biggest value after n

62 Example: delete 3 ( and replaced by node from the right )

63 Case 3: deleted node has 2 Children n tree1 tree2 rest of tree delete n y y tree2 tree1 rest of tree 3.2) Select the greatest value in the left subtree to replace  y is the biggest value less than n

64 Example: delete 3 (and replaced by node from the left)