The Binary Tree Data Structure - PowerPoint PPT Presentation

Loading...

PPT – The Binary Tree Data Structure PowerPoint presentation | free to download - id: 7b68d4-MDljM



Loading


The Adobe Flash plugin is needed to view this content

Get the plugin now

View by Category
About This Presentation
Title:

The Binary Tree Data Structure

Description:

The Binary Tree Data Structure Mugurel Ionu Andreica Spring 2012 – PowerPoint PPT presentation

Number of Views:2
Avg rating:3.0/5.0
Slides: 10
Provided by: Mug67
Learn more at: http://andrei.clubcisco.ro
Category:
Tags: binary | data | structure | tree

less

Write a Comment
User Comments (0)
Transcript and Presenter's Notes

Title: The Binary Tree Data Structure


1
The Binary Tree Data Structure
  • Mugurel Ionu? Andreica
  • Spring 2012

2
The Elements of a Binary Tree
  • composed of nodes
  • one special node the root gt 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

3
Binary Tree - example
4
Binary Tree C code
  • void setRoot(BinaryTreeNodeltTgt 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
    BinaryTreeNodeltTgt
  • left_son-gtpinfo new T
  • (left_son-gtpinfo) x
  • include ltstdio.hgt
  • include ltstdlib.hgt
  • char chstack1000 // will be used later
  • template lttypename Tgt class BinaryTreeNode
  • public
  • T pinfo
  • BinaryTreeNodeltTgt left_son, right_son,
    parent, root
  • BinaryTreeNode()
  • pinfo NULL
  • left_son right_son parent NULL
  • root this
  • void setInfo(T info)

5
Binary Tree C code (cont.)
left_son-gtparent this
left_son-gtroot root
else
left_son-gtinsert_rec(x)
else // right son
if (right_son NULL)
right_son new BinaryTreeNodeltTgt
right_son-gtpinfo new T
(right_son-gtpinfo) x
right_son-gtleft_son
right_son-gtright_son NULL
right_son-gtparent this
right_son-gtroot root
else
right_son-gtinsert_rec(x)
  • BinaryTreeNodeltTgt find(T x)
  • BinaryTreeltTgt rez
  • if (pinfo NULL)
  • return NULL
  • // Use an equality testing function
    instead
  • if ((pinfo) x)
  • return this
  • if (left_son ! NULL)
  • rez left_son-gtfind(x)
  • else
  • rez NULL
  • if (rez ! NULL)
  • return rez
  • else if (right_son ! NULL)
  • return right_son-gtfind(x)

6
Binary Tree C code (cont.)
else return NULL
void remove()
BinaryTreeNodeltTgt leaf // find a
leaf in this node's subtree leaf
findLeaf() if (this leaf)
if (parent NULL) // this root
if (this-gtpinfo
! NULL) delete this-gtpinfo
root-gtpinfo NULL else
if (parent-gtleft_son this)
parent-gtleft_son NULL
else
parent-gtright_son NULL
delete this-gtpinfo delete
this
else if
(leaf-gtparent-gtleft_son leaf)
leaf-gtparent-gtleft_son NULL
else leaf-gtparent-gtright_son
NULL leaf-gtparent parent
leaf-gtleft_son left_son
leaf-gtright_son right_son
delete this-gtpinfo this-gtpinfo
leaf-gtpinfo delete leaf
void removeInfo(T x)
BinaryTreeNodeltTgt t find(x)
if (t ! NULL) t-gtremove()
BinaryTreeNodeltTgt findLeaf()
if (left_son NULL right_son NULL)
return this
7
Binary Tree C code (cont.)
  • else
  • if (left_son ! NULL)
  • return left_son-gtfindLeaf()
  • else
  • return right_son-gtfindLeaf()
  • void preOrderTraversal()
  • printf("d\n", pinfo) / we should
    use the correct format for printing type T values
    /
  • if (left_son ! NULL)
  • left_son-gtpreOrderTraversal()
  • if (right_son ! NULL)
  • right_son-gtpreOrderTraversal()
  • void postOrderTraversal()
  • if (left_son ! NULL)
  • left_son-gtpostOrderTraversal()
  • if (right_son ! NULL)

printf("d\n", pinfo) / we should
use the correct format for printing type T values
/ void inOrderTraversal()
if (left_son ! NULL)
left_son-gtinOrderTraversal()
printf("d\n", pinfo) / we should use the
correct format for printing type T values /
if (right_son ! NULL)
right_son-gtinOrderTraversal()
void preOrderTraversal2(int level)
int i for (i 0 i lt level i)
printf("-")
printf("d\n", pinfo) / we should use the
correct format for printing type T values /
8
Binary Tree C code (cont.)
int main() srand(7290)
BinaryTreeNodeltintgt r new BinaryTreeNodeltintgt
// r-gtsetRoot(r) r-gtinsert(6)
r-gtinsert(8) r-gtinsert(1)
r-gtinsert(9) r-gtinsert(10)
r-gtinsert(4) r-gtinsert(13)
r-gtinsert(1) r-gtinsert(12)
r-gtpreOrderTraversal() printf("___\n")
r-gtpreOrderTraversal2(0) printf("___\n")
  • if (left_son ! NULL)
  • left_son-gtpreOrderTraversal2(level
    1)
  • if (right_son ! NULL)
  • right_son-gtpreOrderTraversal2(leve
    l 1)
  • void preOrderTraversal3(int level)
  • int i
  • for (i 1 i lt level i)
  • printf("c", chstacki)
  • printf(" d\n", (pinfo)) / use
    the correct format /
  • chstacklevel 1 'L'
  • if (left_son ! NULL)
  • left_son-gtpreOrderTraversal3(level
    1)
  • chstacklevel 1 'R'
  • if (right_son ! NULL)
  • right_son-gtpreOrderTraversal3(leve
    l 1)

9
Binary Tree C code (cont.)
  • r-gtpreOrderTraversal3(0)
  • printf("___\n")
  • r-gtpostOrderTraversal()
  • printf("___\n")
  • r-gtinOrderTraversal()
  • printf("___\n")
  • printf("d\n", r-gtfind(100))
  • printf("d\n", r-gtfind(1))
  • printf("d\n", r-gtfind(12))
  • printf("d\n", r-gtfind(8))
  • printf("d\n", r-gtfind(10))
  • printf("d\n", r-gtfind(20))
  • (r-gtfind(10))-gtremove()
  • printf("_______\nd\n", r-gtfind(1))
  • printf("d\n", r-gtfind(10))
  • printf("d\n", r-gtfind(12))
  • printf("d\n", r-gtfind(8))
About PowerShow.com