Final DS Mod3

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 92

MODULE-3

 Additional List Operations


 Sparse Matrices
 Doubly Linked List
 TREES: Introduction
 Binary Trees
 Binary Tree Traversals
 Threaded Binary Trees
To reverse a List
void reverseList()
{
struct node *prev, *current;
if(head!=NULL){
prev = head;
current = head->next;
head = head->next;
prev->next=NULL;
while(head!=NULL)
{
head=head->next;
current->next=prev;
prev = current;
current=head;
}
head=prev;
}
if(head!=NULL){
printf("after reverse the list is :n");
while(head->next!=NULL)
{
printf("%dt",head->data);
head=head->next;
}
printf("%d t",head->data);
}else{
printf("List is empty. Please insert some data in list n");
}
}
To Concatenate 2 lists
void Concat(struct Node *first, struct Node *second)
{
struct Node *p = first;
while (p->next != NULL)
{
p = p->next;
}
p->next = second;
second = NULL;
}
SPARSE MATRIX REPRESENTATION
 In data representation, each column of a sparse matrix is
represented as a circularly linked list with a header node.
 A similar representation is used for each row of a sparse matrix.
 Each node has a tag field, which is used to distinguish between
header nodes and entry nodes.
Header Node:
 Each header node has three fields: down, right, and next as
shown in figure (a).
 The down field is used to link into a column list and the
right field to link into a row list.
 The next field links the header nodes together.
 The header node for row i is also the header node for column
i, and the total number of header nodes is max {number of
rows, number of columns}.
Element node:
 Each element node has five fields in addition to the tag field: row, col, down,
right, value as shown in figure (b).
 The down field is used to link to the next nonzero term in the same column
and the right field to link to the next nonzero term in the same row.
 Thus, if aij ≠ 0, there is a node with tag field = entry, value = aij, row = i, and
col = j as shown in figure (c).
 We link this node into the circular linked lists for row i and column j. Hence,
it is simultaneously linked into two different lists.
Doubly Linked List

 The difficulties with single linked lists is that, it is possible to traversal only in one
direction, ie., direction of the links.
 The only way to find the node that precedes p is to start at the beginning of the list.
The same problem arises when one wishes to delete an arbitrary node from a
singly linked list. Hence the solution is to use doubly linked list
Doubly linked list: It is a linear collection of data elements, called nodes, where
each node N is divided into three parts:
1. An information field INFO which contains the data of N
2. A pointer field RLINK (FORW) which contains the location of the next node in
the list
3. A pointer field LLINK (BACK) which contains the location of the preceding
node in the list
The declarations are:
typedef struct node *nodePointer;
typedef struct {
nodePointer llink;
element data;
nodePointer rlink;
} node;
Insertion into a doubly linked list
 Assume there are two nodes, node and newnode, node may be either a header node
or an interior node in a list. The function dinsert performs the insertion operation in
constant time.
void dinsert(nodePointer node, nodePointer newnode)
{/* insert newnode to the right of node */
newnode→llink = node;
newnode→rlink = node→rlink;
node→rlink→llink = newnode;
node→rlink = newnode;
}
Inserting a node at the end
COMP104 Doubly Linked Lists / Slide 12

Inserting a Node
 Insert a node New before Cur (not at front or
rear)
New->next = Cur;
New->prev = Cur->prev;
Cur->prev = New;
(New->prev)->next = New;

10 20 55 70

Head 40
Cur

New
Deletion from a doubly linked list
Deletion from a doubly linked list is equally easy. The function ddelete deletes the
node deleted from the list pointed to by node.
To accomplish this deletion, we only need to change the link fields of the nodes that

precede (deleted→llink→rlink) and follow (deleted→rlink→llink) the node we want


to delete.
void ddelete(nodePointer node, nodePointer deleted)
{/* delete from the doubly linked list */
if (node == deleted)
printf("Deletion of header node not
permitted.\n");
else {
deleted→llink→rlink = deleted→rlink;
deleted→rlink→llink = deleted→llink;
free(deleted) ;
}
}
Deleting a Node
 Delete a node Cur at front
(Cur->prev)->next = Cur->next;
(Cur->next)->prev = Cur->prev;
free Cur;

Dummy Head Node

10 20 40 55 70

Head Cur
 Delete a node Cur in the middle
(Cur->prev)->next = Cur->next;
(Cur->next)->prev = Cur->prev;
free Cur; // same as delete front!

Dummy Head Node

10 20 40 55 70

Cur
Head
Circular Linked List
typedef struct node
{
int info;
struct node * link;
}NODE;

NODE * last=NULL:
Insert at Front
void insert_front (int data)
{
NODE *newnode=NULL;
newnode = (NODE *)malloc(sizeof(NODE));
if (last == NULL)
{
newnode->info = data;
newnode->link=newnode;
last = newnode;
}
else
{
newnode->info = data;
newnode->link = last->link;
last->link = newnode;
}
}
Insert at End
void insert_end (int data)
{
NODE *newnode=NULL;
newnode = (NODE *)malloc(sizeof(NODE));
if (last == NULL)
{
newnode->info = data;
newnode->link=newnode;
last = newnode;
}
else
{
newnode->info = data;
newnode->link = last->link;
last->link = newnode;
last=newnode;
}
}
Delete at Front
void del_front ( )
{
NODE * temp;
if (last == NULL)
printf(“list is empty\n”);
else
{
if(last->link==last)
{
free(last);
last=NULL;
}
else
{
temp=last->link;
last->link=temp-> link;
free(temp);
}
Delete at end
void del_end ( )
{
NODE * temp;
if (last == NULL)
printf(“list is empty\n”);
else
{
if(last->link==last)
{
free(last);
last=NULL;
}
else
{
temp=last->link;
while(temp->link!=last)
temp=temp->link
temp-> link=last->link;
last=temp;
}
Display()
void display()
{
if (last == NULL)
printf("\nList is empty\n");
else
{

NODE *temp;
temp = last->link;
do {
printf("\nData = %d", temp->info);
temp = temp->link;
} while (temp != last->link);
}
}
HEADER LINKED LISTS
A header linked list is a linked list which contains a special node, called the header
node, at the beginning of the list.
The following are two kinds of widely used header lists:
1. A grounded header list is a header list where the last node contains the null
pointer.
2. A circular header list is a header list where the last node points back to the header
node.
Algorithm: (Traversing a Circular
Header List)
Let LIST be a circular header list in memory. This algorithm traverses LIST,
applying an operation PROCESS to each node of LIST.
1. Set PTR: = START→LINK. [Initializes the pointer PTR.]
2. Repeat Steps 3 and 4 while PTR ≠ START:
3. Apply PROCESS to PTR→INFO.
4. Set PTR: = PTR→LINK. [PTR now points to the next node.]
[End of Step 2 loop.]
5. Exit
Algorithm: SRCHHL (INFO, LINK, START,
ITEM, LOC)
LIST is a circular header list in memory. This algorithm finds the location LOC of
the node where ITEM first appears in LIST or sets LOC = NULL.
1. Set PTR: = START→LINK
2. Repeat while PTR→INFO [PTR] ≠ ITEM and PTR ≠ START:
Set PTR: =PTR→LINK.
[PTR now points to the next node.]
[End of loop.]
3. If PTR→INFO = ITEM,
then: Set LOC: = PTR.
Else:Set LOC: = NULL.
[End of If structure.]
4. Exit
The two properties of circular header lists:
1. The null pointer is not used, and hence all pointers contain valid addresses.
2. Every (ordinary) node has a predecessor, so the first node may not require a special
case.
There are two other variations of linked lists
3. A linked list whose last node points back to the first node instead of containing the
null pointer, called a circular list
4. A linked list which contains both a special header node at the beginning of the list
and a special trailer node at the end of the list
Introduction
 A tree structure means that the data are organized so that
items of information are related by branches
 It is a Nonlinear non primitive data structure.
 Examples:
Definition of Tree
A tree is a finite set of one or more nodes such that:
 There is a specially designated node called the root.
 The remaining nodes are partitioned into n>=0 disjoint sets
T1, ..., Tn, where each of these sets is a tree.
 We call T1, ..., Tn the subtrees of the root.

Every node in the tree is the root of some subtree


Terminology
 Node: The item of information plus the branches to other
nodes
 Degree: The number of subtrees of a node
 Degree of a tree: The maximum of the degree of the nodes in
the tree.
 Terminal nodes (or leaf): nodes that have degree zero or node
with no successor
 Nonterminal nodes: nodes that don’t belong to terminal nodes.
 Parent and Children: Suppose N is a node in T with left
successor S1 and right successor S2, then N is called the
Parent (or father) of S1 and S2. Here, S1 is called left child (or
Son) and S2 is called right child (or Son) of N.
Terminology

 Siblings: Children of the same parent are said to be siblings.


 Edge: A line drawn from node N of a T to a successor is
called an edge
 Path: A sequence of consecutive edges from node N to a
node M is called a path.
 Ancestors of a node: All the nodes along the path from the
root to that node.
 The level of a node: defined by letting the root be at level
zero. If a node is at level l, then it children are at level l+1.
 Height (or depth): The maximum level of any node in the
tree
Example
A is the root node
B is the parent of D and E
C is the sibling of B
D and E are the children of B
D, E, F, G, I are external nodes, or leaves
A, B, C, H are internal nodes
The level of E is 3
The height (depth) of the tree is 4 Level
The degree of node B is 2 A
The degree of the tree is 3 1
The ancestors of node I is A, C, H B C
The descendants of node C is F, G, H, I 2

H
3
D E F G
I
4
Representation of Trees
 There are several ways to represent a given tree such as

1. List Representation
2. Left Child- Right Sibling Representation
3. Representation as a Degree-Two tree
List Representation:
The tree can be represented as a List. The tree of figure (A) could be written as the list.
(A (B (E (K, L), F), C (G), D (H (M), I, J) ) )

 The information in the root node comes first.


 The root node is followed by a list of the subtrees of that node
 Tree node is represented by a memory node that has fields for the data and pointers to
the tree node's children
 Since the degree of each tree node may be different, so memory nodes with
a varying number of pointer fields are used.
 For a tree of degree k, the node structure can be represented as below
figure. Each child field is used to point to a subtree.
Left Child-Right Sibling Representation
 To convert the tree of Figure (A) into this representation:
 First note that every node has at most one leftmost child
 At most one closest right sibling.
Ex:
 In Figure (A), the leftmost child of A is B, and the leftmost child of D is H.
 The closest right sibling of B is C, and the closest right sibling of H is I.
 Choose the nodes based on how the tree is drawn. The left child field of each node
points to its leftmost child (if any), and the right sibling field points to its closest
right sibling (if any).
Figure (D): Left child-right sibling representation of tree of figure (A)
Representation as a Degree-Two Tree

Fig: degree-two representation

 To obtain the degree-two tree representation of a tree, simply rotate the right-sibling pointers in a left
child-right sibling tree clockwise by 45 degrees.
 This gives us the degree-two tree displayed in Figure (E).
 In the degree-two representation, a node has two children as the left and right children.
Binary Trees
Definition:
A binary tree T is defined as a finite set of nodes such that,
 T is empty or
 T consists of a root and two disjoint binary trees called
the left subtree and the right subtree.
Binary Trees
 Binary trees are characterized by the fact that any node can have at
most two branches
 Definition
 A binary tree T 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
 Thus the left subtree and the right subtree are distinguished
A A

B B

 Any tree can be transformed into binary tree


◦ by left child-right sibling representation
If T does not contain a root R,then two trees
T1 and T2 are called left and right subtrees of
R.
If T1 is nonempty, then root is called the left
successor of R
If T2 is nonempty ,then root is called right
successor of R.
Abstract Data Type Binary_Tree
structure Binary_Tree(abbreviated BinTree) is
Objects: a finite set of nodes either empty or
consisting of a root node, left Binary_Tree,
and right Binary_Tree.
functions:
for all bt, bt1, bt2  BinTree, item  element
Bintree Create()::= creates an empty binary tree
Boolean IsEmpty(bt)::= if (bt==empty binary
tree) return TRUE else return FALSE
BinTree MakeBT(bt1, item, bt2)::= return a binary tree
whose left subtree is bt1, whose right subtree is bt2,
and whose root node contains the data

item Bintree Lchild(bt)::= if (IsEmpty(bt)) return error


else return the left subtree of bt

element Data(bt)::= if (IsEmpty(bt)) return error


else return the data in the root node of bt

Bintree Rchild(bt)::= if (IsEmpty(bt)) return error


else return the right subtree of bt
Types of Binary trees
1. Skewed tree.
2. Complete binary tree
3. Strictly binary tree(full Binary tree)
4. Extended binary tree
5. Binary Search tree
Samples of Trees
Complete Binary Tree

A A 1 A

B B 2 B C

C Skewed Binary Tree


3 D E F G
D

4 H I
E 5
Skewed Tree
A skewed tree is a tree, skewed to the left or skews to the right.
or
It is a tree consisting of only left subtree or only right subtree.
 A tree with only left subtrees is called Left Skewed Binary Tree.
 A tree with only right subtrees is called Right Skewed Binary Tree.
Complete Binary Tree
 A binary tree T is said to complete if all its levels,
except possibly the last level, have the maximum
number node 2i, i ≥ 0 and if all the nodes at the last
level appears as far left as possible.
Strictly Binary Tree(Full Binary Tree)
 If every non-leaf node in a binary tree has nonempty left
and right subtrees , the tree is called a strictly binary tree.

 A binary tree in which every node has either two or zero number
of children is called Strictly Binary Tree
 A binary tree has 2i nodes in any given level i is
called strictly binary tree.
 Every node other than leaf node has 2 children.
 Level 0- 20 =1 node
 Level 1-21= 2 nodes
 Level 2=22 =4 nodes
Full Binary Tree (Strictly binary tree)

 A full binary tree of depth ‘k’ is a binary tree of depth


k having 2k – 1 nodes, k ≥ 1.

Full binary tree of level 4 with sequential node number


Extended Binary Trees or 2-trees
 An extended binary tree is a transformation of any binary tree into a complete binary
tree.
 This transformation consists of replacing every null subtree of the original tree with
“special nodes.”
 The nodes from the original tree are then internal nodes, while the special nodes are
external nodes.
Properties Of Binary Trees

Lemma 1: [Maximum number of nodes]:


The maximum number of nodes on level i of a binary tree is 2 , i ≥ 1.
i-1

The maximum number of nodes in a binary tree of depth k is 2k -1, k ≥


1.
Proof:
(1) The proof is by induction on i.
Induction Base: The root is the only node on level i = 1. Hence, the
maximum number of nodes on level i =1 is 2i-1 = 20 = 1.
Induction Hypothesis: Let i be an arbitrary positive integer greater than 1.
Assume that the maximum number of nodes on level i -1is 2i-2
Induction Step: The maximum number of nodes on level i -1 is 2i-2 by the
induction hypothesis. Since each node in a binary tree has a maximum
degree of 2, the maximum number of nodes on level i is two times the
maximum number of nodes on level i -1, or 2i-1
(2) The maximum number of nodes in a binary tree of depth k is

k k

∑ (maximum number of nodes on level i) = ∑ 2i-1 = 2k-1

i=0 i=0
Lemma 2: [Relation between number of leaf nodes
and degree -2 nodes]:
For any nonempty binary tree, T, if n0 is the number of leaf nodes and n2 the
number of nodes of degree 2, then n0 = n2 + 1.
Proof: Let n1 be the number of nodes of degree one and
n the total number of nodes.
Since all nodes in T are at most of degree two,
we have to Count the number of branches in a binary tree.
If B is the number of branches, then n =B + 1.
All branches stem from a node of degree one or two. Thus,
Subtracting Eq. (2)
n = n0 +Bn=n 1+
1+ n2 2n2. (1)
Hence, we obtain
n = B + 1= n 1+ 2n2 +
1
from Eq. (1) and rearranging terms, we get (2)

n0 = n2 +1
Here, For Figure (b) n2=4, n0= n2+1= 4+1=5
Therefore, the total number of leaf node=5
Binary Tree Representation
The storage representation of binary trees can be classified
as
 Array representation
 Linked representation
Binary tree representations (using array)
Lemma: If a complete binary tree with n nodes is represented
sequentially, then for any node with index i, 1  i  n, we have
1. parent(i) is at i /2 if i  1.
If i = 1, i is at the root and has no parent.
2. LeftChild(i) is at 2i if 2i  n.
If 2i  n, then i has no left child.
3. RightChild(i) is at 2i+1 if 2i+1  n.
If 2i +1  n, then i has no right child
Binary tree representations (using array)
 A tree can be represented using an array, which is called
sequential representation.
 The nodes are numbered from 1 to n, and one dimensional
array can be used to store the nodes.
 Position 0 of this array is left empty and the node numbered i
is mapped to position i of the array.
 For complete binary tree the array representation is ideal,
as no space is wasted.
 For the skewed tree less than half the array is utilized.
 Binary tree representations (using array)
◦ Wastage of space: in the worst case, a skewed tree of depth k
requires 2k-1 spaces. Of these, only k spaces will be occupied
◦ Insertion or deletion
of nodes from the
middle of a tree
requires the
movement of
potentially many nodes
to reflect the change in
the level of these nodes
Linked representation of Binary Tree:
The problems in array representation are:
It is good for complete binary trees, but more memory is wasted
for skewed and many other binary trees.
The insertion and deletion of nodes from the middle of a tree

require the movement of many nodes to reflect the change in level


number of these nodes.
These problems can be easily overcome by linked representation

Each node has three fields,


· LeftChild - which contains the address of left subtree
 RightChild - which contains the address of right subtree.
· Data - which contains the actual information
Ctypedef
Code for node:
struct node *treepointer;

int data;
}node;
Binary tree representations (using link)
• Binary tree representations (using link)
BINARY TREE TRAVERSALS
Visiting each node in a tree exactly once is called tree
traversal

The different methods of traversing a binary tree are:


Preorder
Inorder
Postorder
Iterative inorder Traversal
Level-Order traversal
Binary Tree Traversals
 Arithmetic Expression using binary tree
◦ inorder traversal (infix expression)
A/B*C*D+E
◦ preorder traversal (prefix expression)
+**/ABCDE
◦ postorder traversal
(postfix expression)
AB/C*D*E+
◦ level order traversal
+*E*D/CAB
Inorder:
1. Inorder traversal calls for moving down the tree toward the left until you
cannot go further. Then visit the node, move one node to the right and
continue.
2.If no move can be done, then go back one more node.

Let ptr is the pointer which contains the location of the node N currently
being scanned. L(N) denotes the leftchild of node N and R(N) is the right
child of node N
Recursion function:
The inorder traversal of a binary tree can be recursively defined as
 Traverse the left subtree in inorder.
 Visit the root.
 Traverse the right subtree in inorder.

void inorder(treepointerptr)
{
if (ptr)
{
inorder (ptr→leftchild);
printf (“%d”,ptr→data);
inorder (ptr→rightchild);
}
}
• Inorder traversal (LVR) (recursive version)

Output: A / B * C * D + E
Binary Tree Traversals
 Inorder traversal (LVR) (recursive version)
output A / B * C * D + E
:
ptr
L
V
R
Preorder:
Preorder is the procedure of visiting a node, traverse left and continue. When you cannot
continue, move right and begin again or move back until you can move right and resume.
Recursion function:
The Preorder traversal of a binary tree can be recursively defined as

 Visit the root


 Traverse the left subtree in preorder.
 Traverse the right subtree in preorder

void preorder (treepointerptr)


{
if (ptr)
{
printf (“%d”,ptr→data)
preorder (ptr→leftchild);
preorder (ptr→rightchild);
}
}
Preorder traversal (VLR) (recursive version)

Output: + * * / A B C D E
Binary Tree Traversals
 Preorder traversal (VLR) (recursive version)
output + * * / A B C D E
:

V
L
R
Postorder:
Postorder traversal calls for moving down the tree towards the left until you
can go no further. Then move to the right node and then visit the node and
continue.
Recursion function:
The Postorder traversal of a binary tree can be recursively defined as
Traverse the left subtree in postorder.
 Traverse the right subtree in postorder.
 Visit the root
void postorder(treepointerptr)

{
postorder (ptr→rightchild);
printf (“%d”,ptr→data);
}
Postorder traversal (LRV) (recursive version)

Output: A B / C * D * E +
Binary Tree Traversals
 Postorder traversal (LRV) (recursive version)
output A B / C * D * E +
:

L
R
V
Iterative inorder Traversal:

 Iterative inorder traversal explicitly make use of stack function.


 The left nodes are pushed into stack until a null node is reached,
the node is then removed from the stack and displayed, and the
node’s right child is stacked until a null node is reached.
 The traversal then continues with the left child. The traversal is
complete when the stack is empty
 Iterative inorder traversal
◦ we use a stack to simulate recursion

5 4 11
8 3 14
2 17
1
A B
/ *C D
* E
+

L
V

output A / B*C * D +E node


:
Level-Order traversal:
Visiting
the nodes using the ordering suggested by the
node numbering is called level ordering traversing.
The nodes in a tree are numbered starting with the
root on level 1 and so on.
Firstly visit the root, then the root’s left child,
followed by the root’s right child. Thus continuing in
this manner, visiting the nodes at each new level from
the leftmost node to the rightmost node.
Level order traversal: 1 2 3 4 5

The function operates by deleting the node at the front of the


queue, printing the nodes data field and adding the nodes left
and right children to the queue.
 Level-order traversal (using
queue)
output + * E * D / C A B
:
1 17 3 14 4 11 5
2 8
*+ E * D / C A B

FIFO

ptr
Binary Tree Traversals

 Analysis of inorder2 (Non-recursive Inorder traversal)


◦ Let n be the number of nodes in the tree
◦ Time complexity: O(n)
 Every node of the tree is placed on and removed
from the stack exactly once
◦ Space complexity: O(n)
 equal to the depth of the tree which
(skewed tree is the worst case)
Threaded Binary Trees
◦ Do you find any drawback of the above tree?
◦ Too many null pointers in current representation of
binary trees
n: number of nodes
number of non-null links: n-1
total links: 2n
null links: 2n-(n-1) = n+1
◦ Solution: replace these null pointers with some
useful “threads”
Threaded Binary Trees
 Definition: A threaded binary tree is a type of binary tree data structure where the empty left and
right child pointers in a binary tree are replaced with threads that link nodes directly to their in-order
predecessor or successor, thereby providing a way to traverse the tree without using recursion or a
stack.
 Rules for constructing the threads
◦ 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
Advantages and Disadvanatges

Advanatges:
 No need for stacks or recursion: Unlike binary trees, threaded
binary trees do not require a stack or recursion for their
traversal.
 Optimal memory usage: Another advantage of threaded
binary tree data structure is that it decreases memory wastage.
Disadvantages:
 Insertion and deletion operation becomes more difficult.
 Tree traversal algorithm becomes difficult.
 Memory required to store a node increases.
 Each node has to store the information whether the links is
normal links or threaded links
Threaded Binary Trees
 A Threaded Binary Tree
root A
t: true  thread
f: false  child
dangling
f B f C

D t E t F G
dangling

inorder traversal:
H I
H D I B E A F C G
Threaded Binary Tree

 Two additional fields of the node structure, left-thread and right-


thread
◦ If ptr->left-thread=TRUE,
then ptr->left-child contains a thread;
◦ Otherwise it contains a pointer to the left child.
◦ Similarly for the right-thread
Threaded Binary Trees
 If we don’t want the left pointer of H and the right pointer of G to
be dangling pointers, we may create root node and assign them
pointing to the root node
Threaded Binary Trees
Inorder traversal of a threaded binary tree
◦ By using of threads we can perform an inorder traversal
without making use of a stack (simplifying the task)
◦ Now, we can follow the thread of any node, ptr, to the
“next” node of inorder traversal

1. If ptr->right_thread = TRUE, the inorder successor of ptr is


ptr->right_child by definition of the threads
2. Otherwise we obtain the inorder successor of ptr by
following a path of left-child links from the right-child of
ptr until we reach a node with left_thread = TRUE
Threaded Binary Trees
 Finding the inorder successor (next node) of a node
threaded_pointer insucc(threaded_pointer tree){
threaded_pointer temp;
temp = tree->right_child; tree
if (!tree->right_thread)
while (!temp->left_thread)
temp = temp->left_child;
return temp;
temp
}

Inorder
Threaded Binary Trees
 Inorder traversal of a threaded binary tree
void tinorder(threaded_pointer tree){
/* traverse the threaded binary tree inorder */

threaded_pointer temp = tree; output H D I B E A FC G


for (;;) { :
temp = insucc(temp);
if (temp==tree) tree
break;
printf(“%3c”,temp->data);
}
}

Time Complexity: O(n)


Threaded Binary Trees
Inserting A Node Into a Threaded Binary Tree
Insert child as the right child of node parent
1. change parent->right_thread to FALSE
2. set child->left_thread and child->right_thread to TRUE
3. set child->left_child to point to parent
4. set child->right_child to parent->right_child
5. change parent->right_child to point to child
Threaded Binary Trees
 Right insertion in a threaded binary tree
void insert_right(thread_pointer parent, threaded_pointer child){
/* insert child as the right child of parent in a threaded binary
tree */ root
threaded_pointer temp;
child->right_child = parent->right_child; A parent
child->right_thread = parent->right_thread; B
child->left_child = parent;
child->left_thread = TRUE; C X child
parent->right_child = child;
parent->right_thread = FALSE; temp
A parent
If(!child->right_thread){
B child
temp = insucc(child);
temp->left_child = child; X
C
}
} D
First Case E F
Second Case successor

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy