Adding to an AVL tree - 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.

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.

Search a Subtree to find many characterisitcs

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.

Counting the numbers in array that are on left and are bigger than element

I need help with this problem, I have given array of N elements, and I want to generate new array in which for every index I will be keeping how many numbers are on left of this index and are bigger than this element.
Let's say we have this array {3,2,1,0} and i want to generate this array {0, 1,2,3}. In the second array we have zero because there are no elements on left of the element 3, we have 1 because number 3 is on the left of number 2 and it is bigger...
I think this could be done with binary index tree, but i don't know how to implement it.
Thanks in advance.
You can do this in NlogN by constructing a binary tree and saving some metadata on the nodes along the way - namely the current size of the right subtree of each node. After each element is added, you can count the number of elements that were previously added to the tree that are larger than it. I will assume you know how to create a binary search tree by inserting nodes one by one. So let's split this into two things we need to do:
Maintain the size of the right subtree of each node: At each node we pick if the current value goes on the right subtree (current value is larger than the node value) or left subtree. If we choose to go right, increment the value of the rightSubtreeSize by one.
Count how many elements that were previously inserted are larger than the current element: Assuming that for each node we know the size of the right subtree, we can now count how many elements to the left of the current element are larger than it (i.e. elements to the left have already been added to the tree). Again, we follow the binary tree insert operation. At each node, if the current value is smaller than the node, it means that the node and it's whole right subtree are larger than the current value. so for each node we traverse, we keep a sum of all the elements that are larger than the current value. By the time we finish inserting to the tree, we have the sum we are looking for.
Let me know if you need any clarification.

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.

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.

Resources