0% found this document useful (0 votes)
51 views38 pages

Binary Tree PPT (Till BST)

The document provides an overview of trees as an abstract data type, detailing their properties, types, and traversal methods. It specifically discusses binary trees, including full, complete, and almost complete binary trees, along with various traversal algorithms such as preorder, inorder, and postorder. Additionally, it covers binary search trees, including their structure, searching, insertion, and deletion processes.

Uploaded by

iamnityasinha
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)
51 views38 pages

Binary Tree PPT (Till BST)

The document provides an overview of trees as an abstract data type, detailing their properties, types, and traversal methods. It specifically discusses binary trees, including full, complete, and almost complete binary trees, along with various traversal algorithms such as preorder, inorder, and postorder. Additionally, it covers binary search trees, including their structure, searching, insertion, and deletion processes.

Uploaded by

iamnityasinha
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/ 38

Tree internal

root node

nodes

• A tree is an abstract data type


• one entry point, the root
• Each node is either a leaf or an internal
node
• An internal node has 1 or more children,
nodes that can be reached directly from
that internal node.
• The internal node is said to be the parent
of its child nodes
leaf nodes

CS314 Binary Trees 1


Properties of Trees
• Only access point is the root
• All nodes, except the root, have one parent
• Traditionally trees drawn upside down
root

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

path length from root to this


node is 3

CS314 Binary Trees 3


More Properties of Trees
• descendants: any nodes that can be reached via 1 or more
edges from this node

• ancestors: any nodes for which this node is a descendant


The ancestors of a node are all the nodes along the path
from the root to the node

CS314 Binary Trees 4


• The level or depth of a node with respect to a tree is defined recursively: the level of the root is zero;
and the level of any other node is one higher than that of its parent. Or to put it another way, the
level or depth of a node ni is the length of the unique path from the root to ni.

• 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.

CS314 Binary Trees 5


Tree Visualization
A

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

left child right child

CS314 Binary Trees 7


FULL/STRICTLY BT
• Full / Strictly binary tree: A Binary Tree is a full binary tree if
every node has 0 or 2 children.
• If every non-leaf node in a binary tree has nonempty left and right
subtrees, the tree is termed a strictly binary tree. Or, to put it another
way, all of the nodes in a strictly binary tree are of degree zero or two,
never degree one. A strictly binary tree with N leaves always contains
2N – 1 nodes.
• We can also say a full binary tree is a binary tree in
which all nodes except leaf nodes have two children

CS314 Binary Trees 8


Full Binary Tree
•.

CS314 Binary Trees 9


Complete BT
• A complete binary tree of depth d is the strictly binary tree all of
whose leaves are at level d. The total number of nodes in a complete
binary tree of depth d equals – 1. Since all leaves in such a tree are at
level d, the tree contains 2d leaves and, therefore, 2d - 1 internal
nodes.

CS314 Binary Trees 10


Almost Complete Binary
Tree
• A binary tree of depth d is an almost complete
binary tree if:
• Each leaf in the tree is either at level d or at level d
– 1.
• For any node nd in the tree with a right descendant
at level d, all the left descendants of nd that are
leaves are also at level d.

CS314 Binary Trees 11


ACBT

CS314 Binary Trees 12


CS314 Binary Trees 13
CS314 Binary Trees 14
CS314 Binary Trees 15
CS314 Binary Trees 16
Binary Tree Traversals
• Many algorithms require all nodes of a binary tree be visited and the
contents of each node processed or examined.
• There are 4 traditional types of traversals
• preorder traversal: process the root, then process all sub trees (left to right)
NLR
• in order traversal: process the left sub tree, process the root, process the right
sub tree LNR
• post order traversal: process the left sub tree, process the right sub tree, then
process the root LRN
• level order traversal: starting from the root of a tree, process all nodes at the
same depth from left to right, then proceed to the nodes at the next depth.

CS314 Binary Trees 17


Results of Traversals
• To determine the results of a traversal on a given tree draw a path
around the tree.
• start on the left side of the root and trace around the tree. The path should
stay close to the tree. 12 pre order: process when
pass down left side of node
12 49 13 5 42
in order: process when pass
49 42
underneath node
13 49 5 12 42
13 5 post order: process when
pass up right side of node
13 5 49 42 12
CS314 Binary Trees 18
CS314 Binary Trees 19
Clicker 5 A- Tree Traversals
What is a the result of a
post order traversal of
the tree to the left?
A. FCGAKHLDJ
C D B. FGCKLHJDA
C. ACFGDHKLJ
D. ACDFGHJKL
E. LKJHGFDCA
F G H J

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

TRUE: thread FALSE: child

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 */

if (!root) return NULL;


if (key == root->data) return root;
if (key < root->data)
return search(root->left_child,
key);
return search(root->right_child,key);
} CHAPTER 5 30
Another Searching Algorithm
tree_pointer search2(tree_pointer tree,
int key)
{
while (tree) {
if (key == tree->data) return tree;
if (key < tree->data)
tree = tree->left_child;
else tree = tree->right_child;
}
return NULL;
} CHAPTER 5 31
Insert Node in Binary Search Tree

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

You might also like