The Binary Tree Data Structure

Slides:



Advertisements
Similar presentations
Pointers & Dynamic Memory Allocation Mugurel Ionu Andreica Spring 2012.
Advertisements

S. Sudarshan Based partly on material from Fawzi Emad & Chau-Wen Tseng
IKI 10100: Data Structures & Algorithms Ruli Manurung (acknowledgments to Denny & Ade Azurat) 1 Fasilkom UI Ruli Manurung (Fasilkom UI)IKI10100: Lecture20.
IKI 10100I: Data Structures & Algorithms Ruli Manurung (acknowledgments to Denny & Ade Azurat) 1 Fasilkom UI Ruli Manurung (Fasilkom UI)IKI10100I: Data.
Senem Kumova Metin Spring2009 BINARY TREES && TREE TRAVERSALS Chapter 10 in A Book on C.
Computer Science C++ High School Level By Guillermo Moreno.
DictionaryADT and Trees. Overview What is the DictionaryADT? What are trees? Implementing DictionaryADT with binary trees Balanced trees DictionaryADT.
Recursion practice. Problem 0 Using recursion (and no arrays), write the code to read in a series of numbers (until EOF) and then print them backwards.
B+ Trees Similar to B trees, with a few slight differences
Chapter 9 contd. Binary Search Trees Anshuman Razdan Div of Computing Studies
Pointers Example Use int main() { int *x; int y; int z; y = 10; x = &y; y = 11; *x = 12; z = 15; x = &z; *x = 5; z = 8; printf(“%d %d %d\n”, *x, y, z);
1 CS308 Data Structures An application of binary trees: Binary Expression Trees.
1 BST Trees A binary search tree is a binary tree in which every node satisfies the following: the key of every node in the left subtree is.
Data Structures Using C++1 Chapter 11 Binary Trees.
Properties: -Each node has a value -The left subtree contains only values less than the parent node’s value -The right subtree contains only values greater.
CSCE 3110 Data Structures & Algorithm Analysis Binary Search Trees Reading: Chap. 4 (4.3) Weiss.
Min Chen School of Computer Science and Engineering Seoul National University Data Structure: Chapter 7.
Thought for the Day “To become truly great, one has to stand with people, not above them.” – Charles de Montesquieu.
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.
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.
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.
Chapter 19: Binary Trees Java Programming: Program Design Including Data Structures Program Design Including Data Structures.
Binary Search Trees Binary Search Trees (BST)  the tree from the previous slide is a special kind of binary tree called a binary.
1 Chapter 10 Trees. 2 Definition of Tree A tree is a set of linked nodes, such that there is one and only one path from a unique node (called the root.
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 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)
Introduction to C Programming CE Lecture 24 Insertion and Deletion with Binary Search Trees.
Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy Walters, and Godfrey Muganda Modified for use at Midwestern State University Chapter.
Data Structures Using Java1 Chapter 10 Binary Trees.
1 Binary Trees and Binary Search Trees Based on Dale & Co: Object-Oriented Data Structures using C++ (graphics)
1 Lecture 21: Binary Search Tree delete etc. operations Lecturer: Santokh Singh CompSci 105 SS 2005 Principles of Computer Science.
1 Trees - Part II © Dave Bockus. 2 Building a Binary Search Tree h i b c e d f m k a Input: i h b m e c f a d k.
CSCS-200 Data Structure and Algorithms Lecture
Trees. What is a tree? You know…  tall  green  leafy  possibly fruit  branches  roots  I’m sure you’ve seen them.
Copyright © 2012 Pearson Education, Inc. Chapter 20: Binary Trees.
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.
The Linked List Data Structure Mugurel Ionu Andreica Spring 2012.
CS 240Chapter 10 – TreesPage Chapter 10 Trees The tree abstract data type provides a hierarchical to the representation of certain types of relationships.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 20: 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 child and.
BSTs, AVL Trees and Heaps Ezgi Shenqi Bran. What to know about Trees? Height of a tree Length of the longest path from root to a leaf Height of an empty.
CSCE 3110 Data Structures & Algorithm Analysis
CSCE 3110 Data Structures & Algorithm Analysis
Binary Trees and Binary Search Trees
Binary search tree. Removing a node
CISC220 Fall 2009 James Atlas Lecture 13: Binary Trees.
Binary Search Tree (BST)
Lecture No.15 Data Structures Dr. Sohail Aslam
Chapter 20: Binary Trees.
The Binary Search Tree Data Structure
Data Structures Using Java
Threaded Trees Binary trees have a lot of wasted space: the leaf nodes each have 2 null pointers We can use these pointers to help us in inorder traversals.
Chapter 21: Binary Trees.
Lec 12 March 9, 11 Mid-term # 1 (March 21?)
Search Sorted Array: Binary Search Linked List: Linear Search
The Heap Data Structure
Operations on Binary Tree
CSE 1002 Fundamentals of Software Development 2 More Linked Structures
Chapter 20: Binary Trees.
Basic Data Structures - Trees
Binary Search Trees.
Search Sorted Array: Binary Search Linked List: Linear Search
Trees Trees.
Data Structures Using C++ 2E
COSC2007 Data Structures II
Amir Kamil 8/8/02 1 B+ Trees Similar to B trees, with a few slight differences All data is stored at the leaf nodes (leaf pages); all other nodes (index.
Presentation transcript:

The Binary Tree Data Structure Mugurel Ionuț Andreica Spring 2012

The Elements of a Binary Tree composed of nodes one special node: the root => rooted trees unrooted trees also exist (but they are not studied in this course) each node has: one left son (possibly NULL) one right son (possibly NULL) one parent (NULL, in case of the root) a pointer to some useful information

Binary Tree - example

Binary Tree – C++ code void setRoot(BinaryTreeNode<T> *r) { root = r; } void insert(T x) { if (pinfo == NULL) setInfo(x); else insert_rec(x); void insert_rec(T x) { int next_son = rand() % 2; if (next_son == 0) // left son { if (left_son == NULL) { left_son = new BinaryTreeNode<T>; left_son->pinfo = new T; *(left_son->pinfo) = x; left_son->left_son = left_son->right_son = NULL; #include <stdio.h> #include <stdlib.h> char chstack[1000]; // will be used later template <typename T> class BinaryTreeNode { public: T *pinfo; BinaryTreeNode<T> *left_son, *right_son, *parent, *root; BinaryTreeNode() { pinfo = NULL; left_son = right_son = parent = NULL; root = this; } void setInfo(T info) pinfo = new T; *pinfo = info;

Binary Tree – C++ code (cont.) left_son->parent = this; left_son->root = root; } else left_son->insert_rec(x); else // right son { if (right_son == NULL) { right_son = new BinaryTreeNode<T>; right_son->pinfo = new T; *(right_son->pinfo) = x; right_son->left_son = right_son->right_son = NULL; right_son->parent = this; right_son->root = root; right_son->insert_rec(x); BinaryTreeNode<T>* find(T x) { BinaryTree<T> *rez; if (pinfo == NULL) return NULL; // Use an equality testing function instead if ((*pinfo) == x) return this; if (left_son != NULL) rez = left_son->find(x); else rez = NULL; if (rez != NULL) return rez; else if (right_son != NULL) return right_son->find(x);

Binary Tree – C++ code (cont.) else return NULL; } void remove() { BinaryTreeNode<T> *leaf; // find a leaf in this node's subtree leaf = findLeaf(); if (this == leaf) { if (parent == NULL) // this == root { if (this->pinfo != NULL) delete this->pinfo; root->pinfo = NULL; } else { if (parent->left_son == this) parent->left_son = NULL; parent->right_son = NULL; delete this->pinfo; delete this; }} else { if (leaf->parent->left_son == leaf) leaf->parent->left_son = NULL; else leaf->parent->right_son = NULL; leaf->parent = parent; leaf->left_son = left_son; leaf->right_son = right_son; delete this->pinfo; this->pinfo = leaf->pinfo; delete leaf; } void removeInfo(T x) { BinaryTreeNode<T> *t = find(x); if (t != NULL) t->remove(); BinaryTreeNode<T>* findLeaf() { if (left_son == NULL && right_son == NULL) return this;

Binary Tree – C++ code (cont.) else if (left_son != NULL) return left_son->findLeaf(); return right_son->findLeaf(); } void preOrderTraversal() { printf("%d\n", *pinfo); /* we should use the correct format for printing type T values */ left_son->preOrderTraversal(); if (right_son != NULL) right_son->preOrderTraversal(); void postOrderTraversal() { left_son->postOrderTraversal(); right_son->postOrderTraversal(); printf("%d\n", *pinfo); /* we should use the correct format for printing type T values */ } void inOrderTraversal() { if (left_son != NULL) left_son->inOrderTraversal(); if (right_son != NULL) right_son->inOrderTraversal(); void preOrderTraversal2(int level) { int i; for (i = 0; i < level; i++) printf("-");

Binary Tree – C++ code (cont.) int main() { srand(7290); BinaryTreeNode<int> *r = new BinaryTreeNode<int>; // r->setRoot(r); r->insert(6); r->insert(8); r->insert(1); r->insert(9); r->insert(10); r->insert(4); r->insert(13); r->insert(12); r->preOrderTraversal(); printf("___\n"); r->preOrderTraversal2(0); if (left_son != NULL) left_son->preOrderTraversal2(level + 1); if (right_son != NULL) right_son->preOrderTraversal2(level + 1); } void preOrderTraversal3(int level) { int i; for (i = 1; i <= level; i++) printf("%c", chstack[i]); printf("* %d\n", (*pinfo)); /* use the correct format */ chstack[level + 1] = 'L'; left_son->preOrderTraversal3(level + 1); chstack[level + 1] = 'R'; right_son->preOrderTraversal3(level + 1); };

Binary Tree – C++ code (cont.) r->preOrderTraversal3(0); printf("___\n"); r->postOrderTraversal(); r->inOrderTraversal(); printf("%d\n", r->find(100)); printf("%d\n", r->find(1)); printf("%d\n", r->find(12)); printf("%d\n", r->find(8)); printf("%d\n", r->find(10)); printf("%d\n", r->find(20)); (r->find(10))->remove(); printf("_______\n%d\n", r->find(1)); return 0; }