0% found this document useful (0 votes)
117 views15 pages

Trees in C

This document discusses binary trees and their implementation in C. It defines a binary tree as having empty, data, left subtree, and right subtree. It shows the struct for a binary tree node with data, left, and right pointers. It demonstrates how to create and traverse a binary tree in preorder, inorder, and postorder fashion. It also covers deleting binary trees and mapping trees to arrays.

Uploaded by

Sayali Shivarkar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
117 views15 pages

Trees in C

This document discusses binary trees and their implementation in C. It defines a binary tree as having empty, data, left subtree, and right subtree. It shows the struct for a binary tree node with data, left, and right pointers. It demonstrates how to create and traverse a binary tree in preorder, inorder, and postorder fashion. It also covers deleting binary trees and mapping trees to arrays.

Uploaded by

Sayali Shivarkar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 15

Trees in C

CSE 2541
Matt Boggus

Tree definition
Recursively defined data structure
Tree (in general)
Empty
Data + a specific number of subtrees

Binary tree
Empty
Data + left subtree + right subtree

C Binary Tree node

struct btnode {
int data;
struct btnode *left;
struct btnode *right;
};

What is this?

struct treenode {
int data;
struct treenode *mynode;
};

Equivalent to a linked list

Creating a tree
struct btnode *root;
struct btnode *mynode =
(struct btnode *) malloc (sizeof(struct
btnode));
root = mynode;
// use root to access the tree, like head for a
linked list

Visual example of a binary


tree

Each value corresponds to a node in the tree


root is a struct btnode pointer that points at the node containing the 9

Tree traversal (preorder)

PreOrderPrint(struct btnode *anode)


{
printf(%i, anode->data);
PreOrderPrint(anode->left);
PreOrderPrint(anode->right);
}
Any problems with this function?

Tree traversal (preorder


corrected)

PreOrderPrint(struct btnode *anode)


{
if(anode == NULL) return;
printf(%i , anode->data);
PreOrderPrint(anode->left);
PreOrderPrint(anode->right);
}
Output of PreOrderPrint(root) is:
9 6 2 7 15 12 25

Tree traversal (inorder)

InOrderPrint(struct btnode *anode)


{
if(anode == NULL) return;
InOrderPrint(anode->left);
printf(%i , anode->data);
InOrderPrint(anode->right);
}
Output of InOrderPrint(root) is:
2 6 7 9 12 15 25

Tree traversal (postorder)

PostOrderPrint(struct btnode *anode)


{
if(anode == NULL) return;
PostOrderPrint(anode->left);
PostOrderPrint(anode->right);
printf(%i , anode->data);
}
Output of PostOrderPrint(root) is:
2 7 6 12 25 15 9

Tree termination
NULL pointers
NULL are not
btnodes, but the
value of their
parents left and
right pointers

NULL data
-1 are btnodes,
whose left and
right btnodes are
uninitialized
Assumption: -1 is never
valid data in the tree

Creating nodes
struct node * NewNode(int data) {
struct node *mynode = (struct node *) malloc (sizeof(struct
node));
mynode->data = data;
mynode->left = NULL;
mynode->right = NULL;
return(node);
}

Deallocating binary trees


Three things to do
Free current node
Recursively free left subtree
Recursively free right subtree

What is the order?


void delete_tree(struct btnode *leaf) {
if( leaf != NULL ) {
delete_tree(leaf->left);
delete_tree(leaf->right);
free( leaf );
}
}

Trees and arrays


Map current node, left child, and right child to array positions

Order in the
array by level
in the tree

Figure from http://scientopia.org/blogs/goodmath/2008/04/29/implementing-compactbinary-heaps/

Trees and arrays


Map current node, left child, and right child to array positions
t[i]
, t[2i+1] ,
t[2i+2]

Order in the
array by level
in the tree

Figure from http://scientopia.org/blogs/goodmath/2008/04/29/implementing-compactbinary-heaps/

You might also like