Presentation is loading. Please wait.

Presentation is loading. Please wait.

Tree 1 2 3 A tree is a data structure in which each node is comprised of some data as well as node pointers to child nodes. 4 5 6 7 8 9.

Similar presentations


Presentation on theme: "Tree 1 2 3 A tree is a data structure in which each node is comprised of some data as well as node pointers to child nodes. 4 5 6 7 8 9."— Presentation transcript:

1 Tree 1 2 3 A tree is a data structure in which each node is comprised of some data as well as node pointers to child nodes. 4 5 6 7 8 9

2 Root 1 2 3 4 5 6 7 Leaves We'll refer to node 1 as the root node, and external nodes like 5, 6, 7, 8, and 9 as leaves. It's also convention to use the following terms to describe relationships between nodes: nodes 2 and 3 are siblings of each other nodes 5, 6, and 7 are children of node 3 node 2 is the parent of node 4 8 9

3 Binary Tree 33 99 77 A binary tree is a type of tree in which each node is comprised of some data as well as node pointers to at most two children. 88 22 11 66

4 typedef struct node { int n; struct node* left; struct node* right; }
55 Here's an example of a binary tree node implementation. Note how each node in a binary tree is comprised of two node pointers and some data. n is the data stored in this node, and left and right are pointers either to the node's children (or to NULL if no children exist).

5 Binary Search Tree 55 33 77 33 77 A binary search tree is a special type of binary tree that simplifies searching. For each node in a binary search tree, every value on its left child's side is less than its own value, and every value on its right is greater. Notice how all values to the left of the root (55) are less than 55, and all the values to the right are greater than 55. 22 44 66 88 22 44 66 88

6 bool search(node* root, int val) { if root is NULL return false.
if root->n is val return true. if val is less than root->n search left child if val is greater than root->n search right child } Here's some pseudocode to search a binary search tree for a particular value val. If the current node's value is less than the value you're looking for, recursively search its right child. If the current node's value is greater than the value you're looking for, recursively search its left child.


Download ppt "Tree 1 2 3 A tree is a data structure in which each node is comprised of some data as well as node pointers to child nodes. 4 5 6 7 8 9."

Similar presentations


Ads by Google