Binary Tree PPT (Till BST)
Binary Tree PPT (Till BST)
root node
nodes
leaves
CS314 Binary Trees 2
Properties of Trees and
Nodes root
• siblings: two nodes that have the
same parent
edge
• edge: the link from one node to
another
• path length: the number of edges
that must be traversed to get from
one node to another siblings
• The height of ni is the length of the longest path from ni to a leaf. Thus all leaves in the tree are at
height 0.
• The height of a tree is equal to the height of the root. The depth of a tree is equal to the level or
depth of the deepest leaf; this is always equal to the height of the tree.
B C D
E F G H I J
K L M
N O
CS314 Binary Trees 6
Binary Trees
• There are many variations on trees but we will start with
binary trees
• binary tree: A binary tree is a finite set of nodes that is
either empty or consists of a root and two disjoint binary
trees called the left subtree and the right subtree.
• each node has at most two children
parent
K L
Binary Trees 20
Inorder Traversal (recursive version)
void inorder(tree_pointer ptr)
/* inorder tree traversal */
{
A/B*C*D+E
if (ptr) {
inorder(ptr->left_child);
printf(“%d”, ptr->data);
indorder(ptr->right_child);
}
}
CHAPTER 5 21
Preorder Traversal (recursive version)
void preorder(tree_pointer ptr)
/* preorder tree traversal */
{
+**/ABCDE
if (ptr) {
printf(“%d”, ptr->data);
preorder(ptr->left_child);
predorder(ptr->right_child);
}
}
CHAPTER 5 22
Postorder Traversal (recursive version)
void postorder(tree_pointer ptr)
/* postorder tree traversal */
{
AB/C*D*E+
if (ptr) {
postorder(ptr->left_child);
postdorder(ptr->right_child);
printf(“%d”, ptr->data);
}
}
CHAPTER 5 23
Threaded Binary Trees (Continued)
If ptr->left_child is null,
replace it with a pointer to the node that would be
visited before ptr in an inorder traversal
If ptr->right_child is null,
replace it with a pointer to the node that would be
visited after ptr in an inorder traversal
CHAPTER 5 24
A Threaded Binary Tree
root A
dangling
B C
dangling D E F G
inorder traversal:
H I H, D, I, B, E, A, F, C, G
CHAPTER 5 25
Data Structures for Threaded BT
left_thread left_child data right_child right_thread
TRUE FALSE
struct Node
{
int data;
struct Node *left, *right;
bool rightThread;
bool leftThread;
}
Memory Representation of A Threaded BT
root --
f f
f A f
f B f f C f
f D f t E t t F t t G t
t H t t I t
CHAPTER 5 27
Overview of Binary
Search Tree
Binary search tree definition:
T is a binary search tree if either of these is true
• T is empty; or
• Root has two subtrees:
• Each is a binary search tree
• Value in root > all values of the left subtree
• Value in root < all values in the right subtree
- Each node has a unique key
Chapter 8: Trees 28
CS314 Binary Trees 29
Searching a Binary Search Tree
tree_pointer search(tree_pointer root,
int key)
{
/* return a pointer to the node that
contains key. If there is no such
node, return NULL */
30 30 30
5 40 5 40 5 40
2 2 80 2 35 80
Insert 80 Insert 35
CHAPTER 5 32
Insertion into A Binary Search Tree
void insert_node(tree_pointer *node, int num)
{tree_pointer ptr,
temp = modified_search(*node, num);
if (temp || !(*node)) {
ptr = (tree_pointer) malloc(sizeof(node));
if (IS_FULL(ptr)) {
fprintf(stderr, “The memory is full\n”);
exit(1);
}
ptr->data = num;
ptr->left_child = ptr->right_child = NULL;
if (*node)
if (num<temp->data) temp->left_child=ptr;
else temp->right_child = ptr;
else *node = ptr;
}
}
CHAPTER 5 33
CS314 Binary Trees 34
CS314 Binary Trees 35
CS314 Binary Trees 36
Deletion for A Binary Search Tree
1
leaf 30
node
5 80
T1 T2
1
2
2
T1 X
T2
Deletion for A Binary Search Tree
non-leaf
40 40
node
20 60 20 55
10 30 50 70 10 30 50 70
45 55 45 52
10 20 30 40 45 50 52 55 60 70
52
Before deleting 60 After deleting 60
CHAPTER 5 38