Introduction to C Programming CE00312-1 Lecture 23 Binary Trees.

Slides:



Advertisements
Similar presentations
Binary Trees CSC 220. Your Observations (so far data structures) Array –Unordered Add, delete, search –Ordered Linked List –??
Advertisements

S. Sudarshan Based partly on material from Fawzi Emad & Chau-Wen Tseng
Senem Kumova Metin Spring2009 BINARY TREES && TREE TRAVERSALS Chapter 10 in A Book on C.
TREES Chapter 6. Trees - Introduction  All previous data organizations we've studied are linear—each element can have only one predecessor and successor.
Binary TreesCS-2303, C-Term Binary Trees (and Big “O” notation) CS-2303 System Programming Concepts (Slides include materials from The C Programming.
CS 171: Introduction to Computer Science II
DictionaryADT and Trees. Overview What is the DictionaryADT? What are trees? Implementing DictionaryADT with binary trees Balanced trees DictionaryADT.
Kymberly Fergusson CSE1303 Part A Data Structures and Algorithms Summer Semester 2003 Lecture A12 – Binary Trees.
Tree Traversal. Traversal Algorithms preorder inorder postorder.
© 2006 Pearson Addison-Wesley. All rights reserved11 A-1 Chapter 11 Trees.
Chapter 12 C Data Structures Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc.
Kymberly Fergusson CSE1303 Part A Data Structures and Algorithms Summer Semester 2003 Lecture A12 – Binary Trees.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 12 – Data Structures Outline 12.1Introduction.
Introduction to C Programming CE Lecture 19 Linear Linked Lists.
Tree Traversal. Traversal Algorithms preorder inorder postorder.
Binary Search Trees Chapter 7 Objectives
Data Structures Using C++1 Chapter 11 Binary Trees.
Binary Trees. Node structure Data A data field and two pointers, left and right.
By : Budi Arifitama Pertemuan ke Objectives Upon completion you will be able to: Create and implement binary search trees Understand the operation.
Chapter 12 Data Structure Associate Prof. Yuh-Shyan Chen Dept. of Computer Science and Information Engineering National Chung-Cheng University.
Chapter 19: Binary Trees. Objectives In this chapter, you will: – Learn about binary trees – Explore various binary tree traversal algorithms – Organize.
Trees Chapter 15 Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013.
1 TK1924 Program Design & Problem Solving Session 2011/2012 L8: Binary Trees.
Trees EENG212 Algorithms and Data Structures. Trees Outline  Introduction to Trees  Binary Trees: Basic Definitions  Traversing Binary Trees  Node.
Trees.ppt1 Introduction Many data structures are linear –unique first component –unique last component –other components have unique predecessor and successor.
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.
CISC220 Fall 2009 James Atlas Lecture 13: Trees. Skip Lists.
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 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 A tree is a set of nodes which are connected by branches to other nodes in a 'tree-like' structure. There is a special node called the root from.
Binary Search Tree Traversal Methods. How are they different from Binary Trees?  In computer science, a binary tree is a tree data structure in which.
TREES. What is a tree ? An Abstract Data Type which emulates a tree structure with a set of linked nodes The nodes within a tree are organized in a hierarchical.
Data Structures and Algorithms TREE. Searching Sequential Searches Time is proportional to n We call this time complexity O(n) Pronounce this “big oh”
Discrete Structures Trees (Ch. 11)
Introduction to C Programming CE Lecture 24 Insertion and Deletion with Binary Search Trees.
Computer Science 112 Fundamentals of Programming II Introduction to Trees.
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.
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.
Lecture - 11 on Data Structures. Prepared by, Jesmin Akhter, Lecturer, IIT,JU Threaded Trees Binary trees have a lot of wasted space: the leaf nodes each.
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.
Trees Ellen Walker CPSC 201 Data Structures Hiram College.
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.
Graphs and Trees Mathematical Structures for Computer Science Chapter 5 Copyright © 2006 W.H. Freeman & Co.MSCS SlidesGraphs and Trees.
BINARY TREES Objectives Define trees as data structures Define the terms associated with trees Discuss tree traversal algorithms Discuss a binary.
TREES General trees Binary trees Binary search trees AVL trees Balanced and Threaded trees.
Trees By JJ Shepherd. Introduction Last time we discussed searching and sorting in a more efficient way Divide and Conquer – Binary Search – Merge Sort.
Discrete Mathematics Chapter 10 Trees.
1 Trees. 2 Trees Trees. Binary Trees Tree Traversal.
AVL Balanced Trees Introduction Data structure AVL tree balance condition AVL node level AVL rotations Choosing the rotation Performing a balanced insertion.
Binary Search Trees Chapter 7 Objectives
Chapter 12 – Data Structures
Trees Chapter 15.
Data Structure and Algorithms
Week 6 - Wednesday CS221.
Tree.
Section 8.1 Trees.
Data Structures & Algorithm Design
TREES General trees Binary trees Binary search trees AVL trees
Find in a linked list? first last 7  4  3  8 NULL
Binary Search Trees Chapter 7 Objectives
Trees.
Chapter 20: Binary Trees.
Data Structures Using C++ 2E
Chapter 11 Trees © 2011 Pearson Addison-Wesley. All rights reserved.
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:

Introduction to C Programming CE Lecture 23 Binary Trees

Binary Search Tree A binary tree has a maximum of two branches. A binary search tree has some further characteristics: 1)With respect to any particular node in the tree, all those nodes in the left sub-tree have data which are less (alphabetically for strings) than the node, and all those in the right sub-tree are greater.

A Binary Search Tree teddy fred nick darryl fong thomas rob brian colin a leaf root of tree

Searching a Binary Search Tree 2)A search for a particular data item, only involves searching down one branch at each node. In the example, searching for “fong” involves going left at “fred” (“fong” < “fred”), then right at “colin” (“fong” > “colin”), then right at “darryl” (“fong” > “darryl”) and then finding “fong” as a leaf.

Inserting into a Binary Search Tree teddy fred nick darryl fong thomas rob brian colin claude node to insert a leaf root of tree

Insertion into a Binary Search Tree 3)Insertion of a new node is similar to a search, and then linking in a new leaf for that node. In the example, a new node for “claude” would be inserted to the right of “brian”, because “claude” is less than both “fred” and “colin” but not “brian”. 4)An Inorder Traversal of the tree yields all the nodes in order (alphabetical for strings).

Efficiency Searching, insertion, deletion and sorting (see below) are efficient because half the tree is eliminated at each comparison (cf binary search with arrays). In searching for an item in a binary search tree only involves going left or right for each node as we descend the tree. This is similar to choosing first or second half during a binary search for an array. Eliminating half the data with each comparison implies the total number of comparisons is log 2 n for n items. However, this is only guaranteed if the tree is balanced!

A Binary Tree as a Linked List fred teddy nick NULLNULL NULLNULL NULLNULL NULLNULL NULLNULL root darryl

Structure of a tree node leftrightdata left sub-tree right sub-tree

Header for Binary Search Tree Let us define a header file called “tree.h” containing all our types, structures and tree function prototypes. This file can be included in all files that need to use binary search trees. We shall also keep all our tree functions in “tree.c” and an application in “treegrow.c”. All these may be compiled by: cc treegrow.c tree.c

Header file “tree.h” #include "stdio.h“// for I/O and NULL #include "stdlib.h“// for malloc #include "string.h"// for strings struct node { struct node *left; // left branch char data[21];// string data struct node *right;// right branch }; typedef struct node *Treepointer; // new type called Treepointer

Prototypes for binary search trees void inorder(Treepointer); // traverse tree void insert(Treepointer, Treepointer); // insert a node into a tree Treepointer createnode(char []); // create a node for an item Treepointer delete(Treepointer, char []); // delete an item from a tree

Inorder Traversal A traversal involves visiting all the nodes in the tree in a particular sequence. The most commonly used one is the inorder traversal. Inorder traversal 1. visits all the nodes to left of the given node, 2. then the given node itself and 3. then visits all those to the right. For a binary search tree this yields the data in sort order.

Traversing a binary search tree teddy fred nick darryl fong thomas rob brian colin a leaf root of tree

Traversals are recursive Any function that visits all the nodes in a tree has to be recursive. All traversal algorithms should be recursive. Iterative (using while loops) solutions are extremely cumbersome - not to mention very difficult - to write. This should be expected because trees themselves are recursive data structures - every tree has branches which are themselves subtrees.

Inorder Traversal #include "tree.h" void inorder(Treepointer T) {// traverse the tree, T if (T != NULL) { inorder(T -> left); // traverse left printf("%s\n", T -> data);// print data inorder(T -> right); // traverse right }//else empty tree do nothing } To print all nodes in alphabetical order use: inorder (root);

Algebraic Trees

Inorder Traversal can give the same result for different trees. Using the normal infix notation, brackets would have to be used to distinguish (A + B) * CfromA + B * C E.G. (2 + 3) * * 4 Give 2014

Reverse Polish However, postorder traversal gives different results for different trees. Thus, reverse polish notation can represent algebraic trees faithfully without the use of either brackets or operator precedences! A B + C *(do A B + first) is different from A B C * +(do B C * first) Hence the use of reverse polish by compilers

Postorder traversal Postorder traversal is almost the same as inorder traversal – both have to visit all the same nodes – but in a different sequence. The recursive algorithm should be very similar as the two traversals do similar things. Only two statements are swapped, so that for a postorder traversal, the right subtree is visited before printing the current node, T.

Postorder Traversal #include "tree.h" void postorder(Treepointer T) {// traverse the tree, T if (T != NULL) { postorder(T -> left); // traverse left postorder(T -> right);// traverse right printf("%s ", T -> data);// print data }//else empty tree do nothing } To print all nodes in reverse polish use: postorder (root);