Binary trees יום חמישי 02 יולי 2015 יום חמישי 02 יולי 2015 יום חמישי...

20
Binary trees 17:39:03 1 Department of Computer Science-BGU

Transcript of Binary trees יום חמישי 02 יולי 2015 יום חמישי 02 יולי 2015 יום חמישי...

Page 1: Binary trees יום חמישי 02 יולי 2015 יום חמישי 02 יולי 2015 יום חמישי 02 יולי 2015 יום חמישי 02 יולי 2015 יום חמישי 02 יולי

Binary trees

00:23:33

1

Department of Computer Science-BGU

Page 2: Binary trees יום חמישי 02 יולי 2015 יום חמישי 02 יולי 2015 יום חמישי 02 יולי 2015 יום חמישי 02 יולי 2015 יום חמישי 02 יולי

Tree Structure

Tree structure is a way of representing the hierarchical nature of a structure in a graphical form.

Mathematically, a tree is an acyclic connected

graph where each node has zero or more children nodes and at most one parent node. Furthermore, the children of each node have a specific order.

In computer science, a tree is a widely-used data structure that emulates a hierarchical tree structure with a set of linked nodes.

00:23:33

2

Department of Computer Science-BGU

Page 3: Binary trees יום חמישי 02 יולי 2015 יום חמישי 02 יולי 2015 יום חמישי 02 יולי 2015 יום חמישי 02 יולי 2015 יום חמישי 02 יולי

Binary trees : definitions

Binary tree is a tree data structure in which each node has at most two child nodes, usually distinguished as "left" and "right".

nodes with children are parent nodes, and child nodes may contain references to their parents.

A node that has a child is called the child's parent node (or ancestor node, or superior). A node has at most one parent.

nodes that do not have any children are called leaf nodes.

00:23:33

3

Department of Computer Science-BGU

Page 4: Binary trees יום חמישי 02 יולי 2015 יום חמישי 02 יולי 2015 יום חמישי 02 יולי 2015 יום חמישי 02 יולי 2015 יום חמישי 02 יולי

Binary trees : definitions (cont.)

The height of a node is the length of the longest downward path to a leaf from that node.

The height of the root is the height of the tree. The depth of a node is the length of the path to

its root (i.e., its root path). An internal node or inner node is any node of

a tree that has child nodes and is thus not a leaf node.

A subtree of a tree T is a tree consisting of a node in T and all of its descendants in T.

00:23:34

4

Department of Computer Science-BGU

Page 5: Binary trees יום חמישי 02 יולי 2015 יום חמישי 02 יולי 2015 יום חמישי 02 יולי 2015 יום חמישי 02 יולי 2015 יום חמישי 02 יולי

Common operations

Enumerating all the items Enumerating a section of a tree Searching for an item Adding a new item at a certain position on the

tree Deleting an item Removing a whole section of a tree (called

pruning) Adding a whole section to a tree (called grafting) Finding the root for any node

00:23:34

5

Department of Computer Science-BGU

Page 6: Binary trees יום חמישי 02 יולי 2015 יום חמישי 02 יולי 2015 יום חמישי 02 יולי 2015 יום חמישי 02 יולי 2015 יום חמישי 02 יולי

node – Definition and making

typedef struct node_t {int data;struct node_t *right, *left;

}node;

node *make_node(int y) {node *newnode;newnode=(struct node *)malloc(sizeof(struct node));newnode->data=y;newnode->right=newnode->left=NULL;return(newnode);

}

00:23:34

6

Department of Computer Science-BGU

Page 7: Binary trees יום חמישי 02 יולי 2015 יום חמישי 02 יולי 2015 יום חמישי 02 יולי 2015 יום חמישי 02 יולי 2015 יום חמישי 02 יולי

Sorting tree

In computer science, a binary search tree (BST) or ordered binary tree is a node-based binary tree data structure which has the following properties:

The left subtree of a node contains only nodes with keys less than the node's key.

The right subtree of a node contains only nodes with keys greater than the node's key.

Both the left and right subtrees must also be binary search trees.

00:23:34Department of Computer Science-BGU

7

Page 8: Binary trees יום חמישי 02 יולי 2015 יום חמישי 02 יולי 2015 יום חמישי 02 יולי 2015 יום חמישי 02 יולי 2015 יום חמישי 02 יולי

Examples

00:23:34Department of Computer Science-BGU

8

Page 9: Binary trees יום חמישי 02 יולי 2015 יום חמישי 02 יולי 2015 יום חמישי 02 יולי 2015 יום חמישי 02 יולי 2015 יום חמישי 02 יולי

Making Sorting Tree

void main(){node *root = NULL;int data;

do { printf("\n Enter number:"); scanf("%d",& data); if(data==-1) break; root = build_tree(root, data);

} while(1);}

00:23:34Department of Computer Science-BGU

9

Page 10: Binary trees יום חמישי 02 יולי 2015 יום חמישי 02 יולי 2015 יום חמישי 02 יולי 2015 יום חמישי 02 יולי 2015 יום חמישי 02 יולי

Adding node to sorting tree

node * build_tree( node * root, int data){if (root == NULL)

root = newnode(data);else if (data <= root->data)

root->left = build_tree(root->left, data);else

root->right = build_tree(root->right, data);return root;

}

00:23:34

10

Department of Computer Science-BGU

Page 11: Binary trees יום חמישי 02 יולי 2015 יום חמישי 02 יולי 2015 יום חמישי 02 יולי 2015 יום חמישי 02 יולי 2015 יום חמישי 02 יולי

Binary Tree Traversal - Inorder

void inorder(node *r){ if(r!=NULL){

inorder(r->left);printf("\t %d",r->data);inorder(r->right);

}}

00:23:34

11

Department of Computer Science-BGU

Page 12: Binary trees יום חמישי 02 יולי 2015 יום חמישי 02 יולי 2015 יום חמישי 02 יולי 2015 יום חמישי 02 יולי 2015 יום חמישי 02 יולי

Binary Tree Traversal - Preorder

void preorder( node *r) {if(r!=NULL) {

printf("\t %d",r->data);preorder(r->left);preorder(r->right);

}}

00:23:34

12

Department of Computer Science-BGU

Page 13: Binary trees יום חמישי 02 יולי 2015 יום חמישי 02 יולי 2015 יום חמישי 02 יולי 2015 יום חמישי 02 יולי 2015 יום חמישי 02 יולי

Binary Tree Traversal - Postorder

void postorder( node *r) {if(r!=NULL) {

postorder(r->left);postorder(r->right);printf("\t %d",r->data);

} }

00:23:34

13

Department of Computer Science-BGU

Page 14: Binary trees יום חמישי 02 יולי 2015 יום חמישי 02 יולי 2015 יום חמישי 02 יולי 2015 יום חמישי 02 יולי 2015 יום חמישי 02 יולי

Functions on Trees

00:23:34Department of Computer Science-BGU

14

Page 15: Binary trees יום חמישי 02 יולי 2015 יום חמישי 02 יולי 2015 יום חמישי 02 יולי 2015 יום חמישי 02 יולי 2015 יום חמישי 02 יולי

Print leaves

void printLeaves (node * root) {if (root == NULL)

return;if (root->left == NULL && root->right == NULL)

printf("(%2d)", root->data);else {

printLeaves(root->left);printLeaves(root->right);

}{

00:23:35Department of Computer Science-BGU

15

Page 16: Binary trees יום חמישי 02 יולי 2015 יום חמישי 02 יולי 2015 יום חמישי 02 יולי 2015 יום חמישי 02 יולי 2015 יום חמישי 02 יולי

Size of tree

int size(node * root) {if (root == NULL)

return 0;return 1 + size(root->left) + size(root->right);

{

00:23:35Department of Computer Science-BGU

16

Page 17: Binary trees יום חמישי 02 יולי 2015 יום חמישי 02 יולי 2015 יום חמישי 02 יולי 2015 יום חמישי 02 יולי 2015 יום חמישי 02 יולי

Height of tree

int height(node * root) {int l_h, r_h;if (root == NULL)

return -1;l_h = height(root->left);r_h = height(root->right);return 1 + (l_h > r_h) ? l_h : r_h;

}

00:23:35Department of Computer Science-BGU

17

Page 18: Binary trees יום חמישי 02 יולי 2015 יום חמישי 02 יולי 2015 יום חמישי 02 יולי 2015 יום חמישי 02 יולי 2015 יום חמישי 02 יולי

Searching data in tree

int search(node * root, int data) {if (root == NULL)

return 0;if (root->data == data)

return 1;return search(root->left, data)

|| search(root->right, data);

}

00:23:37Department of Computer Science-BGU

18

Page 19: Binary trees יום חמישי 02 יולי 2015 יום חמישי 02 יולי 2015 יום חמישי 02 יולי 2015 יום חמישי 02 יולי 2015 יום חמישי 02 יולי

Searching in tree

int search(node * root, int data) {if (root == NULL)

return 0;if (root->data == data)

return 1;if (data < root->data)

return search(root->left, data);else

return search(root->right, data);}

00:23:37Department of Computer Science-BGU

19

Page 20: Binary trees יום חמישי 02 יולי 2015 יום חמישי 02 יולי 2015 יום חמישי 02 יולי 2015 יום חמישי 02 יולי 2015 יום חמישי 02 יולי

Freeing tree

// frees all the allocated memory and sets the root to NULL

void empty_tree(node **root) {if (!(*root))

return;empty_tree(&((*root)->left));empty_tree(&((*root)->right));free(*root);*root = NULL;

}

00:23:37Department of Computer Science-BGU

20