Search a Subtree to find many characterisitcs - c

I have many subtrees saved in a file and I want to search them to find many things for each one of these subtrees like : the number of nodes, the number of leafs and the number of levels a subtree consist of...
To be more precise, the difference between a node and a leaf in my work; a node is any vertex in a subtree that could be a parent or a child where as a leaf is only a child vertex i.e every leaf is a node and the opposite is not true.
I am facing many problems in this work, the first one: the file containing the subtrees is not showing the rooted node and is not differentiating between parents and children..
The second problem: I read that for searching a tree programmers usually use a recursive method so I tried to search through the INTERNET for references or algorithms or pseudo-codes but all what I found is dealing with binary tree which is not my case (I am dealing with all configurations of a subtree) !!!
So could anyone kindly help me by giving a reference, an algorithm or an example for searching a tree to find the previous characteristics for such a subtree??
Another question: Is it possible to do this work using R ??
I will use any program to write the code but mainly I am interested in C.
Again,, please my subtree is not a binary one
UPDATE:
Each subtree is represented in my file as a set of edges, You can see below an example of a subtree of size 4:
44180 0
44180 18238
44180 13362
69677 44180
UPDATE: Sorry for the new update but Can I use R in my case even if there is a huge number of subtrees like 100000 subtrees each one with 20 edges (100000*20) ??

A Tree is well defined.
I am going to assume that each file describes exactly one tree. It also seems reasonable to assume that the line 44180 0 means an edge from node 44180 to node 0. You should double check these assumptions.
With these assumptions, you can parse the file into the following data structure:
Node
int id (id of self)
Node* parent (pointer to parent Node)
Node** children (list of children Nodes)
Or even simpler:
Node
int id
int parent (id of the parent node)
int* children (ids of all children)
Once you have the entire file parsed into a list/array of Nodes, then pick ANY node, and recursively traverse up the parent node until you hit a node that has no parent. Then the final node must be the root of the tree.
Now that you have the pointer to the root Node, you should be able to apply other algorithms.

Related

C Algorithm for bounded tree traversal

I am looking for a way to traverse an ordered binary tree with a left and right limit.
So traverse only nodes that are within a left and right bound, preferably inorder and iterative.
I tried modifying an iterative inorder implementation i found on that site: https://www.geeksforgeeks.org/inorder-tree-traversal-without-recursion/
The problem i am facing right now is, that i cant just say dont go left if left node <= leftBound and dont go right if node >= rightBound, cause a node smaller/bigger than a given bound can still have childen within that bound.

Tree number of levels

I wrote a tree in which each node has a list of his children. So my questions are : How can I compute the number of levels of my tree? Can anyone give me some documentation about this ?. Thank you :).
There could be different ways to solve this issue. One solution could be like count the nodes using a counter variable and increment the counter until the leaf node reaches. But you have to take care of
1. Longest Chain
2. Redundancy in couting
Second if each node has a list of children then count the nodes through this list present in root node.
A very appropriate way would be to define a variable by name Level into the struct node
typedef struct node {
...
Other members
...
int node_level;
} NODE;
and initialize it with 1 when root or any other node is created. Then update its value on each insertion into the tree.
By doing so you can see the level of any child tree, whenever you need to find. Also note each inserted node would have a level 1 and its ancestors will have greater level.

Adding to an AVL tree

When I am adding a value to an AVL tree, how do I know where to insert it? I'm not talking about the coding aspect, but more of a pictorial representation. I'm assuming the insertion is done at the first available external node, but I'm unsure, as the example given in my textbook inserts at a random external node.
AVL tree is a self-balancing binary search tree and binary search trees have following property: the left subtree of every node contains only nodes with keys less than the node's key and the right subtree of every node contains only nodes with keys greater than the node's key. So you have to insert new node in such way that stated property remains after insertion: you start with root node and working towards leaf by comparing new node key and current node key if the new node key is greater you visit right child else you visit left child next and when there is no child to visit in that place you insert new node.

convert a binary search tree to doubly linklist using recursion

As mentioned in the Title , I have a Binary search tree. I want to convert it to sorted doubly linklist using recursion.
My code
for each node in tree
find max of left sub-tree and assign its right to present node ,present node left to max
find min of right sub-tree and assign its left to present node ,present node right to max
and now recursively do same thing to other nodes in BST .
but this solution is not efficient as it reaches each node more than one time .In my quest of optimized code i got a link from google greatTreeList sollution . I have searched the same in SO both sollutions are same and worked for me. I didnt understand the append function of the sollution as it contains code
join(alast,b)
join(blast,a)
For tree whose nodes are inserted in following order 10,5,9,6,8,7,12,11,13,14
can anyone please explain how
join(alast,b)
join(blast,a)
are linking node in each recursion call.
I think you are over thinking this actually quite easy task - extracting the data from a binary tree in order is as simple as doing a depth first traversal - this is the point of a binary tree that it very efficiently gives you the elements in the sorted order.
So what you need to do is a standard depth first walk of the tree and each time you find a node add it to you linked list.
This in order depth first recursion is fairly straight forward is pseudocode:
Traverse(Node N)
Traverse(N.left);
Add N to the linked list
Traverse(N.right);
I suggest you try this manually on you example so you see how it works.
In order to convert a binary search tree to a sorted doubly linked list, one typically performs an inorder depth-first traversal, building the list along the traversal.
Try to come up with code that performs an inorder depth-first traversal and prints the items of the binary tree in sorted order. From there, it should be easy to complete your task.
Expanding on Elemental's answer
Traverse(Node N)
Traverse(N.left);
Add N to the linked list
Traverse(N.right);
To add N to the linked list,
your linked list class or data structure should have an append() method or similar.
Use your imagination, but something like this:
def append(N):
new_tail = node(val=N, prev=self.tail, next=None)
self.tail.next = new_tail
self.tail = new_tail
of course you also need to add self.head = self.tail the first time you append.

Finding the rank in a red-black tree without using a parent pointer

I was given code for a red-black tree in class. The struct used to create a node does not have a parent pointer. I have most of my project working, but I cannot figure out how to compute the rank in O(lg n) time. By rank, I mean if you were to do an inorder-traversal and save the keys to an array starting at index 1, what index the given key would be stored. Doing this would be in O(n) time though which is not allowed.
Reading through CLRS, the chapter Augmenting Data Structures has code to return the rank given the key. This is exactly what I need, but the problem is the code uses a parent pointer. Since we never used parent pointers in any of our red-black tree examples and this code does not include a parent pointer, I don't believe we are to change the entire given code just to get the rank to work, which leads me to believe there is a way to do it without using a parent pointer.
The (fields?) that exist in the node struct are: a key (int), a pointer to the left child, pointer to the right child, sub-tree size (int), and the color (int).
All code is done in C. What I am looking for is if this is possible, and how I might accomplish this with or without source code (a good explanation would be perfect).
Assumption: sub-tree size includes root node of sub-tree. Call the value to be ordered in a.
Then, this algorithms gets you the rank in O(lgn):
1: let rank=subtree size(root of tree)
2: if you go left:
- adjust rank=rank - (subtree size(sts) of right child (rc) of root) - 1
- move to left child(lc) of root
3: if you go right:
- adjust rank=rank(prior)
- move to rc(root)
4: iterate 2-3 (replacing root with current node) until you are at the node with value a
5: if this node has a rc, adjust a final time
- rank = rank - (sts(rc))
Done.
Note: assumes the usual left-to-right lower-to-higher ordering of rb tree.

Resources