You are on page 1of 36

# Binary Trees

## | Finding a specified element in a linked list is

not so easy. you must start at the beginning of
the list and visit each element until you find the
one you¶re looking for, because there¶s no way
to access a given element without following the
chain of references to it.
t would be nice if there were ë ëë 
 

     ë     ë  
  ë  ë 
   ë
  ë 
ëë. Trees provide both these characteristics.
ëë
|
tree consists of nodes connected by edges.

B C

D E F G

H
C

F G
Terminology
| ë

## Think of someone walking from node to node

along the edges that connect them. The resulting
sequence of nodes is called a path.

| 3
The node at the top of the tree is called the root.
For a collection of nodes and edges to be
defined as a tree, there must be one (and only
one) path from the root to any other node.
| ë 

ny node (except the root) has exactly one edge
running upward to another node. The node
above it is called the parent of the node.

| !

The nodes below a given node are called its
children.

| xë

node that has no children is called a leaf node.


## ny node may be considered to be the root of a

subtree, which consists of its children and its
children¶s children, and so on.
ë
To traverse a tree means to visit all the nodes in
some specified order.
x
The level of a particular node refers to how
many generations the node is from the root.
Tree Node evel and Path ength

evel: 0

   evel: 1

  evel: 2

 evel: 3
| x 
| number of edges on the path
| O   ë 
| length of the unique path from the root to that
node
| 
 
 ë  is equal to the depth of the
deepest leaf
|    ë 
| length of the longest path from that node to a leaf
| all leaves are at height 0
| 

 
  ë  is equal to the height of the
root
|
  and p  p 
|  
  and  
p  p 
Binary Trees
f every node in a tree can have at most two
children, the tree is called a binary tree.

## The kind of binary tree we¶ll be dealing with is

technically called a binary search tree.

node¶s left child must have a key less than
its parent, and a node¶s right child must have a
key greater than or equal to its parent.
0

30 

1 3
2 

20
Defining a Node

## typedef struct Node_Type

{
int item;
struct Node_Type *left;
struct Node_Type *right;
}Node;
void insert(Node **root,int val)
{
Node *newnode = (Node *)malloc(sizeof(Node));
newnode->item = val;
newnode->left = NULL;
newnode->right = NULL;
if(*root == NULL)
*root = newnode;
else
{
Node* current = *root;
Node* parent;
while(1) {
parent = current;
if(val<current->item)/*go to left*/
{
current = current -> left;
if(current == NULL)
parent ->left = newnode;
}
else
{
current = current->right;
if(current == NULL)
parent->right = newnode;
}
} /*end while*/
} /*end else*/
}/* end of insert */
Deleting a Node
| Start by finding the node you want to delete.
Once you have find the node, there are three
cases to consider.
1. The node to be deleted is a leaf (no
children)
2. The node to be deleted has one child
3. The node to be deleted has two
children
!ë 
Once we¶ve found the node, we check first to
see whether it has no children. When this is true we
check the special case of the root. f that is the node
to be deleted, we simply set it to NULL. Otherwise,
we set the parent¶s leftchild or rightchild field to
NULL to disconnect the parent from the node.
!ë 
Change the appropriate reference in the
parent to point to the deleted node¶s child.
| !ë 

## To delete a node with two children, replace the node

with its  
 (the node with the next
highest key).

| Y   
 

## | Successor is the smallest of the set of nodes that are

larger than the original node.

## | Go to the right child, then follow down the path of all

this right child¶s left children.
Find successor node
Node* getsuccessor(Node *delnode)
{
Node *successorparent = delnode;
Node *successor = delnode;
Node *current = delnode->right;
while(current!=NULL)
{
successorparent = successor;
successor = current;
current = current->left;
}
if(successor != delnode ->right)
{
succesorparent->left = successor->right;
successor->right = delnode;
}
return successor;
}
f successor is right child of delnode

## Unplug current from the rightchild(leftchild) field

of its parent, and set this field to point to the
successor.

## Unplug current¶s left child from current and plug

it into the left child field of successor.

## parent. rightchild = successor;

successor.leftchild = current.leftchild;
R     ë    
 
 
 
Plug the right child of successor into the left child
filed of the successor.
Plug the right of the node to be deleted into the
rightchild field of successor.
Unplug current from the rightchild field of its parent,
and set this field to point to successor.
Unplug current¶s left child from current, and plug it
into the left child field of successor.
successorparent.leftchild = successor.rightchild;
successor.rightchild = delnode.rightchild;
parent.rightchild = successor;
successor.leftchild = current.leftchild;
typedef enum {false,true}boolean;
boolean deletenode(Node **root,int val)
{
Node *current = *root;
Node *parent = *root;
boolean isLeftchild=true;
while(current->item!=val) //search the Node
{
parent=current;
if(val<current->item) //go to left
{
isLeftchild=true;
current=current->left;
}
else //go to right
{
isLeftchild=false;
current=current->right;
}

if(current==NULL)
return false;
}
//found the Node, now delete it
//if no children simply remove it
if(current->left==NULL && current->right==NULL)
{
if(current==*root) //if it is the root node,just
//make it null
*root=NULL;
else if(isLeftchild) //else disconnect from
//parent
parent->left=NULL;
else
parent->right=NULL;
}
//if no right child, replace with left subtree

else if(current->right==NULL)
{
if(current==*root)
*root=current->left;
else if(isLeftchild)
parent->left=current->left;
else
parent->right=current->left;
}
//if no left child, replace with right subtree
else if(current->left==NULL)
{
if(current==*root)
*root=current->right;
else if(isLeftchild)
parent->left=current->right;
else
parent->right=current->right;
}
else //two children, so replace with
//inorder successor
{
Node *successor=getsuccessor(current);
//connect parent to the successor node
if(current==*root)
*root=successor;
else if(isLeftchild)
parent->left=successor;
else
parent->right=successor;
//connect successor to current's left child
successor->left=current->left;
}
free(current);
return true;
}
 ëë

| inorder traversal

| Preorder traversal

| Postorder traversal
norder traversal

## | left, node, right.

| infix expression
|a+b*c+d*e+f*g
void inorder(Node *current)
{
if(current!=NULL)
{
inorder(current->left);
printf("%3d",current->item);
inorder(current->right);
}
}
 ëë

## | node, left, right

| prefix expression
|++a*bc*+*defg
void preorder(Node *current)
{
if(current!=NULL)
{
printf("%3d",current->item);
preorder(current->left);
preorder(current->right);
}
}
 ëë

## |left, right, node

|postfix expression
|abc*+de*f+g*+
2   
 

 


   
   
   
!
!